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?