c++ - clang 不会对 header 中的 "defined but not used"发出警告,而 gcc 会警告

标签 c++ gcc clang++

我发现 clang 和 gcc 在警告未使用的变量方面存在一些差异。

gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04)
clang version 6.0.0-1ubuntu2

在 foo.h 中

const int f = 3;

在 foo.cpp 中

#include "foo.h"

const int a = 2;

int main() {
    int i;
    return 0;
}

我有

$ clang -o foo foo.cpp -Wall -Wunused-variable -Wunused-const-variable
foo.cpp:7:9: warning: unused variable 'i' [-Wunused-variable]
    int i;
        ^
foo.cpp:4:11: warning: unused variable 'a' [-Wunused-const-variable]
const int a = 2;
          ^
2 warnings generated.

$ gcc -o foo foo.cpp -Wall -Wunused-variable -Wunused-const-variable
foo.cpp: In function ‘int main()’:
foo.cpp:7:9: warning: unused variable ‘i’ [-Wunused-variable]
     int i;
         ^
foo.cpp: At global scope:
foo.cpp:4:11: warning: ‘a’ defined but not used [-Wunused-const-variable=]
 const int a = 2;
           ^
In file included from foo.cpp:1:0:
foo.h:1:11: warning: ‘f’ defined but not used [-Wunused-const-variable=]
 const int f = 3;

我有几个问题:

为什么 gcc 提示 header 中的常量?在其中放置客户常量不是很常见吗?我怎样才能让 clang 表现得像 gcc 一样?

最佳答案

How I can make clang behave like gcc?

我认为只有报告这个令人惊讶的 clang 错误并等待修复。 (它仍然存在于 clang 7 中)。

您的foo.cpp定义的翻译单元必须与文件相同 通过预处理产生:

$ clang -E -P foo.cpp >foo.ii
$ cat foo.ii
const int f = 3;

const int a = 2;

int main() {
    int i;
    return 0;
}

与:

$ clang --version
clang version 6.0.1-svn330209-1~exp1~20180427232138.77 (branches/release_60)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

但是 clang 6 搞砸了:

$ clang -o foo foo.cpp -Wall -Wunused-variable -Wunused-const-variable
foo.cpp:6:9: warning: unused variable 'i' [-Wunused-variable]
    int i;
        ^
foo.cpp:3:11: warning: unused variable 'a' [-Wunused-const-variable]
const int a = 2;
          ^
2 warnings generated.

鉴于:

$ clang -o foo foo.ii -Wall -Wunused-variable -Wunused-const-variable
foo.ii:6:9: warning: unused variable 'i' [-Wunused-variable]
    int i;
        ^
foo.ii:1:11: warning: unused variable 'f' [-Wunused-const-variable]
const int f = 3;
          ^
foo.ii:3:11: warning: unused variable 'a' [-Wunused-const-variable]
const int a = 2;
          ^
3 warnings generated.

现在同意:

$ gcc -o foo foo.ii -Wall -Wunused-variable -Wunused-const-variable
foo.ii: In function ‘int main()’:
foo.ii:6:9: warning: unused variable ‘i’ [-Wunused-variable]
     int i;
         ^
foo.ii: At global scope:
foo.ii:3:11: warning: ‘a’ defined but not used [-Wunused-const-variable=]
 const int a = 2;
           ^
foo.ii:1:11: warning: ‘f’ defined but not used [-Wunused-const-variable=]
 const int f = 3;
           ^

稍后

why does the warning apply to constants in the header when this can be part of a library?

头文件(和)不是编译器可以处理的东西 认识。 预处理器通过以下方式识别头文件:

#include <headername>
...
#include "headername"

使用其指定或默认搜索路径(-I dir),预处理器 将 headername 解析为 /some/actual/headername 并粘贴内容 /some/actual/headername 代替翻译单元中的 #include 指令 由编译器消耗。该翻译单元不含所有预处理器 指令。编译器不消耗:

foo.cpp

#include "foo.h"

const int a = 2;

int main() {
    int i;
    return 0;

}

它消耗:

foo.ii

const int f = 3;

const int a = 2;

int main() {
    int i;
    return 0;
}

您观察到的 clang 行为意味着该工具在内部 虚拟化预处理编译之间的分界 - 这实际上是 C/C++ 实现中的常规历史实践 - 但引入了 这个错误出现在虚拟分界中。无论它对源代码做了什么 代码,它与首先对其进行预处理,然后编译它并不完全相同 预处理的输出; 应该

因此在头文件中定义常量并不是 C++ 实现的做法 可以提供任何特殊的慈善事业。如果您正在编写一个公开常量的库 在其 API header bar.h 中,并且您不希望该库的用户面临以下风险: 由于未能引用中定义的每个常量而发出未使用变量警告 bar.h 在每个包含 #include 的编译中,那么您将不会定义这些 常量只是bar.h作为 const 变量。您将执行其他三件事之一:

将常量定义为枚举枚举类的成员:

enum class E : int {
    F = 3
    //...
};

或者,bar.h 中声明常量extern,但在库源文件中定义它们< sup>1:

bar.h

#ifndef BAR_H
#define BAR_H

extern const int f;

#endif

bar.cpp

#include "bar.h"

const int f = 3;

或者,将常量定义为预处理器宏:

#define F 3

以老式的 C 方式。你不会,因为在 C++ 中我们避开预处理器 如果我们可以。


[1] extern 如何避免警告?因为 const 文件作用域变量是 在 C++ 中隐式地static(尽管在 C 中不是),并且编译器从不考虑 一个符合未使用诊断条件的extern变量,因为你告诉 该变量可以在提供给链接器的代码中引用 编译器看不到。

关于c++ - clang 不会对 header 中的 "defined but not used"发出警告,而 gcc 会警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53619461/

相关文章:

gcc - g++ 中 -lm 选项有什么作用?

c++11 统一初始化不适用于 "g++ -std=c++0x"

c++ - 如何检测椭圆是否与另一个椭圆/矩形碰撞

C++快速刷新桌面

c++ - 是否可以实例化具有已删除的构造函数和析构函数的非聚合类?

GCC:正在使用 C 标准

linux - 跨不同机器编译(使用不同的 gcc)

c++ - 使用自动时不检查数组边界

c++ - 为什么 clang 在每次使用时都取消引用参数?

c++ - 绑定(bind)类方法并将其作为函数指针传递