简体   繁体   中英

CMake's ctest: image not found (possible rpath issue)

A CMake project builds and runs tests OK on Linux and Windows, but running the test program fails on macOS.

dyld: Library not loaded: libfoo.dylib
  Referenced from: /some/path/test_foo
  Reason: image not found

I can verify that libfoo.dylib is in the same dir as the test program test_foo . Running ./test_foo works OK.

CMake calls test_foo via ctest (located in another path). Calling by ctest seems to be what makes it fail, but if I try a command like this (working dir being /some/path ) it works:

DYLD_LIBRARY_PATH=`pwd` /another/path/ctest

I assume that running tests with add_test and ctest without setting DYLD_LIBRARY_PATH is possible since I can just run test_foo with success, but I haven't had luck.

Here are the current relevant contents of CMakeLists.txt

set(TEST_LIBS foo)
set(TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

add_executable(test_foo tests/testfoo.cpp)

set_target_properties(test_foo PROPERTIES COMPILE_FLAGS "-DSELF_TEST")
target_link_libraries(test_foo ${TEST_LIBS} ${STUFF})
add_dependencies(build_tests test_foo)

add_test(NAME test_foo COMMAND $<TARGET_FILE:test_foo>
        WORKING_DIRECTORY ${TEST_DIR})
  • I've tried setting/unsetting CMAKE_MACOSX_RPATH and MACOSX_RPATH before these lines, as suggested here :
  • I've tried adding the following snippet after the lines, as suggested here :

      IF(APPLE) SET(CMAKE_INSTALL_NAME_DIR ${TEST_DIR}) SET(CMAKE_BUILD_WITH_INSTALL_RPATH ON) ENDIF(APPLE) 
  • I've not tried using install , I'm not sure how to configure that for my project or even what it does.

Any known approaches or any obvious mistakes here? Is install worth looking at? I want to be able to run the test from the CMake generated ctest line in the Makefile.

CMake 3.5.2, macOS 10.12.5

Solved as mentioned in comments, thanks to @Tsyvarev

Original comment pointing to the cause of the issue:

I can verify that 'libfoo.dylib' is in the same dir as the test program 'test_foo'. - Really? Your executable is created under binary tree, do you have the library there? If so, why do you use TEST_DIR as working directory or as a RPATH, while it points to source tree (tests subdirectory)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM