与dylib ld的静态链接:找不到-lcrt0.o的库collect2:错误:ld返回1退出状态 [英] Static linking with dylib ld: library not found for -lcrt0.o collect2: error: ld returned 1 exit status
问题描述
我正在用gnu49编译一个qt5 c ++项目,同时使用c ++ 11标志和通常的qt框架标志与Mac OSX El Captitan上的其他dylib(armadillo,boost libs等)链接.该项目可以很好地编译,但是为了使其在其他几台机器上具有更高的可移植性,我试图静态地链接一些动态库.
我在此处描述的静态链接库(例如-static -lboost_thread
)之前添加了-static标志.
I'm compiling a qt5 c++ project with gnu49 compile while linking with few other dylibs (armadillo,boost libs etc.) on Mac OSX El Captitan with c++11 flag and usual qt framework flags. The project compiles fine but to make it more portable on few other machines I'm trying to statically link few dynamic libraries.
I added -static flag before the (to be statically linked) library (e.g -static -lboost_thread
) as described here.
https://gcc.gnu.org/ml/gcc/2000-05/msg00517.html
但是,出现以下错误.
ld:-lcrt0.o找不到库.collect2:错误:ld返回1退出状态
ld: library not found for -lcrt0.o collect2: error: ld returned 1 exit status
我验证了该错误仅在尝试静态链接而不是动态链接时出现.
I verified that the error comes only while trying to link statically and not with dynamic linking.
推荐答案
GCC's -static
option, which you are applying, is non-positional. It enforces
static linkage of all libraries. Your linkage then fails because your system has no static version of libcrt0.o
您可能将GCC的static
选项与 ld
的-static
选项(同义词:-Bstatic
,-dn
-non_shared
),
是位置.它仅影响命令行上的后续库.这是逆的
链接器的-Bdynamic
选项(同义词:-dy
,-call_shared
).
You may be confusing GCC's static
option with ld
's -static
option (synonyms: -Bstatic
, -dn
-non_shared
),
which is positional. It affects only subsequent libraries on the commandline. It is the inverse of
the linker's -Bdynamic
option (synonyms: -dy
, -call_shared
).
因此,仅通过GCC静态链接库-lfoo
,-lbar
...,您可以将-Bstatic
传递给
链接器(在您提及它们之前)和-Bdynamic
在它们之后:
So to link only libraries -lfoo
, -lbar
... statically, via GCC, you can pass -Bstatic
through to the
linker just before you mention them and -Bdynamic
just after them:
-Wl,-Bstatic -lfoo -lbar -Wl,-Bdynamic
即使-lbar
是您的最后一个库,也不要省略最后一个-Wl,-Bdynamic
,因为GCC会悄悄地附加标准库
到您的链接(如您所见).
Do not omit the final -Wl,-Bdynamic
, even if -lbar
is the last of your libraries, because GCC quietly appends standard libraries
to your linkage (as you have found).
这篇关于与dylib ld的静态链接:找不到-lcrt0.o的库collect2:错误:ld返回1退出状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!