Extending a platform library name using target_link_libraries with an absolute path as the library path?

Using target_link_libraries in CMake with only the library name, for example

target_link_library( myProject SomeLibrary )

will extend SomeLibrary to SomeLibrary.lib, libSomeLibrary.so, etc. depending on the platform. However, if the full path is specified, the library name does not expand based on the platform, for example.

target_link_library( myProject ${myProject_SOURCE_DIR}/libs/SomeLibrary )

How to get the library name for the extension on the platform? I am currently discovering the platform in a script and setting up library names myself that seem a little ugly.

(Background: over on this question I recommend using absolute paths when specifying libraries, and not using link_directories)

+3
source share
1 answer

Use find_library.

Instead of hard coding the full path, you should specify only the name of the library and a list of (possibly customizable) locations where it can be found, and find_librarydo the rest. If successful, the result of the call find_librarycan be submitted directly to target_link_libraries.

+2
source

All Articles