<div dir="ltr"><div>I am trying to use Conda as a package manager for isolated C++ development</div><div>environments. But unfortunately, when using CMake with the Anaconda-provided</div><div>compilers [1] (which are used to compile the binary packages in the Anaconda</div><div>repositories), things do not work as expected.</div><div><br></div><div>I have a small test case available here [2], with an executable calling a</div><div>shared library and a third-party dependency installed with Conda.</div><div><br></div><div>[1]: <a href="https://conda.io/docs/user-guide/tasks/build-packages/compiler-tools.html">https://conda.io/docs/user-guide/tasks/build-packages/compiler-tools.html</a></div><div>[2]: <a href="https://gist.github.com/smancill/b28ca07ac11fdf285b4d559545a1630b">https://gist.github.com/smancill/b28ca07ac11fdf285b4d559545a1630b</a></div><div><br></div><div>--------------------------------------------------</div><div><br></div><div>First, when using the system compiler, all works fine (but I am not sure of the</div><div>binary compatibility with the Conda packages, that's why I want to use the</div><div>Anaconda compilers):</div><div><br></div><div>    # create the environment and install XercesC</div><div>    $ conda create -n test-system xerces-c</div><div>    ...</div><div>    environment location: /Users/smancill/.local/share/miniconda3/envs/test-system</div><div>    ...</div><div>    The following NEW packages will be INSTALLED:</div><div><br></div><div>    icu:       58.2-h4b95b61_1</div><div>    libcxx:    4.0.1-h579ed51_0</div><div>    libcxxabi: 4.0.1-hebd6815_0</div><div>    xerces-c:  3.2.1-h44e365a_0</div><div>    ...</div><div><br></div><div>    # activate the environment</div><div>    $ conda activate test-system</div><div><br></div><div>    $ mkdir build-osx-system</div><div>    $ cd build-osx-system</div><div>    $ cmake -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_PREFIX_PATH=$CONDA_PREFIX ..</div><div>    -- The CXX compiler identification is AppleClang 9.0.0.9000039</div><div>    -- Check for working CXX compiler: /usr/bin/c++</div><div>    -- Check for working CXX compiler: /usr/bin/c++ -- works</div><div>    ...</div><div>    -- Found XercesC: /Users/smancill/.local/share/miniconda3/envs/test-system/lib/libxerces-c.dylib (found version "3.2.1")</div><div>    -- Configuring done</div><div>    -- Generating done</div><div>    -- Build files have been written to: /Users/smancill/src/conda-test/build-osx-system</div><div><br></div><div>    $ make -j1 VERBOSE=1</div><div>    ...</div><div>    [100%] Linking CXX executable bar</div><div>    /usr/local/bin/cmake -E cmake_link_script CMakeFiles/bar.dir/link.txt --verbose=1</div><div>    /usr/bin/c++   -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.13.sdk -mmacosx-version-min=10.12 -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/bar.dir/bar.cpp.o  -o bar -Wl,-rpath,/Users/smancill/src/conda-test/build-osx-system -Wl,-rpath,/Users/smancill/.local/share/miniconda3/envs/test-system/lib libfoo.dylib /Users/smancill/.local/share/miniconda3/envs/test-system/lib/libxerces-c.dylib</div><div>    ...</div><div><br></div><div>The build directory (~/src/conda-test/build-osx-system) and the conda</div><div>environment lib directory (~/.local/share/miniconda3/envs/test-system/lib)</div><div>are correctly added to the RPATH in the build tree by the link command:</div><div><br></div><div>    $ ./bar</div><div>    Hello, world!</div><div><br></div><div>    $ otool -L ./bar</div><div>    ./bar:</div><div>        @rpath/libfoo.dylib (compatibility version 1.0.0, current version 0.0.0)</div><div>        @rpath/libxerces-c-3.2.dylib (compatibility version 0.0.0, current version 0.0.0)</div><div>        /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0)</div><div>        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.0.0)</div><div><br></div><div>    $ otool -l ./bar | grep -A2 LC_RPATH</div><div>            cmd LC_RPATH</div><div>        cmdsize 56</div><div>            path /Users/smancill/src/conda-test/build-osx-system (offset 12)</div><div>    --</div><div>            cmd LC_RPATH</div><div>        cmdsize 80</div><div>            path /Users/smancill/.local/share/miniconda3/envs/test-system/lib (offset 12)</div><div><br></div><div>If I install the binary, it fails because I haven't configured CMake to set the install RPATH:</div><div><br></div><div>    $ make install</div><div>    ...</div><div>    Install the project...</div><div>    -- Install configuration: ""</div><div>    -- Installing: /Users/smancill/.local/share/miniconda3/envs/test-system/lib/libfoo.dylib</div><div>    -- Installing: /Users/smancill/.local/share/miniconda3/envs/test-system/include/foo.hpp</div><div>    -- Installing: /Users/smancill/.local/share/miniconda3/envs/test-system/bin/bar</div><div><br></div><div>    $ bar</div><div>    dyld: Library not loaded: @rpath/libfoo.dylib</div><div>      Referenced from: /Users/smancill/.local/share/miniconda4/envs/test-system/bin/bar</div><div>      Reason: image not found</div><div>    [1]    84611 abort      bar</div><div><br></div><div>    $ otool -L $CONDA_PREFIX/bin/bar</div><div>    /Users/smancill/.local/share/miniconda3/envs/test-system/bin/bar:</div><div>        @rpath/libfoo.dylib (compatibility version 0.0.0, current version 0.0.0)</div><div>        @rpath/libxerces-c-3.2.dylib (compatibility version 0.0.0, current version 0.0.0)</div><div>        /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0)</div><div>        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.0.0)</div><div><br></div><div>    $ otool -l $CONDA_PREFIX/bin/bar | grep -A2 LC_RPATH</div><div>    # empty</div><div><br></div><div>    # deactivate the environment to start again</div><div>    $ conda deactivate</div><div><br></div><div>The same can be observed on Linux. Everything works as it should.</div><div><br></div><div>--------------------------------------------------</div><div><br></div><div>If I try to use Anaconda compilers on macOS, the build RPATH is not set</div><div>properly anymore:</div><div><br></div><div>    # create the environment and install the Anaconda compiler for macOS, and XercesC</div><div>    $ conda create -n test-conda clangxx_osx-64 xerces-c</div><div>    ...</div><div>    environment location: /Users/smancill/.local/share/miniconda3/envs/test-conda</div><div>    ...</div><div>    The following NEW packages will be INSTALLED:</div><div><br></div><div>        cctools:        895-h7512d6f_0</div><div>        clang:          4.0.1-h662ec87_0</div><div>        clang_osx-64:   4.0.1-h1ce6c1d_11</div><div>        clangxx:        4.0.1-hc9b4283_0</div><div>        clangxx_osx-64: 4.0.1-h22b1bf0_11</div><div>        compiler-rt:    4.0.1-h5487866_0</div><div>        icu:            58.2-h4b95b61_1</div><div>        ld64:           274.2-h7c2db76_0</div><div>        libcxx:         4.0.1-h579ed51_0</div><div>        libcxxabi:      4.0.1-hebd6815_0</div><div>        llvm:           4.0.1-hc748206_0</div><div>        llvm-lto-tapi:  4.0.1-h6701bc3_0</div><div>        xerces-c:       3.2.1-h44e365a_0</div><div>    ...</div><div><br></div><div>    # activate the environment (which sets the variables to use the Anaconda compiler)</div><div>    $ conda activate test-conda</div><div><br></div><div>    $ mkdir build-osx-conda</div><div>    $ cd build-osx-conda</div><div>    $ cmake -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_PREFIX_PATH=$CONDA_PREFIX ..</div><div>    -- The CXX compiler identification is Clang 4.0.1</div><div>    -- Check for working CXX compiler: /Users/smancill/.local/share/miniconda3/envs/test-conda/bin/x86_64-apple-darwin13.4.0-clang++</div><div>    -- Check for working CXX compiler: /Users/smancill/.local/share/miniconda3/envs/test-conda/bin/x86_64-apple-darwin13.4.0-clang++ -- works</div><div>    ...</div><div>    -- Found XercesC: /Users/smancill/.local/share/miniconda3/envs/test-conda/lib/libxerces-c.dylib (found version "3.2.1")</div><div>    -- Configuring done</div><div>    -- Generating done</div><div>    -- Build files have been written to: /Users/smancill/src/conda-test/build-osx-conda</div><div><br></div><div>    $ make -j1 VERBOSE=1</div><div>    ...</div><div>    [100%] Linking CXX executable bar</div><div>    /usr/local/bin/cmake -E cmake_link_script CMakeFiles/bar.dir/link.txt --verbose=1</div><div>    /Users/smancill/.local/share/miniconda3/envs/test-conda/bin/x86_64-apple-darwin13.4.0-clang++  -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -stdlib=libc++ -fvisibility-inlines-hidden -std=c++14 -fmessage-length=0 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.13.sdk -mmacosx-version-min=10.12 -Wl,-search_paths_first -Wl,-headerpad_max_install_names -Wl,-pie -Wl,-headerpad_max_install_names -Wl,-dead_strip_dylibs CMakeFiles/bar.dir/bar.cpp.o  -o bar -Wl,-rpath,/Users/smancill/src/conda-test/build-osx-conda libfoo.dylib /Users/smancill/.local/share/miniconda3/envs/test-conda/lib/libxerces-c.dylib</div><div>    ...</div><div><br></div><div>You can see that the environment lib path is not added to the RPATH by the link command,</div><div>which in turns result in the executable not running from the build tree anymore:</div><div><br></div><div>    $ ./bar</div><div>    dyld: Library not loaded: @rpath/libxerces-c-3.2.dylib</div><div>      Referenced from: /Users/smancill/src/conda-test/build-osx-conda/./bar</div><div>      Reason: image not found</div><div>    [1]    89350 abort      ./bar</div><div><br></div><div>    $ otool -L ./bar</div><div>    ./bar:</div><div>        @rpath/libfoo.dylib (compatibility version 0.0.0, current version 0.0.0)</div><div>        @rpath/libxerces-c-3.2.dylib (compatibility version 0.0.0, current version 0.0.0)</div><div>        @rpath/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)</div><div>        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.0.0)</div><div><br></div><div>    $ otool -l ./bar | grep -A2 LC_RPATH</div><div>          cmd LC_RPATH</div><div>      cmdsize 56</div><div>         path /Users/smancill/src/conda-test/build-osx-conda (offset 12)</div><div><br></div><div>    # deactivate the environment</div><div>    $ conda deactivate</div><div><br></div><div>--------------------------------------------------</div><div><br></div><div>If I try the Anaconda compilers on Linux, there are strange results too:</div><div><br></div><div>    # create the environment and install the Anaconda compiler for Linux, and XercesC</div><div>    $ conda create -n test-conda gxx_linux-64 xerces-c</div><div>    ...</div><div>    environment location: /home/vagrant/miniconda3/envs/test-conda</div><div>    ...</div><div>    The following NEW packages will be INSTALLED:</div><div><br></div><div>        binutils_impl_linux-64: 2.28.1-had2808c_3</div><div>        binutils_linux-64:      7.2.0-had2808c_27</div><div>        gcc_impl_linux-64:      7.2.0-habb00fd_3</div><div>        gcc_linux-64:           7.2.0-h550dcbe_27</div><div>        gxx_impl_linux-64:      7.2.0-hdf63c60_3</div><div>        gxx_linux-64:           7.2.0-h550dcbe_27</div><div>        icu:                    58.2-h9c2bf20_1</div><div>        libgcc-ng:              7.2.0-hdf63c60_3</div><div>        libstdcxx-ng:           7.2.0-hdf63c60_3</div><div>        xerces-c:               3.2.1-hac72e42_0</div><div><br></div><div>    # activate the environment (which sets the variables to use the Anaconda compiler)</div><div>    $ conda activate test-conda</div><div><br></div><div>    $ mkdir build-linux-conda</div><div>    $ cd build-linux-conda</div><div>    $ cmake -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_PREFIX_PATH=$CONDA_PREFIX ..</div><div>    -- The CXX compiler identification is GNU 7.2.0</div><div>    -- Check for working CXX compiler: /home/vagrant/miniconda3/envs/test-conda/bin/x86_64-conda_cos6-linux-gnu-c++</div><div>    -- Check for working CXX compiler: /home/vagrant/miniconda3/envs/test-conda/bin/x86_64-conda_cos6-linux-gnu-c++ -- works</div><div>    ...</div><div>    -- Found XercesC: /home/vagrant/miniconda3/envs/test-conda/lib/libxerces-c.so (found version "3.2.1")</div><div>    -- Configuring done</div><div>    -- Generating done</div><div>    -- Build files have been written to: /vagrant/conda-test/build-linux-conda</div><div><br></div><div>    $ make -j1 VERBOSE=1</div><div>    ...</div><div>    [100%] Linking CXX executable bar</div><div>    /usr/bin/cmake -E cmake_link_script CMakeFiles/bar.dir/link.txt --verbose=1</div><div>    /home/vagrant/miniconda3/envs/test-conda/bin/x86_64-conda_cos6-linux-gnu-c++   -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -pipe    -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now CMakeFiles/bar.dir/bar.cpp.o  -o bar libfoo.so /home/vagrant/miniconda3/envs/test-conda/lib/libxerces-c.so -Wl,-rpath,/vagrant/conda-test/build-linux-conda:/home/vagrant/miniconda3/envs/test-conda/lib:</div><div>    ...</div><div><br></div><div>You can see that the environment lib path is added to the RPATH by the link</div><div>command (unlike macOS):</div><div><br></div><div>    $ ./bar</div><div>    Hello World!</div><div><br></div><div>But when inspecting the dependencies, the environment lib path appears twice:</div><div><br></div><div>    $ readelf -d ./bar</div><div>    ...</div><div>     0x0000000000000001 (NEEDED)             Shared library: [libfoo.so]</div><div>     0x0000000000000001 (NEEDED)             Shared library: [<a href="http://libxerces-c-3.2.so">libxerces-c-3.2.so</a>]</div><div>     0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]</div><div>     0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]</div><div>     0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]</div><div>     0x000000000000000f (RPATH)              Library rpath: [/home/vagrant/miniconda3/envs/test-conda/lib:/vagrant/conda-test/build-linux-conda:/home/vagrant/miniconda3/envs/test-conda/lib:]</div><div>    ...</div><div><br></div><div>Which is wrong. Now the build tree binary will pick first any old version of</div><div>the foo library installed in the environment instead of the version in the</div><div>build tree.</div><div><br></div><div>It seems that the Anaconda toolchain is setting the environment lib path into</div><div>the RPATH by its own. It is also set when installing the binaries, even when</div><div>CMake is not configured to set the install RPATH:</div><div><br></div><div>    $ make install</div><div>    ...</div><div>    Install the project...</div><div>    -- Install configuration: ""</div><div>    -- Installing: /home/vagrant/miniconda3/envs/test-conda/lib/libfoo.so</div><div>    -- Installing: /home/vagrant/miniconda3/envs/test-conda/include/foo.hpp</div><div>    -- Installing: /home/vagrant/miniconda3/envs/test-conda/bin/bar</div><div>    -- Set runtime path of "/home/vagrant/miniconda3/envs/test-conda/bin/bar" to ""</div><div><br></div><div>    $ bar</div><div>    Hello World!</div><div><br></div><div>    $ readelf -d $CONDA_PREFIX/bin/bar</div><div>    ...</div><div>     0x0000000000000001 (NEEDED)             Shared library: [libfoo.so]</div><div>     0x0000000000000001 (NEEDED)             Shared library: [<a href="http://libxerces-c-3.2.so">libxerces-c-3.2.so</a>]</div><div>     0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]</div><div>     0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]</div><div>     0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]</div><div>     0x000000000000000f (RPATH)              Library rpath: [/home/vagrant/miniconda3/envs/test-conda/lib]</div><div>    ...</div><div><br></div><div>    # deactivate the environment</div><div>    $ conda deactivate</div><div><br></div><div>--------------------------------------------------</div><div><br></div><div>TL;DR I cannot get CMake and the Anaconda compilers and packages working</div><div>correctly.</div><div><br></div><div>- On macOS, the Conda environment library path is not added to the build RPATH.</div><div>- On Linux, the Conda environment library path is always added to the RPATH</div><div>  (in both build and install) independently of CMake.</div><div><br></div><div>Any advice or workarounds?</div><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div class="gmail_signature"><div dir="ltr"><div style="font-size:small;font-family:arial">Sebastian Mancilla<br></div></div></div></div></div>