macos ld:在带有 gcc/clang -static 标志的 OSX 10.6 上找不到 -lcrt0.o 的库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3801011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:14:12  来源:igfitidea点击:

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

macosgccld

提问by browneye

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:

在 OS X 10.6.4 上,具有以下标志:

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

谢谢

回答by Nate

This won't work. From the manpage for gcc:

这行不通。从man页面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.

此选项在 Mac OS X 上不起作用,除非所有库(包括 libgcc.a)也已使用 -static 编译。由于既没有提供静态版本的 libSystem.dylib 也没有提供 crt0.o,这个选项对大多数人来说没有用处。

回答by dmilith

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

您也可以尝试 LLVM LLD 链接器 - 我为我的两个主要操作系统做了预构建版本 - https://github.com/VerKnowSys/Sofin-llds

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

这个允许我链接例如:“Qemu”正确 - 这对于Apple预装的ld是不可能的。

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

最后一个是 - 用 libstdc++ 自己构建 GCC(不要)。

回答by sdenham

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

根据 Nate 的回答,完全静态的应用程序显然是不可能的 - 另请参阅man ld

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

-static生成不使用 dyld 的 mach-o 文件。仅用于构建内核。

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

  1. 不要试图通过 -L 和 -l 选项找到它们;相反,在编译器或链接器命令行上指定要使用的库的完整路径。

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

  2. 创建一个单独的目录,包含指向静态库的符号链接,使用 -L 选项首先搜索此目录,然后使用 -l 选项指定要使用的库。

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

  3. 不是在不同目录中创建同名链接,而是在同一目录中创建不同名称的链接,并在 -l 参数中指定该名称。

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