PDA

View Full Version : how to compile Obj-c file in terminal




uaecasher
Jul 29, 2009, 08:40 AM
hello, I'm reading the "Programming in Obj-C 2.0" book and I didn't understand how to compile the file using terminal.

any help?

thanks



kainjow
Jul 29, 2009, 11:01 AM
Assuming the file is called file.m and is located on the Desktop:
cd ~/desktop
gcc file.m -o file -framework Foundation

Then to run:
./file

They key thing is to name the file with a .m extension so it's compiled as Objective-C, and to remember to link to whatever frameworks you're using via the -framework argument.

uaecasher
Jul 29, 2009, 11:12 AM
I'm getting:

-bash: gcc: command not found

here is my code:


gcc Untitled.m -o Untitled -framework Foundation

the file name is Untitled and it's in Home directory

kainjow
Jul 29, 2009, 11:22 AM
Did you install Xcode?

uaecasher
Jul 29, 2009, 11:24 AM
yes i installed it and tried to build the same app and it worked

mags631
Jul 29, 2009, 11:26 AM
What does the following command in the Terminal output?

/usr/bin/gcc -v

And try this as well:

/Developer/usr/bin/gcc -v

uaecasher
Jul 29, 2009, 11:30 AM
strange, both give me no such directory

maybe because I'm using snow leopard? how can I install GCC?

dejo
Jul 29, 2009, 11:39 AM
how can I install GCC?
When you install Xcode, it installs gcc. You said you installed Xcode, right? What is in your /Developer directory? (i.e.: what does 'ls /Developer' say?)

mags631
Jul 29, 2009, 11:45 AM
strange, both give me no such directory

maybe because I'm using snow leopard? how can I install GCC?

Do you still have a partition with something other than Snow Leopard (e.g., Leopard)? Was XCode installed under a different version of Mac OS X (i.e., not Snow Leopard)?

uaecasher
Jul 29, 2009, 11:50 AM
here are screen shots

uaecasher
Jul 29, 2009, 11:55 AM
Do you still have a partition with something other than Snow Leopard (e.g., Leopard)? Was XCode installed under a different version of Mac OS X (i.e., not Snow Leopard)?

ya, i had leopard (which i think there is where I installed xCode) then i updated to snow leopard and before two days I added dual booting with leopard

mags631
Jul 29, 2009, 12:09 PM
ya, i had leopard (which i think there is where I installed xCode) then i updated to snow leopard and before two days I added dual booting with leopard

OK -- try this:
/Volumes/Loeprad\ HD/Developer/usr/bin/gcc -v

uaecasher
Jul 29, 2009, 12:13 PM
i get this :


-bash: /Volumes/Loeprad HD/Developer/usr/bin/gcc: No such file or directory


btw what if i delete the old xCode and download iPhone SDK (it include xCode 3.2) (mine is 3.1)

mags631
Jul 29, 2009, 12:20 PM
i get this :


-bash: /Volumes/Loeprad HD/Developer/usr/bin/gcc: No such file or directory


btw what if i delete the old xCode and download iPhone SDK (it include xCode 3.2) (mine is 3.1)

Reinstalling XCode under Snow Leopard should definitely help.

rossipoo
Jul 29, 2009, 01:36 PM
You must install the Command Line tools with XCode. They are optional.

m3kilpat
Jul 29, 2009, 02:09 PM
It would help if this:

/Volumes/Loeprad\ HD/Developer/usr/bin/gcc -v

had the correct spelling of Leopard.

daracne
Jul 29, 2009, 02:27 PM
Judging by the screenshots you posted, it looks like you have a ton of broken symlinks? I would try invoking the actual gcc binary directly by compiling with this command...

gcc-4.2 MyFile.m -framework Foundation

Or if you just want to verify that method would work, ask for the version number...
gcc-4.2 -v

If that works, then I would try uninstalling xcode completely and reinstalling it to see if it fixes those symlinks.

mags631
Jul 29, 2009, 05:24 PM
It would help if this:

/Volumes/Loeprad\ HD/Developer/usr/bin/gcc -v

had the correct spelling of Leopard.

That spelling was taken straight from his screenshot.

uaecasher
Jul 30, 2009, 08:41 AM
i solved the problem by reinstalling xCode

thanks for your help guys