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

后端 未结 3 653
有刺的猬
有刺的猬 2020-11-29 03:03

When I try to build the following program:

#include 

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

On OS X 1

相关标签:
3条回答
  • 2020-11-29 03:52

    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.

    0 讨论(0)
  • 2020-11-29 04:00

    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

    0 讨论(0)
  • 2020-11-29 04:06

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

    0 讨论(0)
提交回复
热议问题