how to compile Obj-c file in terminal

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

  1. macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #1
    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
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Assuming the file is called file.m and is located on the Desktop:
    Code:
    cd ~/desktop
    gcc file.m -o file -framework Foundation
    Then to run:
    Code:
    ./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.
     
  3. thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #3
    I'm getting:

    Code:
    -bash: gcc: command not found
    here is my code:


    Code:
    gcc Untitled.m -o Untitled -framework Foundation
    the file name is Untitled and it's in Home directory
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  5. thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #5
    yes i installed it and tried to build the same app and it worked
     
  6. Guest

    Joined:
    Mar 6, 2007
    #6
    What does the following command in the Terminal output?

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

    Code:
    /Developer/usr/bin/gcc -v
     
  7. thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #7
    strange, both give me no such directory

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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    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

    Joined:
    Mar 6, 2007
    #9
    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. thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #10
    here are screen shots
     

    Attached Files:

  11. thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #11
    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

    Joined:
    Mar 6, 2007
    #12
    OK -- try this:
    Code:
    /Volumes/Loeprad\ HD/Developer/usr/bin/gcc -v
     
  13. thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #13
    i get this :


    Code:
    -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

    Joined:
    Mar 6, 2007
    #14
    Reinstalling XCode under Snow Leopard should definitely help.
     
  15. macrumors regular

    Joined:
    Jun 7, 2009
    #15
    You must install the Command Line tools with XCode. They are optional.
     
  16. macrumors regular

    Joined:
    Jul 6, 2009
    #16
    It would help if this:

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

    Joined:
    Jun 23, 2009
    #17
    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...

    Code:
    gcc-4.2 MyFile.m -framework Foundation
    Or if you just want to verify that method would work, ask for the version number...
    Code:
    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

    Joined:
    Mar 6, 2007
    #18
    That spelling was taken straight from his screenshot.
     
  19. thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #19
    i solved the problem by reinstalling xCode

    thanks for your help guys
     

Share This Page