问题描述
我的任务是为基于c ++的项目(使用swig)构建python绑定。该项目使用cmake进行构建和ctest进行测试,并且应该将绑定的构建和测试集成到其中。
I'm tasked with building python bindings for a c++-based project (using swig). The project uses cmake to build and ctest to test and the build and test of the bindings are supposed to be integrated into this.
我已经完成了构建工作,并且手动运行时,测试可以正常工作,但是我必须设置几个环境变量才能使它们正常工作,但我无法为自动化过程设置这些环境变量。
I've gotten the build to work and the tests work when run manually, but I have to set a couple of environment variables in order for them to work and I'm having trouble setting those for the automated process.
我需要设置LD_LIBRARY_PATH和PYTHONPATH。 PYTHONPATH我可以通过在测试脚本中操作sys.path来解决,但是使用LD_LIBRARY_PATH则更难。到目前为止,我已经在测试目录的CMakelists.txt中添加了以下内容:
I need to set LD_LIBRARY_PATH and PYTHONPATH. PYTHONPATH I can get around by manipulating sys.path within the testing script, but that's harder to do with LD_LIBRARY_PATH. So far I have the following added to the CMakelists.txt in the testing directory:
#Python wrapper testing
find_package(PythonInterp 3.5 REQUIRED)
if (NOT PYTHONINTERP_FOUND)
message(STATUS "Python interpreter NOT found")
else(NOT PYTHONINTERP_FOUND)
message(STATUS "Python interpreter found")
ADD_TEST(NAME testPyMyproj
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_scripts/test_pyMyproj.py
)
set_property(TEST testPyMyproj PROPERTY ENVIRONMENT LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib/)
endif (NOT PYTHONINTERP_FOUND)
错误我得到的是
ImportError: libMyproj.so: cannot open shared object file: No such file or directory
我的绑定链接到哪个库,并且位于 $ {CMAKE_BINARY_DIR}指定的目录中/ lib /
。
Which is the library my bindings are linked to and is in the directory specified by ${CMAKE_BINARY_DIR}/lib/
.
我认为这是指 $ LD_LIBRARY_PATH
设置不正确,但是我不知道我在做什么。我做错了。
I take this to mean that $LD_LIBRARY_PATH
is not set correctly, but I don't know what I'm doing wrong.
是否可以在测试中检查变量的状态是什么?有人可以发现我在做什么错吗?
Is there a way to check within the test what the state of the variable is? Can anyone spot what I am doing wrong?
推荐答案
您可以使用<$设置环境变量作为调用测试的一部分c $ c> cmake -E env 命令。您可以将 add_test()
调用修改为以下内容:
You can set environment variables as part of invoking the test by using the cmake -E env
command. You can modify the add_test()
call to something like the following:
ADD_TEST(NAME testPyMyproj
COMMAND ${CMAKE_COMMAND} -E env
LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib:$ENV{LD_LIBRARY_PATH}
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_scripts/test_pyMyproj.py
)
以上假设基于Unix的主机环境,但是您可以如果需要,可以将其概括化为支持所有平台/发电机类型。
The above assumes a Unix-based host environment, but you could generalise this to support all platforms/generator types with a bit of work if you needed to.
另一种替代方法是使用测试属性,它应该可以实现基本相同的功能:
Another alternative is to use the ENVIRONMENT
test property which should achieve essentially the same thing:
set_tests_properties(testPyMyproj PROPERTIES
ENVIRONMENT LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib:$ENV{LD_LIBRARY_PATH})
这篇关于无法为CTest测试设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!