简体   繁体   中英

disable all warnings for google tests from cmake

I have a c++ project that I am compiling with cmake. I am also using google tests. These are my cmake files:

cmake_minimum_required(VERSION 3.5)
project(se-03-team-08 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 14)

if(EXISTS $ENV{HOME}/Qt/5.14.2/gcc_64)
  set(CMAKE_PREFIX_PATH $ENV{HOME}/Qt/5.14.2/gcc_64)
endif()

if(EXISTS $ENV{HOME}/Qt/5.14.1/gcc_64)
  set(CMAKE_PREFIX_PATH $ENV{HOME}/Qt/5.14.1/gcc_64)
endif()

include_directories(src)
add_subdirectory(src)
add_subdirectory(tests)

set(directory db)
add_custom_target(build-time-make-directory ALL
    COMMAND ${CMAKE_COMMAND} -E make_directory ${directory})

Tests:

cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)

include_directories(${CMAKE_SOURCE_DIR})

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)

find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Find the QtTest library
find_package(Qt5 COMPONENTS Widgets Network Charts WebEngineWidgets Sql CONFIG REQUIRED)
qt5_add_resources(RESOURCES ../src/frontend/resource.qrc)

# Populate two CMake variables with the sources
set(test_SRCS tests.cpp)

set(project_SRCS 
    ../src/backend/game.cpp
    ../src/frontend/resource.qrc
    )

add_executable(beergame-tests ${test_SRCS} ${project_SRCS} ${RESOURCES})

# set_target_properties(beergame-tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/)

target_link_libraries(beergame-tests Qt5::Widgets Qt5::Charts Qt5::WebEngineWidgets Qt5::Sql ${GTEST_LIBRARIES} pthread)

set(CMAKE_CXX_FLAGS "-g -O0 -Wall -fprofile-arcs -ftest-coverage")
set(CMAKE_C_FLAGS "-g -O0 -Wall -W -fprofile-arcs -ftest-coverage")
set(CMAKE_EXE_LINKER_FLAGS "-fprofile-arcs -ftest-coverage")

The tests.cpp file gives a few warnings during compilation with make . For example about unused variables. I believe these are coming from gcc:

warning: unused variable ‘C’ [-Wunused-variable]
     PlayerDialog* C = new PlayerDialog(&w, *newgame, consumer);

Is there any way I can disable them?

I tried adding -w here:

set(CMAKE_C_FLAGS "-g -O0 -Wall -W -fprofile-arcs -ftest-coverage")

but it didn't work.

-Wall is the compiler flag to enable all warnings. If you don't to see those warnings, I would start with removing that compile flag.

That means, instead of

set(CMAKE_C_FLAGS "-g -O0 -Wall -W -fprofile-arcs -ftest-coverage")

try

set(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")

Of course, compiler warnings are normally a good thing. If there is any practical option of fixing those warnings, it would be better in the long run.

Or you can disable warnings in the code itself. For example, if you want to silence GCC warnings in one file, you can use a #pragma :

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wall"
... // warnings are now disabled
#pragma GCC diagnostic pop

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