2

Suppose I am setting up a fairly strict compiler warning level for my own code but the problem is my code is dependent on an external library which wasn't written too rigorously, so when I include the header file from the external library I get all sorts of warnings. Is there a way I can set different warning levels for different files in CMake?

Here is an example showing the situation, suppose I have main.cpp

#include "external.h"

int main(){
  // some code
}

and the corresponding CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(Test)

set(PROJECT_SRCS
${PROJECT_SOURCE_DIR}/main.cpp
)

if(MSVC)
  if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
    string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  else()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
  endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W -Wall -Werror -Wshadow")
endif()

include_directories(${EXTERNAL_INC})

add_executable(Test ${PROJECT_SRCS})

target_link_libraries(Test ${EXTERNAL_LIB})

Suppose "external.h" is causing -Wshadow warnings, I can take -Wshadow out but that also means any -Wshadow warnings won't be catched for my native code. I know I can add pragma warning push and pop in main.cpp but this approach will only work for windows. Is it possible to do it in CMake so it works for different platform such as linux and windows and also keep the source code clean?

user3667089
  • 2,996
  • 5
  • 30
  • 56

2 Answers2

3

I know I can add pragma warning push and pop in main.cpp but this approach will only work for windows.

GCC has diagnostic pragmas, MSVC warning pragmas. Just check for the compiler: MSVC has _MSC_VER, GCC has __GNUC__ predefined.

Is there a way I can set different warning levels for different files in CMake?

set_source_files_properties allows you to add compile flags to specific source files to override previous options.


You just want to isolate warnings of these (bad written) headers, not of your whole source file, so using pragmas for these includes is the safer way.

Youka
  • 2,646
  • 21
  • 33
1

No, you can't do anything with CMake regarding this issue. You can only rely on some intrusive compiler-dependant method like push/pop pragma warnings. It seems you know how to do it with MSVC already and here is the link how to do it with GCC.

But why you can't do it with CMake without modifying the headers? Because headers are not standalone files they are get embedded into the cpp ones and only then they are processed. So it is the whole units which should be set-up, not the headers. And in general you don't know what units include what headers. So even if the compiler you use supports per file warning levels it is too brittle to try to use this feature: the header might be included in any other unit later by some different programmer who knows nothing about your strategy — it depends too much on a human which is bad.

It is much easier to create TurnOffWarnings/TurnOnWarnings header files and force the policy in the organization to wrap all the includes with angle brackets between these custom headers.

Community
  • 1
  • 1
ixSci
  • 13,100
  • 5
  • 45
  • 79