gpt4 book ai didi

c++ - 将警告视为错误但忽略 googletest 中的警告

转载 作者:太空狗 更新时间:2023-10-29 21:09:58 28 4
gpt4 key购买 nike

我有一个 C++ 项目,我想在其中使用 -Wsign-conversion 编译选项。使用 Bazel 0.28.1 作为构建工具。我自己编写的代码不会生成那种类型的警告。该项目使用Googletest。不幸的是,Googletest 生成了这种警告,它破坏了我的构建。这是我的文件:

.bazelrc

build --cxxopt=-Werror           # Every warning is treated as an error.
build --cxxopt=-std=c++14
build --cxxopt=-Wsign-conversion # Warn for implicit conversions that may change the sign of an integer value

gtest.BUILD

cc_library(
name = "main",
srcs = glob(
["src/*.cc"],
exclude = ["src/gtest-all.cc"]
),
hdrs = glob([
"include/**/*.h",
"src/*.h"
]),
copts = ["-Iexternal/gtest/include"],
linkopts = ["-pthread"],
visibility = ["//visibility:public"],
)

工作区

workspace(name = "GTestDemo")

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
name = "googletest",
remote = "https://github.com/google/googletest",
#tag = "release-1.8.1",
commit = "2fe3bd994b3189899d93f1d5a881e725e046fdc2",
shallow_since = "1535728917 -0400",
)

构建

cc_test(
name = "tests",
srcs = ["test.cpp"],
copts = ["-Iexternal/gtest/include"],
deps = [
"@googletest//:gtest_main",
],
)

测试.cpp

#include <iostream>

#include "gtest/gtest.h"

TEST(sample_test_case, sample_test)
{
EXPECT_EQ(1, 1);
}

当我尝试构建代码时,出现以下错误:

Starting local Bazel server and connecting to it...
INFO: Analyzed target //:tests (21 packages loaded, 540 targets configured).
INFO: Found 1 target...
INFO: Deleting stale sandbox base /mnt/ramdisk/bazel-sandbox.be60b2910864108c1e29c6fce8ad6ea4
ERROR: /home/admin/.cache/bazel/_bazel_admin/cc9b56275ffa85d1a0fca263d1d708e4/external/googletest/BUILD.bazel:55:1: C++ compilation of rule '@googletest//:gtest' failed (Exit 1) gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 36 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
external/googletest/googletest/src/gtest-filepath.cc: In member function 'testing::internal::FilePath testing::internal::FilePath::RemoveFileName() const':
external/googletest/googletest/src/gtest-filepath.cc:168:45: error: conversion to 'std::__cxx11::basic_string<char>::size_type {aka long unsigned int}' from 'long int' may change the sign of the result [-Werror=sign-conversion]
dir = std::string(c_str(), last_sep + 1 - c_str());
~~~~~~~~~~~~~^~~~~~~~~
cc1plus: all warnings being treated as errors
Target //:tests failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 7.225s, Critical Path: 0.66s
INFO: 0 processes.
FAILED: Build did NOT complete successfully

Bazel 是否有可能忽略第 3 方库(例如 googletest)中的警告?

最佳答案

这更像是一个 GCC 问题。

“系统标题”are immune from this kind of thing :

-Wsystem-headers: Print warning messages for constructs found in system header files. Warnings from system headers are normally suppressed, on the assumption that they usually do not indicate real problems and would only make the compiler output harder to read.

因此,您可以使用 GCC's -isystem flag 将第 3 方库假装为系统 header 。 (而不是 -I):

copts = ["-isystem external/gtest/include"],

关于c++ - 将警告视为错误但忽略 googletest 中的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56431850/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com