gpt4 book ai didi

c++ - 如何将编译选项添加到 CMake FetchContent 依赖项?

转载 作者:行者123 更新时间:2023-12-05 03:39:06 27 4
gpt4 key购买 nike

我有一个 C/C++ 项目,我想在其中使用 CppUTest。所以我将 CppUTest 的依赖包括在内:

include(FetchContent)
FetchContent_Declare(
CppUTest
GIT_REPOSITORY https://github.com/cpputest/cpputest.git
GIT_TAG latest-passing-build
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
)
set(TESTS OFF CACHE BOOL "Switch off CppUTest Test build")
FetchContent_MakeAvailable(CppUTest)

这很好用。

在我的(汽车)项目中,我使用 C++20 标准,我需要启用所有/大多数警告。但是当我这样做时,CppUTest 会给我带来很多警告。因此,我想使用与项目代码不同的编译器选项来编译 CppUTest。

如何在 CMake 中配置它?我在谷歌上搜索了很多,但没有找到任何有用的东西。

谢谢,斯特凡

PS:我简化的 CMake 文件是:

cmake_minimum_required(VERSION 3.5)

project(projectName LANGUAGES CXX C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
MESSAGE("Setting Clang flags:")
SET(COMPILE_FLAGS_WARNINGS "-Weverything")
SET(COMPILE_FLAGS_DEBUG "-Og")
SET(COMPILE_FLAGS_RELEASE "-O2")
SET(COMPILE_FLAGS_COVERAGE "--coverage")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
#more options here
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
#more options here
endif()

MESSAGE("Debug: ${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS_WARNINGS} ${COMPILE_FLAGS_DEBUG}")
MESSAGE("Release: ${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS_WARNINGS} ${COMPILE_FLAGS_RELEASE}")
MESSAGE("Coverage: ${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS_WARNINGS} ${COMPILE_FLAGS_DEBUG} ${COMPILE_FLAGS_COVERAGE}")

### Dependencies:

include(FetchContent)
FetchContent_Declare(
CppUTest
GIT_REPOSITORY https://github.com/cpputest/cpputest.git
GIT_TAG latest-passing-build
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
)

set(TESTS OFF CACHE BOOL "Switch off CppUTest Test build")
FetchContent_MakeAvailable(CppUTest)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS_WARNINGS}")
set(CMAKE_CXX_FLAGS_DEBUG "${COMPILE_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_RELEASE "${COMPILE_FLAGS_RELEASE}")

enable_testing()

### add tests
add_subdirectory(path/to/my/tests)

最佳答案

不要使用 set(CMAKE_<LANG>* .使用 *target*接口(interface)。如果您不希望使用特定选项编译所有 内容,请不要全局设置它们。相反:

set_target_properties(mytests PROPERTY
CXX_STANDARD 11
etc.
)
target_compile_options(mytests PUBLIC -option1 -option2 ...)

无论如何,SET(CMAKE_<LANG>_*影响在之后声明的目标。所以搬家set(CMAKE_*FetchContent_MakeAvailable(CppUTest) 之后

关于c++ - 如何将编译选项添加到 CMake FetchContent 依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68708076/

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