简体   繁体   English

CMake的ctest:找不到图片(可能是rpath问题)

[英]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. CMake项目可以在Linux和Windows上正常构建并运行测试,但是在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 . 我可以验证libfoo.dylib与测试程序test_foo位于同一目录中。 Running ./test_foo works OK. 运行./test_foo可以正常工作。

CMake calls test_foo via ctest (located in another path). CMake通过ctest调用test_foo (位于另一个路径中)。 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: ctest调用似乎是使它失败的原因,但是如果我尝试这样的命令(工作目录为/some/path ),它将起作用:

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. 我假设可以使用add_testctest运行测试而无需设置DYLD_LIBRARY_PATH ,因为我可以成功运行test_foo ,但是我没有走运。

Here are the current relevant contents of CMakeLists.txt 这是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 : 我尝试在这些行之前设置/ CMAKE_MACOSX_RPATH设置CMAKE_MACOSX_RPATHMACOSX_RPATH ,如此处所示
  • 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. 我没有尝试使用install ,我不确定如何为我的项目甚至它做什么配置它。

Any known approaches or any obvious mistakes here? 这里有任何已知的方法或明显的错误吗? Is install worth looking at? install值得一看吗? I want to be able to run the test from the CMake generated ctest line in the Makefile. 我希望能够从Makefile中的CMake生成的ctest行中运行测试。

CMake 3.5.2, macOS 10.12.5 CMake 3.5.2,macOS 10.12.5

Solved as mentioned in comments, thanks to @Tsyvarev 如评论中所述解决,感谢@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)? 如果是这样,为什么您在将TEST_DIR指向源树(tests子目录)的同时将其用作工作目录或RPATH?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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