CMake 为目标设置自定义配置名称

时间:2021-07-06 11:01:19

标签: c++ cmake

我有一个名为 test_containers 的项目。它只包含带有测试和 CMakeLists.txt 的 cpp 文件:

cmake_minimum_required(VERSION 3.0.0)
project(test_containers)

set(source_files
    test_array.cpp
    test_linear_hash_table.cpp
    test_list.cpp
    test_set.cpp
    test_span.cpp
    test_stack.cpp
    test_string.cpp
    test_vector.cpp
)

foreach(source IN LISTS source_files)
    get_filename_component(name ${source} NAME_WE)
    add_executable(${PROJECT_NAME}.${source})
    set_target_properties(${PROJECT_NAME}.${source} PROPERTIES OUTPUT_NAME ${name})
    target_sources(${PROJECT_NAME}.${source} PRIVATE ${source})
    target_link_libraries(${PROJECT_NAME}.${source} PRIVATE gtest_main wise_engine_cert)
    add_test(
        NAME ${name}
        COMMAND ${PROJECT_NAME}.${source}
    )
endforeach()

CMake 生成名为 test_array、test_linear_hash_table 等的 bin 文件。在 CTest 中,我看到所有测试都被称为相同的 test_array、test_linear_hash_table 等。

问题是我不知道如何更改配置名称。在 CLion 配置列表中,我看到类似“test_containers.test_array.cpp”的内容。如何让自定义配置名称像简单的“test_array”一样?

1 个答案:

答案 0 :(得分:1)

这能让你的船漂浮吗?

foreach(source IN LISTS source_files)
    get_filename_component(name ${source} NAME_WE)
    add_executable(${name})
    target_sources(${name} PRIVATE ${source})
    target_link_libraries(${name} PRIVATE gtest_main wise_engine_cert)
    add_test( NAME ${name} COMMAND ${name}
    )
endforeach()