How can I improve my C coding process on OS X? - c

I'm currently learning C from some Harvard screencasts. They're great. I'm currently, because I'm used to it, using this process for running my code:
Type it it TextMate
Save it
gcc filname
run "./a.out"
goto 1
Is there a major process, program, or best practice I'm missing here? I've tried apple-R in TextMate, but to compile something bare-bones like...
#include <stdio.h>
int
main(int argc, char * argv[])
{
int x = 342;
int y = 92;
int z = x + y;
printf("these values added are: %d\n", z);
return 0;
}
...takes 4 seconds which is clearly no good. Is there a great environment/compiler shortcut, or anything else I could be doing to make the actual testing more streamlined?
Thanks!

I use text mate and gcc in the terminal. I find Xcode too bulky for small commandline programs in C. It's great if you want to do cocoa GUI applications, need a quick Apple API reference or want a graphic frontend to gdb etc. 4 seconds sounds excessive for the program you have above, but it's probably due to Textmate cmd-R shortcut. I have never used that for anything else than scripts. But I agree that you should install Xcode, it's on your Mac OS install disk.

I disagree if you want to code ANSI C then this is what I'd suggest. Get familiar with a more powerful text editor, be it vim, emacs or whatever. I prefer vim.
See here:
http://macvim.org/OSX/index.php
Now, you should try to setup GNU Make for Mac OS X.
Doing this you will learn the methods most C programmers have used for ages. I think you'll get a lot more out of this than using Xcode straight out. With Xcode you want to make sure you are doing C and not Objective-C.
Now, with an editor like vim, you can do everything from a single terminal and waste fewer cycles switching around in your workflow.
So your workflow becomes:
$vim foo.c
(save)
$make
$./a.out
If you want to do any serious C programming that has any relevance to the UNIX envirnoment (and I suspect that Harvard course does), you will need to learn make at some point.

