OS X 下的 CMake 警告:未为以下目标指定 MACOSX_RPATH

CMake warnings under OS X: MACOSX_RPATH is not specified for the following targets

我尝试在 OS X (Yosemite) 下构建一个基于 CMake 的软件,它可以在 Fedora 21 下成功构建。它使用了一堆库。既有像 Boost 这样的大开放的,也有一些躺在 /installation_folder/lib 中的自写的。我使用 CMake 版本 3.3.0。

执行后

mkdir build
cd build
cmake .. -DCMAKE_C_COMPILER=/usr/local/Cellar/gcc/5.2.0/bin/gcc-5 -DCMAKE_CXX_COMPILER=/usr/local/Cellar/gcc/5.2.0/bin/g++-5  -DCMAKE_MODULE_PATH=${PWD}/../external/install/share/llvm/cmake 

我收到以下警告:

CMake Warning (dev):
Policy CMP0042 is not set: MACOSX_RPATH is enabled by default.  Run "cmake
--help-policy CMP0042" for policy details.  Use the cmake_policy command to
set the policy and suppress this warning.

MACOSX_RPATH is not specified for the following targets:

ClangWrapper
Structure
WCETXML

This warning is for project developers.  Use -Wno-dev to suppress it.

CMakeLists.txt 包含以下关于 RPATH 的行:

SET(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)

IF("${isSystemDir}" STREQUAL "-1")
  SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
ENDIF("${isSystemDir}" STREQUAL "-1")

我只能说 ${CMAKE_INSTALL_PREFIX}/lib 确实是正确的路径,并且正确找到了像 Boost 这样的其他库。

忽略警告并继续构建目录中的 "make" 会导致链接错误。

我阅读了 CMake Wiki RPATH handling article,但我仍然无法区分这些路径变量及其在 OS X 上的正确用法。

set(CMAKE_MACOSX_RPATH 1)添加到CMakeLists.txt中,在上面写的语句之前,让警告消失。执行 make 后的链接问题仍然存在。这让我假设我的 RPATH 设置与我的链接问题无关。

不过,这个帖子的问题已经解决了。仍然非常欢迎CMakeLists.txt中有关正确使用RPATH选项的解释!

好吧,我将从@fotinsky 的回答向前迈出一步。 (请随意将其纳入您的答案。)

对运行 cmake-policy --help-policy CMP0042 的警告建议的输出是:

CMake 2.8.12 and newer has support for using ``@rpath`` in a target's install
name.  This was enabled by setting the target property
``MACOSX_RPATH``.  The ``@rpath`` in an install name is a more
flexible and powerful mechanism than ``@executable_path`` or ``@loader_path``
for locating shared libraries.

CMake 3.0 and later prefer this property to be ON by default.  Projects
wanting ``@rpath`` in a target's install name may remove any setting of
the ``INSTALL_NAME_DIR`` and ``CMAKE_INSTALL_NAME_DIR``
variables.

This policy was introduced in CMake version 3.0.  CMake version
3.1.3 warns when the policy is not set and uses OLD behavior.  Use
the cmake_policy command to set it to OLD or NEW explicitly.

这只是意味着在以后的cmake版本中,用户需要明确启用或禁用CMAKE_MACOSX_RPATH

在此 CMake blog entry.

中还有更多关于引入此变量的背景信息

所述,如果您不需要针对旧版本的cmake,您可以简单地设置:

cmake_minimum_required (VERSION 3.0)

这消除了主要版本之间默认值的歧义,并在默认情况下简单地启用 runtime path behaviors