Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

manee

macrumors newbie
Original poster
Sep 5, 2011
6
0
can someone please tell me how to write and compile a c program in terminal. i use xcode and don't really like it. :confused:
 

talmy

macrumors 601
Oct 26, 2009
4,726
332
Oregon
Edit the file with any text editor (I'm an emacs fan myself, but most people like vi and there is also nano and probably others). Compile with the gcc command.

Example:
yourfavoriteeditor hello.c
gcc -o hello hello.c
./hello
 

cqexbesd

macrumors regular
Jun 4, 2009
175
42
Germany
Compile with the gcc command.

There is nothing wrong with gcc but can I recommend you try out clang as well. The usage is almost identical but as a new user you might find the warnings and errors a bit easier to deal with. You should try turning on some extra warnings as well so the compiler gives you some hints about possible errors. So instead of:

gcc -o hello hello.c

try:

clang -Wall -o hello hello.c
 

talmy

macrumors 601
Oct 26, 2009
4,726
332
Oregon
Yep, use Clang. I had replied with the "generic answer" but have been using Clang myself. Superior error and warning messages wrt GCC.

I'll also add that anyone spending any time compiling from the command line should learn to use make. Every C program I work on (and there are lots) is built using make.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.