Install Apple's development tools, and use the XCode IDE. It may or may not increase your compile time, but it will greatly decrease much of the pain of hand coding and compiling on the command line.
To create a simple command-line C program (like the one you're describing above) do this:
File->New Project
Choose Command Line Tool as the project type.
The template includes a "hello world" program with a main.c file.
Edit the main.c file to include the code you're working on.
Run the project by choosing the Build and Run button.
**be sure to open the Console to view the output of your program. (Run->Console)
Good luck!

As mentioned before, XCode is the Mac way of doing it.
Just to give you an alternative solution, you can also use vi/vim from within a terminal. You open the file for the first time by typing vi filename.c. Then you type :w to save the file, :!gcc filename.c to compile it and :!./a.out to run it. The exclamation point does the trick of calling a shell command, so the last two commands can be replaced by one :!gcc filename.c; ./a.out . You also have a command history ("arrow up", "arrow down") from within vi, after pressing the colon.
Getting used to vi is a different story...

Try to build with gcc -Wall -Werror and try to understand and fix your warnings. That is always good practice.

I wouldn't recommended switching to XCode unless you need to use the debugger. GDB integration with XCode is pretty nice, and non-existent with Textmate.
To improve your Textmate workflow for smallish projects, learn to use make and install the Makefile bundle for Textmate. You can find it on GitHub.
This will give you a new Build ("Command-B") option in Textmate that will run make, parse the output, and let you easily correct compile time errors.

I think you should continue what you're doing. 4 seconds isn't that much time to wait, especially once your programs grow more extensive. Just using a basic text editor and gcc will let you master the basics of C. Once you're ready to start making GUI apps, then I would suggest to start using XCode to take advantage of the tools to make those kind of applications. I think an IDE is overkill at this point.

There should use Emacs or Vim with a Makefile. They can do anything.
For example, in Emacs, compiling is as simple as M-x Compile (But of course you could bind any key to it). Emacs is also cross-platform (Unlike XCode and TextMate).
Emacs also provides various other features useful for debugging (Built-in gdb support, ability to parse compile output, etc)
Using an IDE-Specific build tool is the exact wrong way to do things (Especially if that IDE happens to be platform specific, like XCode and TextMate)

Related

Run C programs from sublime 2.02

I want to compile and run C/C++ programs with sublimetext 2 itself.I dont want to use terminal to do so. What do I need to do for this??I am compeletely new to this so a lot of answers to similar questions did not help me as most of them were for windows.
sublime text 2 build system for C programming language should show you how to setup your build environment. Note: you need gcc etc. installed
Edit: you should although run your programm in a Terminal because SublimeText2 is an Editor and can not recieve user input for your programm. so you better just build it with ST2 and run it in Terminal or you just forget about ST2 and make your own Bash script that manages compiling and runs the programm.
I would use sublime text as an editor (which is what IDEs are). Then you'll need to have a Makefile for your program. And build it by typing make in a terminal (or by configuring your editor to run that command). BTW, you can type m a k e enter once in your day (and later use up in the same terminal, to reapeat the latest command).
Learning how to write simple Makefile-s is a reusable skill (see here for "why?"). See this and that and many other examples. Don't attach yourself to one particular editor (even if you may like one more than others; my preference goes to emacs).
You'll learn a big lot by studying (and improving) the source code of some free software. Look at sourceforge, github, etc.. to find some free software interesting to you, then download its source code, build it, and study it.
The tips and advices I gave here are still valid today, with a recent Ubuntu (e.g. 14.x)
BTW, your question looks like you might be confused; Sublime Text is an editor (perhaps glorified as an IDE). It does not compile anything. The C or C++ compiler on Ubuntu is GCC (using gcc command for compiling C code, and g++ command for compiling C++ code) or perhaps Clang/LLVM (using clang command for C code, and clang++ command for C++ code). That compiler is often started by make, and IDEs on Linux are often running make -or directly the compiler- command for you. You'll better be able to run these commands yourself.

Is there a "TUI" mode for standalone lldb?

Since gdb is getting onerous to work with on a Mac these days (at least I feel like I am fighting uphill against Apple), I've started to play around with lldb.
Is there an equivalent mode to gdb -tui that shows a nice, persistent view of the source and where you are in it when running lldb standalone from the command line? Obviously, in Xcode, there is such a display, but I deploy most of my code to Linux boxes eventually and would prefer to use the same development environment on both platforms (i.e., vim, Makefiles, autotools, etc.).
In top of tree svn source lldb there's a very new command line GUI mode that's been added by Greg Clayton. It's pretty neat with separate windows for the source, local variables, stack, threads, registers, etc. No console window yet. It's pretty cool. See http://lldb.llvm.org/build.html for how to build lldb from source if you'd like to check it out. To enter the GUI mode, just type gui from the lldb command line when you're debugging.
UPDATE: this gui feature is included in the lldb that ships in Xcode 6 and later. It's a nights-and-weekends side project that isn't seeing a lot of active development right now but give it a try.
Sadly, no.
However, I did came across this:
https://github.com/snarez/voltron
If you are familiar with screen/tmux it works pretty good.
Type gui in lldb's prompt after you're at an interesting breakpoint. It has a small tutorial the first time it's opened and built-in help.
I'm having good luck with a free GUI facade from Affinic. It's not perfect (or, at least, my understanding of it is not perfect). I must occasionally restart it and sometimes it gets unsynchronized from particularly gnarly / fancy C++ code, e.g., code that does a lot of implicit stuff in constructors. But overall it has saved me a lot of time over the old-fashioned way at the command line.
I have not been able to get the ncurses GUI mode in the LLDB CLI to work, and I have not tried Voltron.

Colored console output in Linux

I just started learning programming in C.
the first problem was to choose on which platform should I learn it, and I selected Ubuntu.
I found a GCC compiler to compile my projects, and it worked fine for me. I was running my compiled projects through Terminal.
But when I wanted to write a program which have to show a text on a colorful background, I understood that Terminal is not helping me. Actually I am learning from lessons written for programming on Windows, and they use there Borland C++ 5.2, but these lessons were written in 2007
So I am wondering, where I should run my compiled program in Ubuntu 10.10, to see the result properly?
Actually I tried to transfer to Windows, but Ubuntu on my computer works much faster, and it is easier I think to work in Ubuntu rather than Windows.
The simplest way to print color message without any toolkit is to use VT100 escape commands:
http://www.termsys.demon.co.uk/vtansi.htm#colors
So you can write something like this:
printf("\x1b[31mThis is red text\x1b[0m\n");
printf("\x1b[32mThis is green text\x1b[0m\n");
But this code works only for VT100-compatible terminals (almost all terminals of the Unix-system). The best way is to check TERM environment variable before colorizing your output.
Disclaimer: if you want to write code capable with all terminal types then you should use ncurses-like library.
The ncurses library does exactly what you are looking for. Using this library, you can choose to write any text, anywhere on the screen with any text/background color.
You can read more on it on Wikipedia. You can install it under ubuntu with:
$ sudo apt-get install libncurses5-dev
If you want to start with GUI programming in Linux, you will probably have to look into one of GUI toolkits, which is a library to write GUI elements, widgets, etc.
One of these toolkints is Gtk, which you can start to learn via Gtk tutorial. Another option is Qt which is probably easier to use, but that is based on C++.
I'm afraid that Borland C++ Windows courses are pretty much unusable for Linux GUI programming.
Or, if you just to want a colored terminal text, look into this question: stdlib and colored output in C

non-IDE C development environment

How do I set up a non-IDE-based C development environment on Linux?
gcc + make + vi and you will live happily ever after!
A pretty good development environment is most likely already there--no setup necessary. This is because most Linux installations are GNU/Linux. GNU provides all the pieces you need for C software development.
Use the 'which' command to make sure these programs are in your execution path
which gcc
which make
Gcc is your compiler. Make lets you define the compiling and linking steps. (It can call gcc for you.)
You are likely to have several editors installed such as vi, nano, and GNU emacs. To see if you have emacs (for instance), type
which emacs
If the which command returns a valid path (i.e. it doesn't say which: no emacs in ...) then that means you can run the program just by typing its name:
emacs
for example.
Okay my environment:
Geany or gedit, make, GCC. If I have to code on the command line I'd probably go with vi or nano over emacs, I just can't cope with emacs anymore.
Okay, someone's gotta say it: Emacs, gcc, and make.
If - as a commenter suggested - you're looking for instructions on setting this up, there's probably no set up needed, as most Linux distributions I've used come with these programs already installed.
If you're not a masochist:
gcc + cmake + nano ;)
Make sure to edit your .nanorc file (you can copy an example from /etc/nanorc generally)
Emacs + Cedet is the best
I use gtkemacs, gcc, make but more often scons these days.
This question is dangerously close to inciting religious wars. Let me get it started: All configurations other than mine are inferior.
I would recommend gcc + vim/vi + (make).
Vim/vi is almost impossible to "just pick up and use" because it will be completely foreign and you will struggle to even move the cursor or insert text, but fortunately, Vim comes with an extremely helpful tutor you can start by invoking the command vimtutor in the shell. It will get you up and running in no time!
While Vim's interface is really bizarre and esoteric at first, once you get used to it (shouldn't take very long once you finish vimtutor), you will see your productivity when editing code increase dramatically!

