CMake cannot find test if CMAKE_RUNTIME_OUTPUT_DIRECTORY is changed

前端 未结 3 1151
时光说笑
时光说笑 2021-02-07 03:57

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

相关标签:
3条回答
  • 2021-02-07 04:30

    You will also have this problem if your test command contains parameters and you quote them together with the executable name.

    So if you have something like this:

        add_test(NAME "name" COMMAND "testExe arg1 arg2 arg3")
    

    Instead do this:

        add_test(NAME "name" COMMAND "testExe" "arg1" "arg2" "arg3")
    

    Otherwise CMake will try to find a test executable name with spaces in it.

    0 讨论(0)
  • 2021-02-07 04:41

    edit :

    actually I missed something in the documentation for add_test:

    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

    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.

    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 :

    add_test(ttest test/ttest)
    

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

    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>)
    

    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.

    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.

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

    0 讨论(0)
  • 2021-02-07 04:43

    Use command:

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

    Writing NAME and COMMAND is mandatory when using TARGET_FILE (or at least it's not working for me otherwise. Use cmake --help-command add_test for more information.

    0 讨论(0)
提交回复
热议问题