如何指示 CMake 查找 MacPorts 安装的库?

发布于 2024-08-06 05:06:52 字数 251 浏览 3 评论 0原文

我正在尝试构建一些我们的软件,这些软件被设计为仅在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

逆流 2024-08-13 05:06:52

/opt/local/lib 和任何其他可能的安装路径添加到 cmake 在 CMakeLists.txt 文件中搜索的路径集:

set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} /opt/local/lib)

这将附加 /opt/local /lib 到 cmake 搜索库的路径集。此 CMAKE_LIBRARY_PATH 技术将影响所有 <设置变量后,执行 code>find_library 命令。

对于更精确的逐个库的方法,请修改各个 find_library 命令:

find_library(Foo foo
    PATHS /opt/local/lib)

请注意,这不会将 /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 your CMakeLists.txt file:

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_PATH technique will affect all find_library commands after you set the variable.

For a more surgical, library-by-library approach, modify the individual find_library commands:

find_library(Foo foo
    PATHS /opt/local/lib)

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 the find_library documentation for more variations on this theme.

You might also wish to change CMAKE_INCLUDE_PATH, which affects the behavior of find_file() and find_path() commands.

半世晨晓 2024-08-13 05:06:52

我为“Darwin”添加了一个工具链文件,它添加了必要的包含路径和库路径。我希望有一些更自动化的东西,但至少它解决了问题。

darwin.cmake

SET(CMAKE_SYSTEM_NAME Darwin)

# Add MacPorts
INCLUDE_DIRECTORIES(/opt/local/include)
LINK_DIRECTORIES(/opt/local/lib)

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:

SET(CMAKE_SYSTEM_NAME Darwin)

# Add MacPorts
INCLUDE_DIRECTORIES(/opt/local/include)
LINK_DIRECTORIES(/opt/local/lib)
邮友 2024-08-13 05:06:52

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 the LD_LIBRARY_PATH environment variable on Linux. Your DYLD_LIBRARY_PATH needs to have the proper path to find libraries installed by MacPorts.

寂寞花火° 2024-08-13 05:06:52

根据 @Nerdling 对已接受解决方案的“不要硬编码” 评论,这里有一个检测 MacPorts 前缀路径的建议。

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

Per @Nerdling's "Do NOT hardcode" comment on the accepted solution, here's a proposal to detect the MacPorts prefix path.

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
旧话新听 2024-08-13 05:06:52

使用 MacPorts 安装 cmakepkgconfig

port install cmake pkgconfig

使用 pkgconfig 查找库的 CMake 构建文件将使用 MacPorts 安装的 pkgconfig,并且它当然会具有 MacPorts 安装的库的正确搜索路径。

假设 CMake 构建文件使用 FindPkgConfig 模块。例如,我的项目中有一个 FindLibuv.cmake 模块,它是这样开始的。

find_package (PkgConfig)
pkg_check_modules (PC_Libuv QUIET libuv)

Install cmake and pkgconfig with 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.

That assumes CMake build files use the FindPkgConfig module. For example, I have a FindLibuv.cmake module in a project, which begins like this.

find_package (PkgConfig)
pkg_check_modules (PC_Libuv QUIET libuv)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文