ld: library not found for -lcrt0.o on OSX 10.6 with gcc/clang -static flag

Viewed 37194

When I try to build the following program:

#include <stdio.h>

int main(void)
{
  printf("hello world\n");
  return 0;
}

On OS X 10.6.4, with the following flags:

gcc -static -o blah blah.c

It returns this:

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

Has anyone else encountered this, or is it something that noone else has been affected with yet? Any fixes?

Thanks

3 Answers

Per Nate's answer, a completely static application is apparently not possible - see also man ld:

-static Produces a mach-o file that does not use the dyld. Only used building the kernel.

The problem in linking with static libraries is that, if both a static and a dynamic version of a library are found in the same directory, the dynamic version will be taken in preference. Three ways of avoiding this are:

  1. Do not attempt to find them via the -L and -l options; instead, specify the full paths, to the libraries you want to use, on the compiler or linker command line.

    $ g++ -Wall -Werror -o hi /usr/local/lib/libboost_unit_test_framework.a hi.cpp

  2. Create a separate directory, containing symbolic links to the static libraries, use the -L option to have this directory searched first, and use the -l option to specify the libraries you want to use.

    $ g++ -Wall -Werror -L ./staticBoostLib -l boost_unit_test_framework -o hi hi.cpp

  3. Instead of creating a link of the same name in a different directory, create a link of a different name in the same directory, and specify that name in a -l argument.

    $ g++ -Wall -Werror -l boost_unit_test_framework_static -o hi hi.cpp

Related