Cmake不能使用“link_directories”来找到库 [英] Cmake cannot find library using "link_directories"

查看:5671
本文介绍了Cmake不能使用“link_directories”来找到库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu,我正在学习cmake和make,只是试着一个简单的例子。我有两个目录: src build 。在 src 中,我有两个文件: main.cpp CMakeLists.txt ,其中只有以下文本:

I Ubuntu, I am learning about cmake and make, and just trying a simple example. I have two directories: src and build. In src, I have two files: main.cpp, and CMakeLists.txt, which has (only) the following text:

add_executable(test main.cpp)
link_directories(/usr/lib/x86_64-linux-gnu)
target_link_libraries(test protobuf)

/ usr / lib / x86_64-linux-gnu 中,有一个共享库 libprotobuf.so 我想链接。我的 main.cpp 使用这个库中的函数,通过包括相关头文件 #include< google / protobuf / message.h> code>。

In /usr/lib/x86_64-linux-gnu, there is a shared library called libprotobuf.so, which I want to link against. My main.cpp uses functions in this library, by including the releveant header file, #include <google/protobuf/message.h>.

现在,在 build 目录中,运行 ../ src ,然后 make 。然而,我然后得到链接器错误告诉我有一些未知的引用protobuf库中的一些函数。如果我在 build 中搜索所有文件和子目录,没有提及任何与protobuf相关的内容。

Now, in my build directory, I run cmake ../src, and then make. However, I then get linker errors telling me that there are undefined references to some of the functions in the protobuf library. If I do a search through all the files and subdirectories in build, there is not mention of anything related to protobuf.

但是,如果我删除 CMakeLists.txt 文件中的 link_directories 指定可执行文件的库,即 target_link_libraries(test /usr/lib/x86_64-linux-gnu/libprotobuf.so),它编译和链接正常。

However, if I remove the link_directories line in my CMakeLists.txt file, and instead write the full path to the library when specifying the executable, i.e. target_link_libraries(test /usr/lib/x86_64-linux-gnu/libprotobuf.so), it compiles and links fine.

为什么 link_directories 不允许cmake找到此库?

Why is link_directories not allowing cmake to find this library?

推荐答案

不要在CMake中使用 link_directories

Do not use link_directories like this in CMake.

这是一个常见的初学者错误,因为许多其他构建环境都是这样工作的,但在CMake中它只是要求麻烦。即使联机帮助页特别建议:

This is a common beginner's mistake, as many other build environments work like this, but in CMake it's just asking for trouble. Even the manpage specifically advises against it:


注意,这个命令[ link_directories ]很少需要。库位置返回
find_package() find_library()是绝对路径。将这些
绝对库文件路径直接传递给 target_link_libraries()
命令。 CMake将确保链接器找到它们。

Note that this command [link_directories] is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command. CMake will ensure the linker finds them.

因此,始终将绝对路径传递到 target_link_libraries 并使用 find_library 来解析链接目录:

So instead, always pass absolute paths to target_link_libraries and use find_library to resolve the link directory:

find_library(PROTOBUF_LIBRARY protobuf HINTS /usr/lib/x86_64-linux-gnu)
target_link_libraries(test PUBLIC ${PROTOBUF_LIBRARY})

如果无法找到预期的库,而不是编译时的随机链接器错误,您可能会在CMake配置时间获得诊断的巨大好处。此外,如果目标机器具有非标准目录布局,则允许用户通过GUI指定库位置。

This has the huge benefit that you will probably get a diagnostic at CMake configure time if the expected library cannot be found, instead of a random linker error at compile time. Also, this allows the user to specify a library location via the GUI if the target machine has a non-standard directory layout.

因此,如果它不立即工作,请务必检查 find_library 调用的结果,并查阅联机帮助页,以了解为什么找不到您的库。

So if it doesn't work right away, be sure to check the result of the find_library call and consult the manpage to track down why it doesn't find your library as intended.

这篇关于Cmake不能使用“link_directories”来找到库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