首页 > 解决方案 > 在 CMake 中抑制来自外部库的编译器警告

问题描述

我正在尝试按照下面复制的 CMake 说明将以下项目https://github.com/whoshuu/cpr#cmake构建到我的项目中:

include(FetchContent)
FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/whoshuu/cpr.git GIT_TAG c8d33915dbd88ad6c92b258869b03aba06587ff9) # the commit hash for 1.5.0
FetchContent_MakeAvailable(cpr)

我的项目已经有一些与主要目标链接的其他库,所以我包含了这个新库,如下所示:

target_link_libraries(my_target PRIVATE cpr::cpr PUBLIC other_libraries)

这样做的问题是构建cpr库的警告阻止了项目的构建。我想压制这些警告。我已尝试SYSTEM按照此处的建议添加关键字:How to suppress GCC warnings from library headers? 所以代码如下所示:

target_link_libraries(my_target PRIVATE SYSTEM cpr::cpr PUBLIC other_libraries)

但这没有帮助。是否有其他方法可以抑制 CMake 中外部库的警告?如果有帮助,我正在使用C++-17 g++-11和忍者。

标签: c++cmake

解决方案


我知道这在技术上并不能回答您的问题,但是,作为新手,我无法发表评论。

我能找到的唯一方法是在代码中使用编译器编译指示禁用警告:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weverything"
#include <cpr/cpr.h>
#pragma GCC diagnostic pop

这取决于编译器。如果您使用 clang,只需将“GCC”替换为“clang”即可。在 Visual Studio 上,使用 pragma 警告...这可以通过宏进行移植,看看这篇文章


推荐阅读