3
votes

Using target_link_libraries in CMake with just the library name, e.g.

target_link_library( myProject SomeLibrary )

will expand SomeLibrary to SomeLibrary.lib, libSomeLibrary.so, etc. depending on platform. However, if a full path is specified then the library name is not expanded based on platform, e.g.

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

How can I get the library name to be expanded based on platform? Currently, I'm detecting platform in the script and adjusting the library names myself which feels a little ugly.

(Background: over on this question I'm advised to use absolute paths when specifying libraries rather than using link_directories)

1

1 Answers

2
votes

Use find_library.

Instead of hardcoding the full path, you should only give the name of the library and a list of (possibly configurable) locations where it might be found and have find_library do the rest. If successful, the result of the find_library call can be fed right into target_link_libraries.