g++ linker: force static linking if static library exists?
I've a program which links to many libraries. g++
, by default, prefers to link to shared libraries, even if the corresponding archive exists.
How can I change this preference to prefer static archives over dynamic libraries, if a static archive exists?
Note, I used -static
option, but it tries to find static archive for all libraries which is not what I want.
g++ -Wl,-Bstatic -lz -lfoo -Wl,-Bdynamic -lbar -Wl,--as-needed
Will link zlib
and libfoo
as static, and libbar
as dynamic . --as-needed
will drop any unused dynamic library.
When you only want to statically link one or two libraries with the rest, including system libraries, being dynamic, it is often easier to simply reference the static library by its full name. I.e. rather than use -l
and -L
to get g++ to resolve a library from what it finds, simpy add the full path to the library as an input. Taking the g++ command above, to link a main.o
application main program to static libz and libfoo and dynamic libbar and libglib etc. :
g++ main.o /usr/lib/libz.a /usr/lib/libfoo.a -lbar
Edit 3 Aug 17: I've just tripped across this answer which goes into more detail and offers an alternative way (-l:
) to specify the library directly.
ReferenceURL : https://stackoverflow.com/questions/3698321/g-linker-force-static-linking-if-static-library-exists
'code' 카테고리의 다른 글
자바 스크립트 메모리 제한 (0) | 2021.01.08 |
---|---|
Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface? (0) | 2021.01.08 |
부모 CMakeLists.txt에서 CMake의 기본 옵션 (…) 값 재정의 (0) | 2021.01.08 |
NLP 및 Elasticsearch를 사용한 의미 검색 (0) | 2021.01.08 |
테이블에 계산 된 열을 어떻게 추가합니까? (0) | 2021.01.08 |