How to link compiled object file (hello.o) with ld on Mac OS X?
gcc, ld, linker, macos, x86-64
Solution
Okay, I had this question before too. Yes, the reason for the linker errors is because you need to feed it all the magic arguments that gcc does. And the easy way to discover those is to invoke the `-v` option on `gcc` to reveal all the commands executed in the compilation stages. In your case, run:
gcc hello.o -o hello -v
...the output of which, on my system, ends with the line:
/usr/libexec/gcc/i686-apple-darwin9/4.2.1/collect2 -dynamic -arch i386 -macosx_version_min 10.5.8 -weak_reference_mismatches non-weak -o test -lcrt1.10.5.o -L/usr/lib/i686-apple-darwin9/4.2.1 -L/usr/lib/gcc/i686-apple-darwin9/4.2.1 -L/usr/lib/gcc/i686-apple-darwin9/4.2.1 -L/usr/lib/gcc/i686-apple-darwin9/4.2.1/../../../i686-apple-darwin9/4.2.1 -L/usr/lib/gcc/i686-apple-darwin9/4.2.1/../../.. test.o -lgcc_s.10.5 -lgcc -lSystem
I don't know what the `collect2` program is, but if you feed all those arguments to `ld` it should work just the same (at least it does on my system).
Problem
I got a problem with link objective files on a Mac OS X. Tracing back the problem is, here is my C hello world program ``` #include <stdio.h> int main(){ printf("Hello, world!\n"); return 0; } ``` //Compile with gcc (clang LLVM compiler on mac) ``` $ gcc -c hello.c ``` The output file is hello.o link with gcc and run the executable is ``` $ gcc hello.o -o hello $ ./hello ``` Now, I have to use the mac linker program ld or Ld to link the the objective files instead of gcc. In this case, what arguments should I pass into the ld program in order to get the program run? A simple pass in the object file name, i.e. $ ld hello.o resulting in ``` ld: warning: -macosx_version_min not specified, assuming 10.6 Undefined symbols for architecture x86_64: "_printf", referenced from: _main in hello.o "start", referenced from: implicit entry/start for main executable ld: symbol(s) not found for inferred architecture x86_64 ``` So what other files that i need to include to link or architecture information that I need to specify? Thanks.