CMake无法找到测试,如果CMAKE_RUNTIME_OUTPUT_DIRECTORY更改 [英] CMake cannot find test if CMAKE_RUNTIME_OUTPUT_DIRECTORY is changed

查看:1619
本文介绍了CMake无法找到测试,如果CMAKE_RUNTIME_OUTPUT_DIRECTORY更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CMake构建我的项目,我试图为每个模块创建一组测试套件。显然,如果我修改变量 CMAKE_RUNTIME_OUTPUT_DIRECTORY ,然后ctest不能找到运行测试和失败。

I'm building my project with CMake, and I'm trying to create a bunch of test suites for each module. Apparently if I modify the variable CMAKE_RUNTIME_OUTPUT_DIRECTORY then ctest cannot find the test to run and fails.

一个最小的例子来展示我在说什么,我运行它与CMake 2.8.11.2在Lubuntu 13.10。我会感激,如果有人可以告诉我这是一个错误和/或如何解决它。感谢。

I've made a minimal example to showcase what I am talking about, and I'm running it with CMake 2.8.11.2 on Lubuntu 13.10. I'd appreciate if somebody could tell me whether this is a bug and/or how to work around it. Thanks.

档案CMakeLists.txt:

file CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
project (Test)

# Put all tests in the test directory, where the sources also are
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/test)

enable_testing()

add_subdirectory (${PROJECT_SOURCE_DIR}/test)

file test / CMakeLists.txt:

file test/CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

add_executable(ttest main.cpp)
add_test(ttest ttest)



文件test / main.cpp:

file test/main.cpp:

int main() {
    return 0;
}

建立新文件夹后 build ,可执行文件在 test 文件夹中正确创建。从构建结果中运行 make test c>将输出以下内容:

After building in a new folder build, the executable is correctly created in the folder test. Running make test from build results in the following output:

Running tests...
Test project /home/svalorzen/Tests/cmake/build
    Start 1: ttest
Could not find executable ttest
Looked in the following places:
ttest
ttest
Release/ttest
Release/ttest
Debug/ttest
Debug/ttest
MinSizeRel/ttest
MinSizeRel/ttest
RelWithDebInfo/ttest
RelWithDebInfo/ttest
Deployment/ttest
Deployment/ttest
Development/ttest
Development/ttest
Unable to find executable: ttest
1/1 Test #1: ttest ............................***Not Run   0.00 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.00 sec

The following tests FAILED:
      1 - ttest (Not Run)
Errors while running CTest
make: *** [test] Error 8


推荐答案

编辑:



实际上我错过了 add_test


如果COMMAND指定可执行目标(由add_executable创建),它将自动替换为在构建时创建的可执行文件

If COMMAND specifies an executable target (created by add_executable) it will automatically be replaced by the location of the executable created at build time

所以使用 ttest 而不是 $< TARGET_FILE:ttest> 应该可以使用。

So using ttest instead of $<TARGET_FILE:ttest> should work.

问题,但我真的不知道它是否是一个错误。

I got the same problem but I do not really know if it is a bug or not.

我发现的解决方案是在命令中提供测试可执行文件的路径(使用目标ttest):

The solution I found for this is providing the path of the test executable in the command (with the target ttest):

in test / CMakeLists.txt:

in test/CMakeLists.txt :

add_test(ttest test/ttest)

最后,您希望将路径存储在变量中,以便写入 $ {test_dir} /

Eventually, you would like to store the path in a variable in order to write something like ${test_dir}/ttest.

如果你想要更健壮的东西,最好使用long add_test命令和生成器表达式:

If you want something more robust, the best is to use the long add_test command and generator expressions :

add_test(NAME ttest COMMAND $<TARGET_FILE:ttest>)

生成表达式 $< TARGET_FILE:ttest> 将扩展到可执行目标的输出文件确保目录没有问题。 cmake文档中引用了其他表达式。

The generation expression $<TARGET_FILE:ttest> will expand to the output file of the executable target (ttest here), so you are sure there is no problem with the directory. There is other expression referenced in cmake documentation.

如果你有很多测试声明,我可以得到一个详细的,所以我使用一个宏,如:

It can get a little verbose if you have lot of tests to declare so I use a macro like :

macro (create_test target)
  add_test (NAME ${target} COMMAND $<TARGET_FILE:${target}>)
endmacro (create_test)

#[some code]

#test definition
create_test(ttest)

假设测试名称与可执行文件名称相同。

Assuming the test name is the same as the executable name.

我找不到任何其他工作解决方案。可以使用 add_test 设置工作目录,这显然使得ctest找到可执行文件,但测试然后崩溃时出现BAD_COMMAND错误。

I could not find any other working solution. It is possible to set the working directory with add_test, which apparently make ctest find the executable but the test then crashes with a "BAD_COMMAND" error.

我是新的cmake,所以可能有另一个解决方案。

I'm new with cmake, so may be there is another solution.

这篇关于CMake无法找到测试,如果CMAKE_RUNTIME_OUTPUT_DIRECTORY更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