macos 如何指示 CMake 查找 MacPorts 安装的库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1487752/
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
How do I instruct CMake to look for libraries installed by MacPorts?
提问by David Holm
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.
我正在尝试构建我们的一些软件,这些软件旨在仅在 Linux 和 MacOS X 上运行。我们正在使用 CMake 并安装了 MacPorts,因此我可以轻松地获得 CMake 以及我们依赖的一些第三方库.
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.
现在的问题是,默认情况下,CMake 似乎不会从 MacPorts 中查找库,因此我们的几个目标被禁用,因为它无法找到所有在/opt/local 中的依赖项。
How can I instruct CMake to also look for includes and libraries from MacPorts?
如何指示 CMake 也从 MacPorts 中查找包含和库?
采纳答案by David Holm
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”添加了一个工具链文件,它添加了必要的包含和库路径。我希望有一些更自动化的东西,但至少它解决了问题。
darwin.cmake:
达尔文.cmake:
SET(CMAKE_SYSTEM_NAME Darwin)
# Add MacPorts
INCLUDE_DIRECTORIES(/opt/local/include)
LINK_DIRECTORIES(/opt/local/lib)
回答by Christopher Bruns
Add /opt/local/lib, and any other likely install paths, to the set of paths searched by cmake in your CMakeLists.txt file:
将 /opt/local/lib 和任何其他可能的安装路径添加到 cmake 在 CMakeLists.txt 文件中搜索的路径集:
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} /opt/local/lib)
This appends /opt/local/lib to the set of paths in which cmake searches for libraries. This CMAKE_LIBRARY_PATHtechnique will affect all find_library
commands after you set the variable.
这会将 /opt/local/lib 附加到 cmake 搜索库的路径集。设置变量后,此CMAKE_LIBRARY_PATH技术将影响所有find_library
命令。
For a more surgical, library-by-library approach, modify the individual find_library commands:
对于更外科手术式的逐个库方法,请修改各个 find_library 命令:
find_library(Foo foo
PATHS /opt/local/lib)
Note that this does nothardcode /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 the find_library documentationfor more variations on this theme.
请注意,这不会将/opt/local/lib 硬编码为查找库的唯一位置。相反,它只是将 /opt/local/lib 附加到要搜索库的位置集。我经常最终添加许多这样的路径,涵盖我所知道的所有机器上观察到的位置。有关此主题的更多变体,请参阅find_library 文档。
You might also wish to change CMAKE_INCLUDE_PATH, which affects the behavior of find_file()
and find_path()
commands.
您可能还希望更改CMAKE_INCLUDE_PATH,这会影响find_file()
和find_path()
命令的行为。
回答by Hank Gay
CMake needs to respect the DYLD_LIBRARY_PATH
environment variable, which is the equivalent of the LD_LIBRARY_PATH
environment variable on Linux. Your DYLD_LIBRARY_PATH
needs to have the proper path to find libraries installed by MacPorts.
CMake需要尊重DYLD_LIBRARY_PATH
环境变量,相当于LD_LIBRARY_PATH
Linux上的环境变量。您DYLD_LIBRARY_PATH
需要有正确的路径来查找 MacPorts 安装的库。
回答by tresf
Per @Nerdling's "Do NOT hardcode"comment on the accepted solution, here's a proposal to detect the MacPorts prefix path.
根据@Nerdling对已接受解决方案的“不要硬编码”的评论,这里有一个检测 MacPorts 前缀路径的建议。
MyModule.cmake
MyModule.cmake
# Detect if the "port" command is valid on this system; if so, return full path
EXECUTE_PROCESS(COMMAND which port RESULT_VARIABLE DETECT_MACPORTS OUTPUT_VARIABLE MACPORTS_PREFIX ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
IF (${DETECT_MACPORTS} EQUAL 0)
# "/opt/local/bin/port" doesn't have libs, so we get the parent directory
GET_FILENAME_COMPONENT(MACPORTS_PREFIX ${MACPORTS_PREFIX} DIRECTORY)
# "/opt/local/bin" doesn't have libs, so we get the parent directory
GET_FILENAME_COMPONENT(MACPORTS_PREFIX ${MACPORTS_PREFIX} DIRECTORY)
# "/opt/local" is where MacPorts lives, add `/lib` suffix and link
LINK_DIRECTORIES(${LINK DIRECTORIES} ${MACPORTS_PREFIX}/lib)
MESSAGE("WINNING!: ${MACPORTS_PREFIX}/lib")
ENDIF()
# Recommendation, also add a "brew --prefix" custom command to detect a homebrew build environment
回答by user7610
Install cmake
and pkgconfig
with MacPorts.
安装cmake
并pkgconfig
使用 MacPorts。
port install cmake pkgconfig
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.
使用 pkgconfig 查找库的 CMake 构建文件将使用 MacPorts 安装的 pkgconfig,它当然会有 MacPorts 安装的库的正确搜索路径。
That assumes CMake build files use the FindPkgConfigmodule. For example, I have a FindLibuv.cmake
module in a project, which begins like this.
假设 CMake 构建文件使用FindPkgConfig模块。例如,我FindLibuv.cmake
在一个项目中有一个模块,它是这样开始的。
find_package (PkgConfig)
pkg_check_modules (PC_Libuv QUIET libuv)