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

MacosGccLd

Macos Problem Overview


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

Macos Solutions


Solution 1 - Macos

This won’t work. From the man page for gcc:

> This option will not work on Mac OS X unless all libraries (including libgcc.a) have also been compiled with -static. Since neither a static version of libSystem.dylib nor crt0.o are provided, this option is not useful to most people.

Solution 2 - Macos

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

Solution 3 - Macos

You may also try LLVM LLD linker - I did prebuilt version for my two major OSes - https://github.com/VerKnowSys/Sofin-llds

This one allows me to link for exmple: "Qemu" properly - which is impossible with ld preinstalled by Apple.

And last one is - to build GCC yourself with libstdc++ (don't).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbrowneyeView Question on Stackoverflow
Solution 1 - MacosNateView Answer on Stackoverflow
Solution 2 - MacossdenhamView Answer on Stackoverflow
Solution 3 - MacosdmilithView Answer on Stackoverflow