C++ cmake - find_library - 自定义库位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12075371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cmake - find_library - custom library location
提问by Daniel
I'm currently trying to get CMake running for my project (on windows). I want to use a custom location where all libraries are installed. To inform CMake about that path I tried to do that:
我目前正在尝试让 CMake 为我的项目运行(在 Windows 上)。我想使用安装所有库的自定义位置。为了告知 CMake 这条路径,我尝试这样做:
set(CMAKE_PREFIX_PATH D:/develop/cmake/libs)
But when I try to find the library with
但是当我试图找到图书馆时
find_library(CURL_LIBRARY NAMES curl curllib libcurl_imp curllib_static)
CMake can't find it. When I set my prefix path to
CMake 找不到它。当我将前缀路径设置为
set(CMAKE_PREFIX_PATH D:/develop/cmake/libs/curl)
... the library is located.
...图书馆位于。
So my question is: How can I configure CMake properly to work with a directory structore at a custom location which looks like that:
所以我的问题是:如何正确配置 CMake 以在自定义位置使用目录结构,如下所示:
D:/develop/cmake/libs/
-> libA
-> include
-> lib
-> libB
-> include
-> lib
-> ...
-> include
-> lib
In "include" lie the public headers and in "lib" are the compiled libraries.
在“include”中是公共头文件,在“lib”中是编译后的库。
Hope someone can help me - Thanks in advance
希望有人能帮助我 - 提前致谢
edit: The current workaround for me is, to do this before i search for libraries:
编辑:我当前的解决方法是,在搜索库之前执行此操作:
set(CUSTOM_LIBRARY_PATH D:/develop/cmake/libs)
file(GLOB sub-dir ${CUSTOM_LIBRARY_PATH}/*)
foreach(dir ${sub-dir})
if(IS_DIRECTORY ${dir})
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH};${dir})
endif()
endforeach()
But that way the default module for boost wont find it until it because the directory structore of boost is a bit different.
但是那样boost的默认模块直到它才会找到它,因为boost的目录结构有点不同。
boost -> include -> boost-1_50 -> *.hpp
When I move the content if "boost-1_50" to "include" the library can be found but that way it's not possible to handle multiple versions right?
当我移动内容时,如果“boost-1_50”到“include”可以找到库,但是这样就不可能处理多个版本,对吗?
采纳答案by Daniel
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 将(至少在 Windows 上)在
CMAKE_PREFIX_PATH/<<package_name>>-<<version>>/<<package_name>>-config.cmake
(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
In that configuration you can set variables. My config file for boost looks like that:
在该配置中,您可以设置变量。我的 boost 配置文件如下所示:
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.
希望这对其他人有帮助。
回答by Silas Parker
The simplest solution may be to add HINTS
to each find_*
request.
最简单的解决方案可能是添加HINTS
到每个find_*
请求中。
For example:
例如:
find_library(CURL_LIBRARY
NAMES curl curllib libcurl_imp curllib_static
HINTS "${CMAKE_PREFIX_PATH}/curl/lib"
)
For Boost I would strongly recommend using the FindBooststandard module and setting the BOOST_DIR
variable to point to your Boost libraries.
对于 Boost,我强烈建议使用FindBoost标准模块并将BOOST_DIR
变量设置为指向您的 Boost 库。
回答by Pierluigi
Use CMAKE_PREFIX_PATH by adding multiple paths (separated by semicolons and no white spaces). You can set it as an environmental variable to avoid having absolute paths in your cmake configuration files
通过添加多个路径(以分号分隔且无空格)来使用 CMAKE_PREFIX_PATH 。您可以将其设置为环境变量,以避免在您的 cmake 配置文件中出现绝对路径
Notice that cmake will look for config file in any of the following folderswhere is any of the path in CMAKE_PREFIX_PATH and name is the name of the library you are looking for
请注意,cmake 将在以下任何文件夹中查找配置文件,其中 CMAKE_PREFIX_PATH 中的任何路径和 name 是您要查找的库的名称
<prefix>/ (W)
<prefix>/(cmake|CMake)/ (W)
<prefix>/<name>*/ (W)
<prefix>/<name>*/(cmake|CMake)/ (W)
<prefix>/(lib/<arch>|lib|share)/cmake/<name>*/ (U)
<prefix>/(lib/<arch>|lib|share)/<name>*/ (U)
<prefix>/(lib/<arch>|lib|share)/<name>*/(cmake|CMake)/ (U)
In your case you need to add to CMAKE_PREFIX_PATH the following two paths:
在您的情况下,您需要将以下两个路径添加到 CMAKE_PREFIX_PATH:
D:/develop/cmake/libs/libA;D:/develop/cmake/libB
回答by arrowd
There is no way to automatically set CMAKE_PREFIX_PATH
in a way you want. I see following ways to solve this problem:
没有办法以CMAKE_PREFIX_PATH
您想要的方式自动设置。我看到以下方法可以解决这个问题:
Put all libraries files in the same dir. That is,
include/
would contain headers for all libs,lib/
- binaries, etc. FYI, this is common layout for most UNIX-like systems.Set global environment variable
CMAKE_PREFIX_PATH
toD:/develop/cmake/libs/libA;D:/develop/cmake/libs/libB;...
. When you run CMake, it would aautomatically pick up this env var and populate it's ownCMAKE_PREFIX_PATH
.Write a wrapper .bat script, which would call
cmake
command with-D CMAKE_PREFIX_PATH=...
argument.
将所有库文件放在同一个目录中。也就是说,
include/
将包含所有库的头lib/
文件,- 二进制文件等。仅供参考,这是大多数类 UNIX 系统的常见布局。将全局环境变量设置
CMAKE_PREFIX_PATH
为D:/develop/cmake/libs/libA;D:/develop/cmake/libs/libB;...
. 当你运行 CMake 时,它会自动获取这个 env var 并填充它自己的CMAKE_PREFIX_PATH
.编写一个包装器 .bat 脚本,它将调用
cmake
带-D CMAKE_PREFIX_PATH=...
参数的命令。
回答by Sameer Asal
You have one extra level of nesting.
CMAKE will search under $CMAKE_PREFIX_PATH/include
for headers and $CMAKE_PREFIX_PATH/libs
for libraries.
您有一层额外的嵌套。CMAKE 将在下面搜索$CMAKE_PREFIX_PATH/include
标题和$CMAKE_PREFIX_PATH/libs
库。
From CMAKE documentation:
从 CMAKE文档:
For each path in the CMAKE_PREFIX_PATH list, CMake will check "PATH/include" and "PATH" when FIND_PATH() is called, "PATH/bin" and "PATH" when FIND_PROGRAM() is called, and "PATH/lib and "PATH" when FIND_LIBRARY() is called.
对于 CMAKE_PREFIX_PATH 列表中的每个路径,CMake 将在调用 FIND_PATH() 时检查“PATH/include”和“PATH”,在调用 FIND_PROGRAM() 时检查“PATH/bin”和“PATH”,以及“PATH/lib 和” PATH”,当调用 FIND_LIBRARY() 时。
回答by vivi
I've encountered a similar scenario. I solved it by adding in this following code just before find_library()
:
我遇到过类似的场景。我通过在之前添加以下代码来解决它find_library()
:
set(CMAKE_PREFIX_PATH /the/custom/path/to/your/lib/)
then it can find the library location.
然后它可以找到库位置。