mac osx development environment

i have a unix c programming assignment in which i do some advanced C network programming.this is my first step into advance programming. so i was wondering what is the best combination of tools for this on a mac. using an IDE like Eclipse is how i'd normally do it but i have to make my own makefiles and stuff. so i would like to learn how it can be done effectively using may be emacs or vim + other tools. it will be quite a big project so i am worried about project management and debugging issues mostly as well as the productivity factor. in essence i want to learn how programmers do it in the professional environment without the bloated IDE part. i am using Snow Leopard.i would also delve into C++ and python in the future so may be something that will be useful for those as well.
I know you're asking for how to do it with makefiles/VI/etc. but on the Mac, Xcode is really the way to go, especially for large projects. It's a very effective wrapper that will call gcc and gdb and the linker for you. Especially when moving to a new platform, not having to worry with many of the pesky details will be a big leap in productivity. It's IDE debugger is quite awesome.
Of course you can also use makefiles etc. Many projects (just to take OpenSSL as an example) come with makefiles and you can compile them on the Mac from the commandline just like under the *ix operating systems, i.e. calling ./configure and then make. But setting up stuff like that (e.g. compiler options for universal binaries and such) is tedious while the IDE it's just a few options. Also, if you google for specific questions, you will find far more answers on how to do it with Xcode.
If you want to get started with Xcode, it's either on your Mac operating system CD (it just does not pre-install automatically) or you can download it from Apple. When you run it, just open a Mac OS X Project of type "Application - Commandline Tool" and you'll have a project with a main.c set up in a minute. You can then just run it or run it in the debugger like that and adding more source files to it is rather easy.
Xcode can be quite a beast for setting up an already large project (we ported a large project with DLLs and depending exes (overall 250000 lines of code) to the Mac and just getting that all set up wasn't what you call a piece of cake) but if you start from scratch you'll easily grow into it.
Bottom line is that Xcode certainly is equipped to deal with large projects and I can not imagine a more productive way of doing it (I have used hand written makefiles and such in the past so I know both worlds).
Xcode is your friend. It's free and is a very nice IDE. When you launch XCode, just start a new Console application (it'll be ANSI C).
Enjoy.
If learning the rudiments of unix editors, shell programming, make, etc., are part of the assignment, then you just need to dive in and learn what you need to learn. Some good books will help. Obviously you need K&R. I always liked the O'Reilly books for Unix stuff, usually because they are the thinnest. I hate thick computer books because they never get read. You should also learn how to use the man pages.
Vim vs. Emacs is a religious choice. If you ask any Unix guy what is the best, he will invariably tell you the one he learned first, because chances are he never learned the other. In my case, I've been using Vim so long that my escape key is worn out and the commands are hard-wired into my brain. Obviously, I think it's way better than emacs (which I never learned!) If you are lucky enough to have a Mac as a work station, install mac vim. It's great.
Make is complicated enough so that you will never really master it. Just learn enough to compile and link your program. You can always learn more if you need it.
Version control is an interesting question... I use RCS for small stuff. Like vi, it is on every Unix machine. For really big projects, I use subversion, but like editors, most people use whatever they learned first. Git people will say its the only one to use, etc.
Command line debuggers are a pain, which is a main selling point for Xcode. I've used gdb, but I don't remember it as a pleasant experience. Its been so long since I used it, I can't even remember how to start it up. There must be better debuggers by now. Try google.
Bottom line, all the things you mentioned are big topics. You need to take realistic bites of each and not get tangled in the weeds. It can take years to master them all.
Finally, I'd stay as far away from C++ as possible! Objective C is much better. Personal prejudice!

Resources