在OS X El Capitan上执行错误“不安全使用相对rpath”

时间:2016-02-13 08:22:11

标签: python c++ macos cmake swig

我一直在开发两个由CMake构建的C ++库。其中一个库(ProjectB)有一个由SWIG生成的Python包装器,它依赖于另一个库(ProjectA)。

这些库在Linux和OS X Yosemite或更早版本上运行良好。但是当我在El Capitan上导入ProjectB的Python包装时,我收到以下错误。

 $ python
 >>> import project_b
 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "project_b.py", line 28, in <module>
    _project_b = swig_import_helper()
  File "project_b.py", line 24, in swig_import_helper
    _mod = imp.load_module('_project_b', fp, pathname, description)
 ImportError: dlopen(./_project_b.so, 2): Library not loaded: libProjectA.dylib
  Referenced from: /Users/oxon/cmake_test/ProjectB_build/_project_b.so
  Reason: unsafe use of relative rpath libProjectA.dylib in ./_project_b.so with restricted binary

我认为这与El Capitan的新安全技术有关,如果相对路径调用动态库,则无法加载/usr/local/lib下安装的动态库。实际上,otool -L表明我的Python包装器(_project_b.so)具有ProjectA(libProjectA.dylib下安装的/usr/local/lib)的相对路径。

 $ otool -L _project_b.so 
_project_b.so:
    /System/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.10)
    libProjectA.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

问题:我想知道如何通过修改这些项目中的CMakeLists.txt来解决此错误。如何通过绝对路径链接libProjectA.dylib

我知道install_name_tool命令可以改变绝对路径的相对路径。但我不想让我的图书馆的用户每次都这样做。所以我想在CMakeLists.txt中解决这个问题。

但是,

ProjectB/exeB在没有rpath问题的情况下正常工作,即使它也使用libProjectB.dyliblibProjectA.dylib

这是ProjectA的目录结构,

ProjectA
├── CMakeLists.txt
├── ProjectAConfig.cmake
├── include
│   └── ProjectA
│       └── MyClassA.h
└── src
    └── MyClassA.cxx

和ProjectB。

ProjectB
├── CMakeLists.txt
├── exeB.cxx
├── include
│   └── ProjectB
│       └── MyClassB.h
├── project_b.i
└── src
    └── MyClassB.cxx

我使用以下步骤完成了构建过程。

$ pwd
/Users/oxon/cmake_test
$ ls
ProjectA       ProjectA_build ProjectB       ProjectB_build
$ cd ProjectA_build
$ cmake ../ProjectA
$ make
$ sudo make install
$ cd ../ProjectB_build
$ cmake ../ProjectB
$ make
$ python
>>> import project_b

您可以从中下载我的最少示例 https://github.com/akira-okumura/stackoverflow_question

1 个答案:

答案 0 :(得分:2)

ProjectB/CMakeLists.txt中添加以下行已解决此问题。

if(APPLE)
  set(CMAKE_MACOSX_RPATH TRUE)

# The following settings were copied from
# https://cmake.org/Wiki/CMake_RPATH_handling
# to avoid the rpath issue that appears on OS X El Capitan

  # use, i.e. don't skip the full RPATH for the build tree
  set(CMAKE_SKIP_BUILD_RPATH  FALSE)

  # when building, don't use the install RPATH already
  # (but later on when installing)
  set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) # Changed to TRUE by A.O. 

  set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

  # add the automatically determined parts of the RPATH
  # which point to directories outside the build tree to the install RPATH
  set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

  # the RPATH to be used when installing, but only if it's not a system directory
  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")
endif()

CMAKE_BUILD_WITH_INSTALL_RPATH选项已从FALSE更改为TRUE,以允许用户在安装ProjectB库之前在构建目录下测试import project_b