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

Adudefelldown

macrumors newbie
Original poster
Jul 24, 2009
12
0
I am trying to compile programs in terminal using the gcc command but everytime I do i get the error as follows:

test.c: In function ‘main’:
test.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
ld: library not found for -lcrt1.10.5.o
collect2: ld returned 1 exit status

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

ld: library not found for -lcrt1.10.5.o
collect2: ld returned 1 exit status


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!
 
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.
 
I'm using this command for the following code
gcc test.c
int main(void)
{
#include <stdio.h>
printf("TEST");

return 0;
}

and this command for the following code
gcc -framework Foundation project1 -o prog1

int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (@"Programming is fun!");

[pool drain];
return 0;
}
 
I added this to one of the codes and still got the same errors

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (@"Programming is fun!");

[pool drain];
return 0;
}
 
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

#include <stdio.h>

int main(void)
{
printf("TEST");

return 0;
}
 
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()
{
...
}
 
@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
 
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.
 
gcc -framework Foundation project1 -o prog1

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++.
 
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

gcc test.c
test.c:1:19: error: stdio.h: No such file or directory
test.c: In function ‘main’:
test.c:5: warning: incompatible implicit declaration of built-in function ‘printf’


and the objective-c code

gcc -framework Foundation project1.m -oprog1
project1.m: In function ‘main’:
project1.m:3: error: ‘NSAutoreleasePool’ undeclared (first use in this function)
project1.m:3: error: (Each undeclared identifier is reported only once
project1.m:3: error: for each function it appears in.)
project1.m:3: error: ‘pool’ undeclared (first use in this function)

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?
 
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
 
Heres the output from that command


gcc -v -h test.c
Using built-in specs.
Target: i686-apple-darwin9
Configured with: /var/tmp/gcc/gcc-5493~1/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=i686-apple-darwin9 --with-arch=apple --with-tune=generic --host=i686-apple-darwin9 --target=i686-apple-darwin9
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5493)
/Developer/usr/bin/../libexec/gcc/i686-apple-darwin9/4.0.1/cc1 -quiet -v -iprefix /Developer/usr/bin/../lib/gcc/i686-apple-darwin9/4.0.1/ -D__DYNAMIC__ test.c -fPIC -quiet -dumpbase test.c -mmacosx-version-min=10.6.0 -mtune=generic -march=apple -auxbase test -version -o /var/folders/US/USGF75QmHp8ahATTN6M45k+++TI/-Tmp-//ccQP80No.s
ignoring nonexistent directory "/Developer/usr/bin/../lib/gcc/i686-apple-darwin9/4.0.1/../../../../i686-apple-darwin9/include"
ignoring nonexistent directory "/usr/lib/gcc/i686-apple-darwin9/4.0.1/../../../../i686-apple-darwin9/include"
#include "..." search starts here:
#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.
GNU C version 4.0.1 (Apple Inc. build 5493) (i686-apple-darwin9)
compiled by GNU C version 4.0.1 (Apple Inc. build 5493).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 741cb18ccfc306e1ecc6fabe7cd14407
test.c:1:19: error: stdio.h: No such file or directory
test.c: In function ‘main’:
test.c:5: warning: incompatible implicit declaration of built-in function ‘printf’
 
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.
 
ls -la /usr/include
total 8
drwxrwxr-x 4 root wheel 136 Sep 21 18:16 .
drwxr-xr-x@ 12 root wheel 408 Sep 20 12:41 ..
drwxr-xr-x 3 root wheel 102 Sep 25 2008 gcc
lrwxr-xr-x 1 root wheel 110 Sep 20 12:41 parallels-server -> ../../Library/Parallels/Parallels Service.app/Contents/Frameworks/ParallelsVirtualizationSDK.framework/Headers

gcc -v -H test.c
Using built-in specs.
Target: i686-apple-darwin9
Configured with: /var/tmp/gcc/gcc-5493~1/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=i686-apple-darwin9 --with-arch=apple --with-tune=generic --host=i686-apple-darwin9 --target=i686-apple-darwin9
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5493)
/Developer/usr/bin/../libexec/gcc/i686-apple-darwin9/4.0.1/cc1 -quiet -v -iprefix /Developer/usr/bin/../lib/gcc/i686-apple-darwin9/4.0.1/ -H -D__DYNAMIC__ test.c -fPIC -quiet -dumpbase test.c -mmacosx-version-min=10.6.0 -mtune=generic -march=apple -auxbase test -version -o /var/folders/US/USGF75QmHp8ahATTN6M45k+++TI/-Tmp-//cckJZlie.s
ignoring nonexistent directory "/Developer/usr/bin/../lib/gcc/i686-apple-darwin9/4.0.1/../../../../i686-apple-darwin9/include"
ignoring nonexistent directory "/usr/lib/gcc/i686-apple-darwin9/4.0.1/../../../../i686-apple-darwin9/include"
#include "..." search starts here:
#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.
GNU C version 4.0.1 (Apple Inc. build 5493) (i686-apple-darwin9)
compiled by GNU C version 4.0.1 (Apple Inc. build 5493).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 741cb18ccfc306e1ecc6fabe7cd14407
test.c:1:19: error: stdio.h: No such file or directory
test.c: In function ‘main’:
test.c:5: warning: incompatible implicit declaration of built-in function ‘printf’


i just got rid of that /usr/local/include and it still gets the same error
 
ls -la /usr/include
total 8
drwxrwxr-x 4 root wheel 136 Sep 21 18:16 .
drwxr-xr-x@ 12 root wheel 408 Sep 20 12:41 ..
drwxr-xr-x 3 root wheel 102 Sep 25 2008 gcc
lrwxr-xr-x 1 root wheel 110 Sep 20 12:41 parallels-server -> ../../Library/Parallels/Parallels Service.app/Contents/Frameworks/ParallelsVirtualizationSDK.framework/Headers

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.
 
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.

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.
 
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:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.