gpt4 book ai didi

c++ - 在子目录中使用target_sources()命令时进行单元测试(qtest)的项目结构

转载 作者:行者123 更新时间:2023-12-02 10:13:07 31 4
gpt4 key购买 nike

首先,我知道这个论坛中有非常相似的问题。但是,没有人能真正回答我的具体情况。
我有以下项目结构:

|---Project_Root
|---CMakeLists.txt
|---build
|---src
| |---CMakeLists.txt
| |---many .cpp and .h files in multiple subfolders with a different CMakeLists.txt
|---tests
| |---CMakeLists.txt
| |---many .cpp files
在Project_Root / CMakeLists.txt中:
cmake_minimum_required(VERSION 3.5)

set(SRC_DIR src)

project(
Project
LANGUAGES CXX
)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(${PROJECT_NAME} "")
target_include_directories(${PROJECT_NAME} PRIVATE ${SRC_DIR})
add_subdirectory(${SRC_DIR})
在src文件夹和CMakeLists.txt子文件夹中:
cmake_minimum_required(VERSION 3.5)

set(SRC_FILES
Source1.cpp
)

set(HEADER_FILES
Source1.hpp
)
target_sources(${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FILES}
${CMAKE_CURRENT_SOURCE_DIR}/${HEADER_FILES}
)
现在在测试子文件夹中:
project(Test LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
enable_testing()

add_executable(Test tst_test.cpp)
add_test(NAME Test COMMAND Test)

target_link_libraries(Test PRIVATE Qt5::Test)
在上面的CMakeLists.txt中,我想将主项目包括为静态库。例如,我可以在根CMakeLists.txt中添加一个新的库目标(例如 Project_Lib),然后再次使用以下命令:
target_sources(Project_Lib
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FILES}
${CMAKE_CURRENT_SOURCE_DIR}/${HEADER_FILES}
)

在所有子文件夹中。
但是有没有一种更优雅的方法,而不必修改子文件夹中的所有CMakeLists.txt?例如,是否有办法从 Project目标中提取源文件,以便可以将其重新用于制作 Project_Lib目标?

最佳答案

如您所述,您可以将新的静态库目标设置为Project_Lib。利用您通过使用项目名称(${PROJECT_NAME})参数化了目标名称这一事实,因此您实际上不必更改子文件夹中的所有CMakeLists.txt文件。只需更改项目名称。
正如我所评论的,只需从静态库中排除main.cpp文件中的,然后将其添加到单独的可执行目标中即可。
在Project_Root / CMakeLists.txt中:

cmake_minimum_required(VERSION 3.5)

set(SRC_DIR src)

# Change the project name, as now the static library is the primary target.
project(
Project_Lib
LANGUAGES CXX
)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Create the static library target, whose sources are populated in subdirectories.
add_library(${PROJECT_NAME} STATIC)
target_include_directories(${PROJECT_NAME} PRIVATE ${SRC_DIR})
# The only modification necessary in the subdirectories is to *exclude* the
# main.cpp file from the target_sources for the static library.
add_subdirectory(${SRC_DIR})

# Add *only* the main.cpp file to the executable target.
add_executable(Project_Exe src/main.cpp)
# Link the static library target to the executable.
target_link_libraries(Project_Exe PRIVATE ${PROJECT_NAME})
在tests / CMakeLists.txt中:
...
# Link the static library to your Test executable also.
target_link_libraries(Test PRIVATE Project_Lib Qt5::Test)

关于c++ - 在子目录中使用target_sources()命令时进行单元测试(qtest)的项目结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62822893/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com