在我的工作设置中,我的“编译器”是一个shell脚本,它设置了一些环境变量并调用了实际的编译器(clang-wrapper
):
#!/bin/sh
export PATH=/path_to_clang_install/bin:/path_to_gcc_install/bin${PATH:+:$PATH}
export LD_LIBRARY_PATH=/path_to_clang_install/lib64:/path_to_gcc_install/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
exec /path_to_clang_install/bin/clang++ --gcc-toolchain=/path_to_gcc_install "$@"
据我了解,cmake和ctest将所有链接目录添加到构建目录中链接的可执行文件的RPATH中,并删除安装目录的RPATH。 (目前我不在乎安装目录,使用objdump -x
时看到的是用target_link_libraries
添加的库的目录出现在RPATH中。)
但是,编译器标准库(/path_to_clang_install/lib64
)的目录未添加到RPATH。
作为最低限度的工作示例,我有
CMakeLists.txt
add_executable(foo foo.cc)
enable_testing()
add_test(NAME foo COMMAND foo)
foo.cc
#include <string>
int main(int, char** argv) {
return std::string(argv[0]).length();
}
然后我用它构建
cmake .. -DCMAKE_CXX_COMPILER=clang-wrapper -DCMAKE_C_COMPILER=clang-wrapper
cmake --build .
ctest
测试通常会失败
UpdateCTestConfiguration from :<buildpath>/DartConfiguration.tcl
UpdateCTestConfiguration from :<buildpath>/DartConfiguration.tcl
Test project <buildpath>
Constructing a list of tests
Done constructing a list of tests
Updating test list for fixtures
Added 0 tests to meet fixture requirements
Checking test dependency graph...
Checking test dependency graph end
test 1
Start 1: foo
1: Test command: <buildpath>/foo
1: Test timeout computed to be: 10000000
1: <buildpath>/foo: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by <buildpath>/foo)
1/1 Test #1: foo ..............................***Failed 0.02 sec
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 0.13 sec
The following tests FAILED:
1 - foo (Failed)
Errors while running CTest
(链接器被/path_to_clang_wrapper/clang-wrapper -rdynamic CMakeFiles/foo.dir/foo.o -o foo
调用。)
我尝试用拼写出标准库
enable_testing()
link_directories(/path_to_gcc/lib64)
add_executable(foo foo.cc)
target_link_libraries(foo /path_to_gcc/lib64/libstdc++.so)
,但这只会将-lstdc++
添加到链接器调用中。我已验证,如果我链接到某些非编译器库,则链接器调用会收到额外的
-L/path_to_random_lib/lib -Wl,-rpath,/path_to_random_lib/lib -lrandomlib
。
所以我的问题是:如何使用cmake将编译器标准库添加到RPATH?我赞赏构建树中的可执行文件和ctests完全独立于环境变量(LD_LIBRARY_PATH)。