简体   繁体   English

禁用来自 cmake 的谷歌测试的所有警告

[英]disable all warnings for google tests from cmake

I have a c++ project that I am compiling with cmake.我有一个 c++ 项目,我正在使用 cmake 进行编译。 I am also using google tests.我也在使用谷歌测试。 These are my cmake files:这些是我的 cmake 文件:

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 . tests.cpp 文件在使用make编译期间给出了一些警告。 For example about unused variables.例如关于未使用的变量。 I believe these are coming from gcc:我相信这些来自 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:我尝试在这里添加-w

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. -Wall是启用所有警告的编译器标志。 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 :例如,如果您想在一个文件中消除 GCC 警告,您可以使用#pragma

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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