简体   繁体   中英

Suppress compiler warnings from external library in CMake

I am trying to build the following project https://github.com/whoshuu/cpr#cmake into my project by following their instructions for CMake reproduced below:

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)

My project already had some other libraries linked with the main target so I included this new library as follows:

target_link_libraries(my_target PRIVATE cpr::cpr PUBLIC other_libraries)

The problem with this is that the warnings from building the cpr library are preventing the project from building. I would like to suppress these warnings. I have tried adding the SYSTEM keyword as recommended here: How to suppress GCC warnings from library headers? so the code would look as follows:

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

but this did not help. Are there other methods to suppress warnings from external libraries in CMake? If it helps, I am using C++-17 g++-11 and Ninja.

I know this does not technically answer your question, however, being new here I cannot post comments.

The only way around I could find was to disable warnings using compiler pragmas within the code:

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

This is compiler dependent. If you use clang, simply replace "GCC" by "clang". On Visual Studio, use pragma warning... This can be made portable with macros, have a look at this article .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM