1
votes

I have a simple library with unit tests. That library can be built as static linked library or dynamic library. The library builds for Windows, Linux and MacOS.

The library is using CMake (with Qt) and has ctest unit tests.

This is how the library gets build and unit tests are called:

cmake -DCMAKE_BUILD_TYPE=Debug path/to/sources
cmake --build . --target all
cmake --build . --target test

This is all working fine, except for MacOS. When building the library is a dynamic linked library, the unit tests do not find the library.

    Start 110: UnitTest
110/119 Test #110: UnitTest.................***Exception: Child aborted  0.01 sec
dyld: Library not loaded: library.dylib
  Referenced from: /tmp/build/bin/UnitTest
  Reason: image not found

Calling ctest directly or calling the executable directly works fine. Only when calling the test target from cmake, the unit test executable does not find the library.

Setting DYLD_LIBRARY_PATH to the location of the library does not help. I used install_name_tool to change the path to the library to an absolute path, which fixes the problem. However, this is only a indicator of the problem, not a solution.

I suspect cmake changes the DYLD_LIBRARY_PATH when calling the test target. Is this true? How should this work?

1

1 Answers

1
votes

I suspect cmake changes the DYLD_LIBRARY_PATH when calling the test target. Is this true?

In fact, ctest is used to execute your tests previously described in your CMakeLists.txt and neither cmake or ctest update DYLD_LIBRARY_PATH environment variable.

That said, if you would like to associate a specific test with some environment variable, you could do so by setting the ENVIRONMENT test property. For example:

set_property(TEST ${name} PROPERTY ENVIRONMENT "DYLD_LIBRARY_PATH=/path/to/foo")

To better address your problem, I suggest you do the following:

  • indicate the version of CMake binaries used
  • if possible possible, provide a small example allow to reproduce the problem
  • share the output of these two commands:
    • (1) ldd /tmp/build/bin/UnitTest
    • (2) otool -l /tmp/build/bin/UnitTest | grep -A 3 LC_RPATH | grep path

As a side note, consider specifying the type of parameter passed to cmake, prefer -DCMAKE_BUILD_TYPE:STRING=Debug instead of -DCMAKE_BUILD_TYPE=Debug.