1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

how to compile Obj-c file in terminal

Discussion in 'Mac Programming' started by uaecasher, Jul 29, 2009.

  1. macrumors 65816


    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?

  2. Moderator emeritus


    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:
    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.
  3. macrumors 65816


    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
  4. Moderator emeritus


    Did you install Xcode?
  5. macrumors 65816


    yes i installed it and tried to build the same app and it worked
  6. Guest

    What does the following command in the Terminal output?

    /usr/bin/gcc -v
    And try this as well:

    /Developer/usr/bin/gcc -v
  7. macrumors 65816


    strange, both give me no such directory

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


    Staff Member

    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?)
  9. Guest

    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)?
  10. macrumors 65816


    here are screen shots

    Attached Files:

  11. macrumors 65816


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

    OK -- try this:
    /Volumes/Loeprad\ HD/Developer/usr/bin/gcc -v
  13. macrumors 65816


    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)
  14. Guest

    Reinstalling XCode under Snow Leopard should definitely help.
  15. macrumors regular

    You must install the Command Line tools with XCode. They are optional.
  16. macrumors regular

    It would help if this:

    /Volumes/Loeprad\ HD/Developer/usr/bin/gcc -v
    had the correct spelling of Leopard.
  17. macrumors newbie

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

    That spelling was taken straight from his screenshot.
  19. macrumors 65816


    i solved the problem by reinstalling xCode

    thanks for your help guys

Share This Page