Help Compiling in terminal

Discussion in 'Mac Programming' started by Adudefelldown, Sep 21, 2009.

  1. Adudefelldown macrumors newbie

    Joined:
    Jul 24, 2009
    #1
    I am trying to compile programs in terminal using the gcc command but everytime I do i get the error as follows:

    or this error when i try to link use -framework Foundation:


    pretty much the same error i guess. Both source files are simple one line print statements so I know the error doesn't lie there.

    I am running Snow Leopard on my mac book pro and I do have Xcode installed, I'm not sure if that matters though.

    If anyone could tell me how to link the library that would be great!
     
  2. rowsdower macrumors 6502

    Joined:
    Jun 2, 2009
    #2
    Can you post your code and the command you are using to compile?
     
  3. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    It looks like you've forgotten to include "stdio"
     
  4. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #4
    Put your classic "hello world" program in a file called "hello.c". Then at the prompt type "gcc hello.c". To run the program type "./a.out"

    Here is a guess: You have parenthesis messed up and what you think is a call to printf() is really outside of main() and you are defining printf rather then calling it.
     
  5. Adudefelldown thread starter macrumors newbie

    Joined:
    Jul 24, 2009
    #5
    I'm using this command for the following code
    gcc test.c
    and this command for the following code
    gcc -framework Foundation project1 -o prog1

     
  6. Adudefelldown thread starter macrumors newbie

    Joined:
    Jul 24, 2009
    #6
    I added this to one of the codes and still got the same errors

     
  7. EX1127 macrumors 6502

    EX1127

    Joined:
    May 26, 2009
    Location:
    Louisiana
    #7
    isnt import stdio suppose to be before main not after?
     
  8. Adudefelldown thread starter macrumors newbie

    Joined:
    Jul 24, 2009
    #8
    For the objective-c code I'm not sure how it works because I'm trying to learn it and just doing the early examples out of a book. it compiled just fine in xcode without the #import <Foundation/Foundation.h>


    EDIT:


    That is a miscopy on teh c code above the #include is outside the main

    it actually looks like this

     
  9. EX1127 macrumors 6502

    EX1127

    Joined:
    May 26, 2009
    Location:
    Louisiana
    #9
    My programming is not the best since im just now learning it but i know with C at least your program is suppose to look something like this
    #import<stdio.h>

    int main()
    {
    ...
    }
     
  10. Adudefelldown thread starter macrumors newbie

    Joined:
    Jul 24, 2009
    #10
    @EX ya i know I just somehow managed to copy my code wrong. Its actually above it like my edit shows.

    Edit.

    All of my code will compile with xcode but i still get those errors with gcc. I think i have to change my bash profile or something but dont know what i need to put in there because I had to create a profile just so it would recognize gcc
     
  11. mac2x macrumors 65816

    Joined:
    Sep 19, 2009
    #11
    Just be sure you aren't using gcc when you shoulda been using g++. Don't ask. :mad:

    Oh, what the heck. I was compiling a bit of C++ code and forgot and used the gcc command. Doesn't work very well.
     
  12. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    Your source code is written in the Objective-C language. Your source file's name, however, is only "project1", not "project1.m". Without a ".m" extension, gcc has to guess what language it is, and it's guessing wrong.

    Change the name of the file to "project1.m", then recompile.

    Code:
    gcc -framework Foundation project1.m -o prog1
    For reference, ".c" files are C, ".m" files are Objective-C, ".cpp" files are C++, and ".mm" files are Objective-C++.
     
  13. Adudefelldown thread starter macrumors newbie

    Joined:
    Jul 24, 2009
    #13
    Thanks that seems to help with the lcrt error atleast and makes sense it shoulda been obvious lol. I still get an error when compiling the c code


    and the objective-c code

    it seems to not be finding the header files for some reason. Is there somewhere where i have to specify where these different librarys are?
     
  14. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #14
    The compiler should work without any other configuration or command-line options. The following command-line will emit a bunch of compilation details, and list the full pathname of every #include'd file. Post its output.

    Code:
    gcc -v -H test.c
    
     
  15. Adudefelldown thread starter macrumors newbie

    Joined:
    Jul 24, 2009
    #15
    Heres the output from that command


     
  16. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    First, you used lower-case -h when it should be upper-case -H. Copy and paste the command exactly as it was given. Post the output.

    Second, the one useful part of the output is this:

    Code:
    #include <...> search starts here:
    /Developer/usr/bin/../lib/gcc/i686-apple-darwin9/4.0.1/include
    /usr/local/include
    /usr/lib/gcc/i686-apple-darwin9/4.0.1/include
    /usr/include
    /System/Library/Frameworks (framework directory)
    /Library/Frameworks (framework directory)
    End of search list.
    
    This tells me there's a "/usr/local/include" directory, and it will be searched for headers before the standard "/usr/include" directory. Without seeing the -H output, my guess is you've got a damaged or empty headers in /usr/local/include, and it's preventing the compiler from seeing the standard file.

    /usr/local/include is most likely something you installed yourself, because it's not part of the standard OS or Xcode install. I don't see /usr/local/include on my Leopard or Snow Leopard installs. In fact, the convention is that dirs or sub-dirs under /usr/local are not modified by any standard OS or tools install. The /usr/local sub-tree is managed entirely by the local user or administrator.

    My first guess is to remove (or move aside, or temporarily rename) /usr/local/include and try compiling again.

    One other thing: enter the following command and post the output:

    Code:
    ls -la /usr/include
    
    If it's not a fairly long listing, then something is broken.
     
  17. Adudefelldown thread starter macrumors newbie

    Joined:
    Jul 24, 2009
    #17

    i just got rid of that /usr/local/include and it still gets the same error
     
  18. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #18
    That looks bad. I think there should be a couple hundred .h files in /usr/include. Either the install didn't work, or it's been damaged or "cleaned out" since the install.
     
  19. electroshock macrumors 6502a

    electroshock

    Joined:
    Sep 7, 2009
    #19
    Agreed.

    OP, you'll want to reinstall Xcode, I think. Not sure what happened to your includes, but if that went missing, who knows what else also went missing. So a reinstallation would probably be the wisest thing to do at this point.
     
  20. Adudefelldown thread starter macrumors newbie

    Joined:
    Jul 24, 2009
    #20
    Ahh, alright. I will try that as soon as I have alil spare time. Thanks. Ill post again once I know if it fixed it or not. I'm crossing my fingers. :eek:
     
  21. Adudefelldown thread starter macrumors newbie

    Joined:
    Jul 24, 2009
    #21
    I have reinstalled xcode and everything is now working. Thanks everyone for all the help!
     

Share This Page