View Full Version : beginner's question about gcc, stdio.h
flyscorpio
Feb 12, 2009, 02:19 PM
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.
eddietr
Feb 12, 2009, 03:34 PM
It should be right there in /usr/include/stdio.h
try:
ls -al /usr/include/stdio.h
and see what it says.
Also try:
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:
#include <...> search starts here:
I assume you installed XCode?
flyscorpio
Feb 12, 2009, 08:08 PM
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.
It should be right there in /usr/include/stdio.h
try:
ls -al /usr/include/stdio.h
and see what it says.
Also try:
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:
#include <...> search starts here:
I assume you installed XCode?
eddietr
Feb 12, 2009, 08:36 PM
I installed Xcode.
The gcc locates at /developer/usr/bin/gcc
Hmm, that should also exist as /usr/bin/gcc
I tried ls -al /usr/include/stdio.h but didn't find it.
Sounds like something went wrong with your XCode install.
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'
...snip
Because stdio.h includes other things that are also expected to be in /usr/include
I really don't what is going on. BTW, if I edit the code in XCode and build it inside XCode then works fine.
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.
flyscorpio
Feb 13, 2009, 10:30 PM
Yes. You are right. I am gonna just reinstall the Xcode.
Thanks.
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.
LtRammstein
Feb 15, 2009, 06:22 PM
I'd reinstall as a last resort.
I'd try:
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.
./welcome
That should print: hello, world.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.