Static linking with dylib ld: library not found for -lcrt0.o collect2: error: ld returned 1 exit status

一笑奈何 提交于 2019-12-12 02:39:35

问题


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

However, I get the following error.

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.


回答1:


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

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

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

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



来源:https://stackoverflow.com/questions/39278390/static-linking-with-dylib-ld-library-not-found-for-lcrt0-o-collect2-error-ld

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!