如何指示 CMake 查找 MacPorts 安装的库?
我正在尝试构建一些我们的软件,这些软件被设计为仅在 Linux 和 MacOS X 上运行。我们正在使用 CMake,并且我安装了 MacPorts,因此我可以轻松获取 CMake 以及我们依赖的一些第三方库。
现在的问题是,CMake 默认情况下似乎不会从 MacPorts 查找库,因此我们的几个目标被禁用,因为它无法找到全部位于 /opt/local 中的依赖项。
我如何指示 CMake 也从 MacPorts 查找包含文件和库?
I'm trying to build some of our software, which was designed to run solely on Linux, on MacOS X. We are using CMake and I installed MacPorts so I could easily get CMake along with some of the third party libraries that we depend on.
Now the problem is that CMake doesn't appear to look for libraries from MacPorts by default so several of our targets are disabled as it fails to find the dependencies which are all in /opt/local.
How can I instruct CMake to also look for includes and libraries from MacPorts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将
/opt/local/lib
和任何其他可能的安装路径添加到 cmake 在CMakeLists.txt
文件中搜索的路径集:这将附加 /opt/local /lib 到 cmake 搜索库的路径集。此
CMAKE_LIBRARY_PATH
技术将影响所有 <设置变量后,执行 code>find_library 命令。对于更精确的逐个库的方法,请修改各个 find_library 命令:
请注意,这不会将
/opt/local/lib
硬编码为唯一的查找位置对于图书馆。相反,它只是将/opt/local/lib
附加到搜索库的位置集。我经常最终添加许多这样的路径,涵盖在我所知道的所有机器上观察到的位置。有关更多变体,请参阅find_library
文档关于这个主题。您可能还希望更改
CMAKE_INCLUDE_PATH
,它会影响find_file()
和find_path()
命令的行为。Add
/opt/local/lib
, and any other likely install paths, to the set of paths searched by cmake in yourCMakeLists.txt
file:This appends /opt/local/lib to the set of paths in which cmake searches for libraries. This
CMAKE_LIBRARY_PATH
technique will affect allfind_library
commands after you set the variable.For a more surgical, library-by-library approach, modify the individual find_library commands:
Note that this does not hardcode
/opt/local/lib
as the only place to look for the library. Rather, it merely appends/opt/local/lib
to the set of locations in which to search for the library. I often end up adding many such paths, covering the locations observed on all of the machines I know about. See thefind_library
documentation for more variations on this theme.You might also wish to change
CMAKE_INCLUDE_PATH
, which affects the behavior offind_file()
andfind_path()
commands.我为“Darwin”添加了一个工具链文件,它添加了必要的包含路径和库路径。我希望有一些更自动化的东西,但至少它解决了问题。
darwin.cmake:
I added a toolchain file for "Darwin" which adds the necessary include and library paths. I was hoping for something a little more automatic but at least it solves the problem.
darwin.cmake:
CMake 需要遵守 DYLD_LIBRARY_PATH 环境变量,它相当于 Linux 上的 LD_LIBRARY_PATH 环境变量。您的
DYLD_LIBRARY_PATH
需要有正确的路径来查找 MacPorts 安装的库。CMake needs to respect the
DYLD_LIBRARY_PATH
environment variable, which is the equivalent of theLD_LIBRARY_PATH
environment variable on Linux. YourDYLD_LIBRARY_PATH
needs to have the proper path to find libraries installed by MacPorts.根据 @Nerdling 对已接受解决方案的“不要硬编码” 评论,这里有一个检测 MacPorts 前缀路径的建议。
MyModule.cmake
Per @Nerdling's "Do NOT hardcode" comment on the accepted solution, here's a proposal to detect the MacPorts prefix path.
MyModule.cmake
使用 MacPorts 安装
cmake
和pkgconfig
。使用 pkgconfig 查找库的 CMake 构建文件将使用 MacPorts 安装的 pkgconfig,并且它当然会具有 MacPorts 安装的库的正确搜索路径。
假设 CMake 构建文件使用 FindPkgConfig 模块。例如,我的项目中有一个 FindLibuv.cmake 模块,它是这样开始的。
Install
cmake
andpkgconfig
with MacPorts.CMake build files that use pkgconfig to find libs will then use the pkgconfig installed by MacPorts, and it will of course have correct search paths for libraries installed by MacPorts.
That assumes CMake build files use the FindPkgConfig module. For example, I have a
FindLibuv.cmake
module in a project, which begins like this.