dlopen error "unsafe use of relative rpath" on OS X El Capitan
<p>I have been developing two C++ libraries which are built by CMake. One of the libraries (ProjectB) has a Python wrapper generated by SWIG, which is dependent on the other library (ProjectA).</p>

<p>These libraries work fine on Linux and OS X Yosemite or earlier. But I get the following error when I import the Python wrapper of ProjectB on El Capitan.</p>

<pre><code> $ python
 &gt;&gt;&gt; import project_b
 Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "project_b.py", line 28, in &lt;module&gt;
    _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
</code></pre>

<p>I think this is related to the new security technology of El Capitan, and dynamic libraries installed under <code>/usr/local/lib</code> cannot be loaded if they are called by a relative path. Indeed, <code>otool -L</code> shows that my Python wrapper (<code>_project_b.so</code>) has a relative path to ProjectA (<code>libProjectA.dylib</code> which is installed under <code>/usr/local/lib</code>).</p>

<pre><code> $ 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)
</code></pre>

<p><strong>Question</strong>: I would like to know how to fix this error by modifying my CMakeLists.txt in these project. How can I link <code>libProjectA.dylib</code> by absolute path?</p>

<p>I know that the <code>install_name_tool</code> command can change a relative path to an absolute path. But I do not want to ask the users of my libraries to do it every time. So I would like to fix this issue in CMakeLists.txt.</p>

<p><code>ProjectB/exeB</code>, however, works OK without the rpath issue even though it also uses <code>libProjectB.dylib</code> and <code>libProjectA.dylib</code>.</p>

<hr>

<p>Here is the directory structure of ProjectA,</p>

<pre><code>ProjectA
├── CMakeLists.txt
├── ProjectAConfig.cmake
├── include
│   └── ProjectA
│       └── MyClassA.h
└── src
    └── MyClassA.cxx
</code></pre>

<p>and of ProjectB.</p>

<pre><code>ProjectB
├── CMakeLists.txt
├── exeB.cxx
├── include
│   └── ProjectB
│       └── MyClassB.h
├── project_b.i
└── src
    └── MyClassB.cxx
</code></pre>

<p>I did the build process with the following steps.</p>

<pre><code>$ 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
&gt;&gt;&gt; import project_b
</code></pre>

<p>You can download my minimum examples from
<a href="https://github.com/akira-okumura/stackoverflow_question" rel="nofollow">https://github.com/akira-okumura/stackoverflow_question</a></p>