View Full Version : Help Compiling in terminal
Adudefelldown
Sep 21, 2009, 09:10 PM
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!
rowsdower
Sep 21, 2009, 09:19 PM
Can you post your code and the command you are using to compile?
lloyddean
Sep 21, 2009, 09:32 PM
It looks like you've forgotten to include "stdio"
ChrisA
Sep 21, 2009, 09:33 PM
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.
Adudefelldown
Sep 21, 2009, 09:35 PM
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;
}
Adudefelldown
Sep 21, 2009, 09:41 PM
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;
}
EX1127
Sep 21, 2009, 09:43 PM
isnt import stdio suppose to be before main not after?
Adudefelldown
Sep 21, 2009, 09:46 PM
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;
}
EX1127
Sep 21, 2009, 09:49 PM
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()
{
...
}
Adudefelldown
Sep 21, 2009, 09:52 PM
@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
mac2x
Sep 22, 2009, 12:01 AM
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.
chown33
Sep 22, 2009, 12:50 AM
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.
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++.
Adudefelldown
Sep 22, 2009, 01:11 AM
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?
chown33
Sep 22, 2009, 08:27 PM
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.
gcc -v -H test.c
Adudefelldown
Sep 23, 2009, 02:57 PM
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’
chown33
Sep 23, 2009, 05:27 PM
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:
#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:
ls -la /usr/include
If it's not a fairly long listing, then something is broken.
Adudefelldown
Sep 23, 2009, 11:25 PM
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
chown33
Sep 24, 2009, 01:09 AM
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.
electroshock
Sep 24, 2009, 07:20 AM
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.
Adudefelldown
Sep 24, 2009, 09:00 PM
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. :o
Adudefelldown
Sep 27, 2009, 07:00 PM
I have reinstalled xcode and everything is now working. Thanks everyone for all the help!
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.