// I have no knowledge of using gcc compiler. Can some1 please help my creating my first program in c on mac book.... saving on desktop.

//saving as testing.c on desktop ..
// Creating C program
int main(void)
int x=2;
return 0;

Building target “PracticeC” of project “PracticeC” with configuration “Debug” — (1 error)
cd /Users/moneysingh/Desktop/PracticeC
/Developer/usr/bin/g++-4.0 -arch i386 -isysroot /Developer/SDKs/MacOSX10.5.sdk -L/Users/moneysingh/Desktop/PracticeC/build/Debug -F/Users/moneysingh/Desktop/PracticeC/build/Debug -filelist /Users/moneysingh/Desktop/PracticeC/build/PracticeC.build/Debug/PracticeC.build/Objects-normal/i386/PracticeC.LinkFileList -mmacosx-version-min=10.5 -o /Users/moneysingh/Desktop/PracticeC/build/Debug/PracticeC
ld: duplicate symbol _main in /Users/moneysingh/Desktop/PracticeC/build/PracticeC.build/Debug/PracticeC.build/Objects-normal/i386/testing.o and /Users/moneysingh/Desktop/PracticeC/build/PracticeC.build/Debug/PracticeC.build/Objects-normal/i386/main.o
collect2: ld returned 1 exit status
ld: duplicate symbol _main in /Users/moneysingh/Desktop/PracticeC/build/PracticeC.build/Debug/PracticeC.build/Objects-normal/i386/testing.o and /Users/moneysingh/Desktop/PracticeC/build/PracticeC.build/Debug/PracticeC.build/Objects-normal/i386/main.o
collect2: ld returned 1 exit status
Build failed (1 error)

I haven't used Xcode in a while, but since you've got it installed, open TextEdit (unless you're comfortable with a command-line text editor, of course), write your program in there, and save it as attempt.c in your home directory (the one with Movies, Music, and so on). Next, fire up your Terminal (Applications -> Utilities -> Terminal.app) and type

gcc attempt.c -o attempt -Wall

Next, type in


Now, to explain things a bit. Compiling the program is done, in the simplest form, using

gcc attempt.c

What this does is 1) call the gcc compiler, 2) pass it attempt.c as argument, and 3) produce object code in the file a.out (correct me if I'm wrong, but a stood for Assembly, right?).

If you compile it using the command above, you have to type


in order to run the actual program. You can, of course, control the name of the output file. This is done by calling gcc with the -o flag (standing for output) and passing an output file name to it. That is, if we wanted the name of the executable to be attempt, we would use

gcc attempt.c -o attempt

Now we run the program using


Compilation will fail if the compiler finds an error in your code, and you'll be notified. It won't fail, though, if your code generates warnings, but the actual program may not behave as intended, or even crash at some point during execution. If you want the warnings to appear on screen, you call gcc with yet another flag, the -Wall flag (Warnings all). This brings us to the way I compile programs, using the following command

gcc attempt.c -o attempt -Wall

A more generic form of it would be

gcc <filename> -o <output filename> <other flags and options>

Of course, there are many other flags and options out there for the compiler. For a complete list, check here.

Edited 6 Years Ago by creeps: n/a

where the F*** should i write the code??

when i use text edit it doesn't let me store in .c, it tells use both or use something like rtf extra...

Actually, TextEdit is a very bad idea, but I was trying to keep things away from the Terminal as much as possible. My fault.

Apparently, TextEdit does some stupid "enhancements" to your text, which are actually messing your C program. Try TextWrangler if you want a standard, GUI text editor. Otherwise, try MacVim (my personal choice), Aquamacs, or another editor, free or paid.

I also suggest that you 1) keep it cool (limit the use of the F word. If a simple text editor makes you go nuts, programming is _clearly_ not for you), and 2) pay (more) attention to the messages you receive from the computer. Therefore, read and understand the whole message, not think of them like "something like rtf extra".

if you use text edit go to preferences and change the setting under new document format to "plain text". you can then save it without the "something like rtf extra" hassle

well... guys.. i compiled and run it easily.

but i am not able to run it through terminal..
what command should i use in terminal?

i did use,
gcc tryc.c -o tryc

nothing happeps,
i know my program doesn't have any errors though.

This article has been dead for over six months. Start a new discussion instead.