问题
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