user2493964
user2493964

Reputation: 25

with cmake how to link some libs statically and others dynamically?

How to tell CMake to link statically some libs and dynamically others ?

I want to compile a C++ exe statically linked to all its dependencies libs except glic

Thanks

Upvotes: 1

Views: 1216

Answers (1)

Guillaume
Guillaume

Reputation: 10961

The CMake approach to libraries is to first find them with find_library then use the result in target_link_libraries.

The choice of using a static or dynamic library is made during the find_library call:

  • if you don't mind which version is used, just call find_library(MYLIB mylib)

  • if you want a static library, use find_library(MYLIB libmylib.a) (that's for linux, you will search for a .lib on windows, etc.)

  • if you want a dynamic library, use find_library(MYLIB libmylib.so)

Then test if you library is found with if (MYLIB) and link it to your target with: target_link_libraries(mytarget ${MYLIB})

Upvotes: 1

Related Questions