8

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.

nbadami
  • 81
  • 2
  • 1
    This would be a great feature to have for linux and cross-platform projects. Visual Studio 16.10 added this recently via an optional setting and including the external library with angle brackets: `#include `. – Casey Jun 01 '21 at 22:04
  • 2
    "the warnings from **building** the `cpr` library are preventing the project from building" - So you get warnings only when compile `cpr` library itself, not when you include its headers? The library integrated with with `FetchContent` uses those compiler flags, which are set in `CMAKE_CXX_FLAGS` at the time when `FetchContent_MakeAvailable` is called. Make sure these flags do not treat warning, emitted by the library, as errors. – Tsyvarev Jun 01 '21 at 22:27
  • Yes it is only when I try to compile the project. Could you elaborate a bit more on the compiler flags? These flags would be set based on cpr's CMake file right? – nbadami Jun 02 '21 at 13:57
  • @nbadami, the important question is whether the warnings come when compiling `cpr/…something.cpp`, or when comping your own .cpp that includes something from `cpr/include`. Different mitigation is needed for each problem. – Jan Hudec Jul 21 '21 at 12:53
  • Does this answer your question? [Is there a way to get -isystem for FetchContent targets?](https://stackoverflow.com/questions/64064157/is-there-a-way-to-get-isystem-for-fetchcontent-targets) – starball Nov 24 '22 at 08:19
  • `cpr` builds with zero warnings for me. What kind of warnings are you getting? Please provide at least one unedited complete warning. – n. m. could be an AI Nov 24 '22 at 11:45

1 Answers1

-1

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
ndx
  • 1