c++ - cmake find_library 和 CMAKE_FIND_ROOT_PATH

标签 c++ c cmake

在 cmake 的 find_library 函数的文档中,我们有

The CMake variable CMAKE_FIND_ROOT_PATH specifies one or more directories to be prepended to all other search directories. This effectively “re-roots” the entire search under given locations. Paths which are descendants of the CMAKE_STAGING_PREFIX are excluded from this re-rooting, because that variable is always a path on the host system. By default the CMAKE_FIND_ROOT_PATH is empty.

The CMAKE_SYSROOT variable can also be used to specify exactly one directory to use as a prefix. Setting CMAKE_SYSROOT also has other effects. See the documentation for that variable for more.

These variables are especially useful when cross-compiling to point to the root directory of the target environment and CMake will search there too. By default at first the directories listed in CMAKE_FIND_ROOT_PATH are searched, then the CMAKE_SYSROOT directory is searched, and then the non-rooted directories will be searched. The default behavior can be adjusted by setting CMAKE_FIND_ROOT_PATH_MODE_LIBRARY. This behavior can be manually overridden on a per-call basis. By using CMAKE_FIND_ROOT_PATH_BOTH the search order will be as described above. If NO_CMAKE_FIND_ROOT_PATH is used then CMAKE_FIND_ROOT_PATH will not be used. If ONLY_CMAKE_FIND_ROOT_PATH is used then only the re-rooted directories and directories below CMAKE_STAGING_PREFIX will be searched.

(参见 http://www.cmake.org/cmake/help/v3.0/command/find_library.html)

我不确定你是怎么读的,但对我来说,这似乎暗示 find_library 将使用 CMAKE_FIND_ROOT_PATH 来查找库。我编写了以下 cmakelists.txt:

cmake_minimum_required( VERSION 3.0 )

project( "cmakefindlibtest" )

message( "CMAKE_FIND_ROOT_PATH is ${CMAKE_FIND_ROOT_PATH}" )
list( APPEND CMAKE_FIND_ROOT_PATH "C:/DEV/lib/" )
message( "CMAKE_FIND_ROOT_PATH is now ${CMAKE_FIND_ROOT_PATH}" )
#find_library( punycode_library_test punycode PATHS "C:/DEV/lib" )
find_library( punycode_library_test punycode  )
message( "punycode_library_test is now ${punycode_library_test}" )

add_executable( cmakefindlibtest main.cpp )
target_link_libraries( cmakefindlibtest ${punycode_library_test} )

main.cpp 只是 Hello World 。在 C:\DEV\lib 中,我放置了一个名为 punycode.lib 的库(这是在 Windows 上)。我已将 CMAKE_FIND_ROOT_PATH 指向该目录。然后当我调用 find_library 时,我得到:

c:\DEV\cmakefindtest\_build>cmake ..
-- Building for: Visual Studio 11 2012
CMAKE_FIND_ROOT_PATH is
CMAKE_FIND_ROOT_PATH is now C:/DEV/lib/
punycode_library_test is now punycode_library_test-NOTFOUND
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
punycode_library_test
    linked by target "cmakefindlibtest" in directory C:/DEV/cmakefindtest

-- Configuring incomplete, errors occurred!
See also "C:/DEV/cmakefindtest/_build/CMakeFiles/CMakeOutput.log".

请注意,当我将目录添加到 find_library 调用的 PATHS 部分时

find_library( punycode_library_test punycode PATHS "C:/DEV/lib" )

一切正常:

c:\DEV\cmakefindtest\_build>cmake ..
-- Building for: Visual Studio 11 2012
CMAKE_FIND_ROOT_PATH is
CMAKE_FIND_ROOT_PATH is now C:/DEV/lib/
punycode_library_test is now C:/DEV/lib/punycode.lib
-- Configuring done
-- Generating done
-- Build files have been written to: C:/DEV/cmakefindtest/_build

那么 find_library 只是不使用 CMAKE_FIND_ROOT_PATH 吗?

更新:

根据 http://public.kitware.com/pipermail/cmake-developers/2012-January/002850.html,看起来 CMAKE_LIBRARY_PATH 主要做了我想要它在这里做的事情,看起来这就是我应该在这种情况下使用它的原因。

但是,我仍在尝试弄清楚为什么 CMAKE_FIND_ROOT_PATH 不适用于 find_libraryhttp://www.cmake.org/Wiki/CMake_Cross_Compiling 处的文档说到 CMAKE_FIND_ROOT_PATH

this is a list of directories, each of the directories listed there will be prepended to each of the search directories of every FIND_XXX() command.

在这种情况下这似乎不是真的,除非我错过了某种设置或其他变量。

最佳答案

首先,CMAKE_FIND_ROOT_PATH 将被添加到 docs of find_library 中描述的一组特定目录中。 .它不会添加到您为 find_library 指定的文件名之前。

您只需要使用CMAKE_FIND_ROOT_PATH 进行交叉编译。例如,虽然 find_library(.. jpeg ...) 会找到 /usr/lib/libjpeg.so,但您可以使用 set(CMAKE_FIND_ROOT_PATH ~/my_toolchain) 来查找 ~/my_toolchain/usr/lib/libjpeg.so

因为你没有提到你正在交叉编译你可能需要的是 CMAKE_PREFIX_PATH .尝试 set(CMAKE_PREFIX_PATH "c:/DEV")set(CMAKE_LIBRARY_PATH "c:/DEV/lib") 进行 find_library 搜索c:/DEV/lib.

关于c++ - cmake find_library 和 CMAKE_FIND_ROOT_PATH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24659753/

相关文章:

cmake - 如何在 Windows Prompt 中调用 cmake 并将 txt 文件内容作为选项传递?

c++ - 无法为 read_graphviz() 示例链接 Boost Graph Library

c++ - union 中未命名结构的同名成员是错误还是 GCC 错误?

c - 为什么指针起作用而不是这段代码中的普通变量?

boost - 在 Windows 上获取 CMake 以查找 vc120 Boost 库

c++ - 我的交叉编译器找不到共享库

c++ - 使用 boost::spirit::qi 解析带分隔符的数字

c++ - 使用 C++ 模数运算符的票据面额

c - 用 C midi 编程

c - 获取协议(protocol)中方法 block 的 NSMethodSignature