问题描述
我有一个使用libCrypto ++的cmake c ++项目。我在托管了FindCryptoPP.cmake模块。重要部分包括:
I have a cmake c++ project that uses libCrypto++. I have FindCryptoPP.cmake module hosted here. Important parts are:
find_library(CryptoPP_LIBRARY
NAMES cryptopp
DOC "CryptoPP library"
NO_PACKAGE_ROOT_PATH
PATHS "/usr/lib/x86_64-linux-gnu/"
)
...
add_library(CryptoPP::CryptoPP UNKNOWN IMPORTED)
set_target_properties(CryptoPP::CryptoPP PROPERTIES
IMPORTED_LOCATION "${CryptoPP_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${CryptoPP_INCLUDE_DIR}")
这很好用,找到静态库文件(* .a)。现在,我想创建单独的目标CryptoPP :: CryptoPP-static和CryptoPP :: CryptoPP-shared。
已安装必需的文件(默认为ubuntu安装):
And this works fine, finds static library file (*.a). Now I would like to create separate targets CryptoPP::CryptoPP-static and CryptoPP::CryptoPP-shared.Necessary files are installed (default ubuntu install):
- /usr/lib/x86_64-linux-gnu/libcryptopp.a
- /usr/lib/x86_64-linux-gnu/libcryptopp.so
我想要知道如何告诉find_library搜索静态版本或共享版本(最好以可移植的方式-我需要所有Linux,Windows,MacOS)并指定创建的目标类型。
I want to know how to tell find_library to search either static or shared version (preferably in portable way - I need all of Linux, Windows, MacOS) and specify the type created target.
推荐答案
实际上CMake的默认设置是先搜索共享库,然后搜索静态库。
Actually CMake's default is to search first for shared libraries and then for static libraries.
关键是值的顺序作为CMake的命令执行以下操作:
The key is the order of values in the CMAKE_FIND_LIBRARY_SUFFIXES
global variable, which is e.g. set in CMakeGenericSystem.cmake
as part of CMake's compiler/platform detection of the project()
command to:
set(CMAKE_FIND_LIBRARY_SUFFIXES ".so" ".a")
对于解决方案,请看看现有的查找模块,例如:
For a solution take a look at an existing find module like FindBoost.cmake
:
# Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
if( Boost_USE_STATIC_LIBS )
set( _boost_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
if(WIN32)
list(INSERT CMAKE_FIND_LIBRARY_SUFFIXES 0 .lib .a)
else()
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
endif()
endif()
这里 CMAKE_FIND_LIBRARY_SUFFIXES
变量已临时更改为调用。
Here the CMAKE_FIND_LIBRARY_SUFFIXES
variable is temporarily changed for the find_library()
calls.
此处同样适用。只需注意 find_library()
确实会缓存其结果,如果您想进行两次相同的搜索。
Same should be applicable here. Just be aware the find_library()
does cache its results if you want to do the same search twice.
- Default values for CMAKE_FIND_LIBRARY_PREFIXES/CMAKE_FIND_LIBRARY_SUFFIXES
- CMake find_library matching behavior?
- 'find_library' returns the same value in the loop in CMake
这篇关于CMake查找模块以区分共享库还是静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!