Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

flyscorpio

macrumors newbie
Original poster
Nov 24, 2006
15
0
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

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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?
 

flyscorpio

macrumors newbie
Original poster
Nov 24, 2006
15
0
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:
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?
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
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

macrumors newbie
Original poster
Nov 24, 2006
15
0
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

macrumors 6502a
Jun 20, 2006
570
0
Denver, CO
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.