gpt4 book ai didi

c++ - 为什么我会根据 header 顺序收到 C++ 编译警告

转载 作者:太空狗 更新时间:2023-10-29 20:55:04 25 4
gpt4 key购买 nike

我正在使用编译标志 -Wall -Wextra 和 -Werror。当我编译以下文件时,我收到大量“已声明‘静态’但从未定义 [-Werror=unused-function]”警告(视为错误)。当我颠倒#include 指令的顺序时没有这样的警告。请帮我理解为什么?

我知道我可以删除额外的警告和错误并编译我的程序,显然这不是我的意图,否则我的代码会更有趣。我正在尝试更深入地了解 C++,并通过清理代码中的警告来改善我的习惯。

我知道 argp 实际上是一个 C 库,而 iostream 是一个 C++ 库,也许这就是问题的一部分。我很乐意使用合适的 C++ 库来完成 argp 的功能,但我找不到。如果有的话,我很乐意听到。

#include <argp.h>
#include <iostream>

int main(int argc, char **argv)
{
return 0;
}

明确地说,我正在开发一个非常重要的程序,并且有特定的原因想要使用 C++ 而不是 C。我已经将此处显示的代码归结为尽可能少的代码以产生我的效果我试图理解。请不要暗示我不需要其中一个 header 。

编译器:gcc

    :~/scratch/argp_example$ gcc --version
gcc (Ubuntu 5.2.1-23ubuntu1~12.04) 5.2.1 20151031 Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

编译器调用:g++ -o obj/main.o -c src/main.cpp -Wall -Wextra -Werror -pedantic -MMD --std=c++11 -Iinc

具体的编译器反馈:

    In file included from /usr/include/x86_64-linux-gnu/c++/5/bits/gthr.h:148:0,
from /usr/include/c++/5/ext/atomicity.h:35,
from /usr/include/c++/5/bits/ios_base.h:39,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from src/main.cpp:2:
/usr/include/x86_64-linux-gnu/c++/5/bits/gthr-default.h:101:1: warning: ‘int __gthrw_pthread_once(pthread_once_t*, void (*)())’ declared ‘static’ but never defined [-Wunused-function] __gthrw(pthread_once) ^

gthr.h 中还有很多类似的错误。这个特定的复制/粘贴来自没有 -Werror 的运行,但这是唯一的区别。

解决方案:这是我选择的解决方案,但当然,您可以简单地颠倒包含的顺序。这是一个公认的错误,因此没有“正确”的答案,所有解决方案都是变通办法。我认为,这个最不可能让我或其他人以后感到不适。

#include <argp.h>
#undef __attributes__
#include <iostream>
...

最佳答案

这是一个 known bug .罪魁祸首是 argp.h 中的这段代码,它在您使用 -std=c++xx 时触发:

#ifndef __attribute__
/* This feature is available in gcc versions 2.5 and later. */
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
# define __attribute__(Spec) /* empty */
# endif

有争议的声明通常用 __attribute__ ((__weakref__("pthread_meow"))) 标记,但这个宏导致该属性消失。

在错误得到修复之前,您可能需要使用 -std=gnu++xx 进行编译,或者在包含 argp.h 之后手动使用 #undef __attribute__

关于c++ - 为什么我会根据 header 顺序收到 C++ 编译警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36960332/

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