Platform library name expansion using target_link_

2019-07-01 10:41发布

问题:

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:

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.



标签: cmake