// 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
#include<stdio.h>
int main(void)
{
int x=2;
printf("X=%d",x);
return 0;
}

Building target “PracticeC” of project “PracticeC” with configuration “Debug” — (1 error)
cd /Users/moneysingh/Desktop/PracticeC
setenv MACOSX_DEPLOYMENT_TARGET 10.5
/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)

Recommended Answers

All 10 Replies

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

./attempt

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

./a.out

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

./attempt

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.

Are you using xCode? or are you using a text editor and terminal?

By the look of the output posted, he is using Xcode.

@gudads
If this is your first program I suggest you ditch X-Code and work on the terminal

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...

Do you know what is a terminal ?
Do you know how to open a file via the terminal ?- pico ? vi ?

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
then
./tryc

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

are you in the directory where those files are stored in terminal?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.