beginner's question about gcc, stdio.h

Discussion in 'Mac Programming' started by flyscorpio, Feb 12, 2009.

  1. macrumors newbie

    Joined:
    Nov 24, 2006
    #1
    Hi All,

    I am starting to learn c programming in mac os X Leopard. So I edited a file called Hello.c

    #include <stdio.h>

    int main()
    {
    printf ("Hello, world");
    return 0;
    }

    and saved it.

    Then I go to the terminal /developer/usr/bin/gcc hello.c.

    It came out that the stdio.h wasn't there.

    Could someone please tell me the reason why I can't compile the hello.c and why gcc can't find stdio.h.

    Thanks.
     
  2. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #2
    It should be right there in /usr/include/stdio.h

    try:
    Code:
    ls -al /usr/include/stdio.h
    
    and see what it says.

    Also try:

    Code:
    gcc -v hello.c
    

    and see what it says. One of the things is will tell you is what search path gcc is using for <> includes. Look for lines that start with:

    Code:
    #include <...> search starts here:
    
    I assume you installed XCode?
     
  3. thread starter macrumors newbie

    Joined:
    Nov 24, 2006
    #3
    I installed Xcode.

    The gcc locates at /developer/usr/bin/gcc

    I tried ls -al /usr/include/stdio.h but didn't find it.

    Then I tried " locate stdio.h" and displayed this:

    /Developer/SDKs/MacOSX10.4u.sdk/usr/include/stdio.h
    /Developer/SDKs/MacOSX10.4u.sdk/usr/include/xlocale/_stdio.h
    /Developer/SDKs/MacOSX10.5.sdk/usr/include/fcgi_stdio.h
    /Developer/SDKs/MacOSX10.5.sdk/usr/include/secure/_stdio.h
    /Developer/SDKs/MacOSX10.5.sdk/usr/include/stdio.h
    /Developer/SDKs/MacOSX10.5.sdk/usr/include/xlocale/_stdio.h

    Then I copied stdio.h from "/Developer/SDKs/MacOSX10.5.sdk/usr/include/stdio.h" to /usr/bin/include/stdio.h and compiled again but got this:

    /usr/include/stdio.h:70: error: syntax error before 'va_list'
    /usr/include/stdio.h:75: error: syntax error before 'off_t'
    /usr/include/stdio.h:80: error: syntax error before 'size_t'
    /usr/include/stdio.h:87: error: syntax error before 'fpos_t'
    /usr/include/stdio.h:145: error: syntax error before 'fpos_t'
    /usr/include/stdio.h:162: error: syntax error before '_offset'


    I really don't what is going on. BTW, if I edit the code in XCode and build it inside XCode then works fine.

     
  4. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #4
    Hmm, that should also exist as /usr/bin/gcc

    Sounds like something went wrong with your XCode install.


    Because stdio.h includes other things that are also expected to be in /usr/include

    Well, you can also make it work by using -I to include /Developer/SDKs/MacOSX10.5.sdk/usr/include/ and then gcc will find the <> headers there.

    But I would probably just reinstall XCode so you have everything in the standard places. Otherwise you'll be working around this a lot and I would think that would get annoying.
     
  5. thread starter macrumors newbie

    Joined:
    Nov 24, 2006
    #5
    Yes. You are right. I am gonna just reinstall the Xcode.
    Thanks.

     
  6. macrumors 6502a

    LtRammstein

    Joined:
    Jun 20, 2006
    Location:
    Denver, CO
    #6
    I'd reinstall as a last resort.

    I'd try:

    Code:
     cd <type the location to where the file is saved>
    gcc -o welcome hello.c
    OS X has a built-in gcc (4.0.1). That should compile it.

    Make sure you're in the directory that you compiled your file in.

    Code:
     ./welcome
    That should print: hello, world.
     

Share This Page