且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

cmake - find_library - 自定义库位置

更新时间:2023-02-03 12:47:13

我看到有两个人把这个问题放到了他们的收藏夹中,所以我会尝试回答对我有用的解决方案:我没有使用查找模块,而是为所有已安装的库编写配置文件.这些文件非常简单,也可以用来设置非标准变量.CMake 将(至少在 Windows 上)在

I saw that two people put that question to their favorites so I will try to answer the solution which works for me: Instead of using find modules I'm writing configuration files for all libraries which are installed. Those files are extremly simple and can also be used to set non-standard variables. CMake will (at least on windows) search for those configuration files in

CMAKE_PREFIX_PATH/<<package_name>>-<<version>>/<<package_name>>-config.cmake

(可以通过环境变量设置).所以例如boost配置在路径中

(which can be set through an environment variable). So for example the boost configuration is in the path

CMAKE_PREFIX_PATH/boost-1_50/boost-config.cmake

在该配置中,您可以设置变量.我的 boost 配置文件如下所示:

In that configuration you can set variables. My config file for boost looks like that:

set(boost_INCLUDE_DIRS ${boost_DIR}/include)
set(boost_LIBRARY_DIR ${boost_DIR}/lib)
foreach(component ${boost_FIND_COMPONENTS}) 
    set(boost_LIBRARIES ${boost_LIBRARIES} debug ${boost_LIBRARY_DIR}/libboost_${component}-vc110-mt-gd-1_50.lib)
    set(boost_LIBRARIES ${boost_LIBRARIES} optimized ${boost_LIBRARY_DIR}/libboost_${component}-vc110-mt-1_50.lib)
endforeach()
add_definitions( -D_WIN32_WINNT=0x0501 )

非常简单+当您编写一些辅助函数时,可以进一步缩小配置文件的大小.我在这个设置中遇到的唯一问题是我没有找到一种方法来让配置文件优先于查找模块 - 所以你需要删除查找模块.

Pretty straight forward + it's possible to shrink the size of the config files even more when you write some helper functions. The only issue I have with this setup is that I havn't found a way to give config files a priority over find modules - so you need to remove the find modules.

希望这对其他人有帮助.

Hope this this is helpful for other people.