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.
GCC's
-staticoption, 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 oflibcrt0.oYou may be confusing GCC's
staticoption withld's-staticoption (synonyms:-Bstatic,-dn-non_shared), which is positional. It affects only subsequent libraries on the commandline. It is the inverse of the linker's-Bdynamicoption (synonyms:-dy,-call_shared).So to link only libraries
-lfoo,-lbar... statically, via GCC, you can pass-Bstaticthrough to the linker just before you mention them and-Bdynamicjust after them:Do not omit the final
-Wl,-Bdynamic, even if-lbaris the last of your libraries, because GCC quietly appends standard libraries to your linkage (as you have found).