我在 clang 和 gcc 警告未使用的变量的方式上遇到了一些差异。
gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04)
clang version 6.0.0-1ubuntu2
在福·
const int f = 3;
在福.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 抱怨标题中的常量?把客户的常量放进去不是很常见吗?我怎样才能让叮当表现得像 gcc?
我怎样才能让 clang 表现得像 gcc?
我认为只有报告这个令人惊讶的叮当错误并等待修复。(它仍然存在于叮当 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
但是叮当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;
^
后
为什么警告适用于标头中的常量,而它可以是库的一部分?
头文件(和库)不是编译器的东西 承认。预处理器通过以下方式识别头文件:
#include <headername>
...
#include "headername"
使用其指定或默认的搜索路径 (-I dir
),预处理器 将headername
解析为/some/actual/headername
并粘贴内容 的/some/actual/headername
,代替编译器使用的翻译单元中的#include
指令。该翻译单元没有任何预处理器 指令。编译器不使用:
傅.cpp
#include "foo.h"
const int a = 2;
int main() {
int i;
return 0;
}
它消耗:
福二
const int f = 3;
const int a = 2;
int main() {
int i;
return 0;
}
您观察到的叮当行为意味着,在内部,该工具 虚拟化预处理和编译之间的分界线 - 这实际上是 C/C++ 实现中的常规历史实践 - 但引入了 虚拟分界中的此错误。无论它真正对源做什么 代码,它与先预处理它,然后编译 预处理输出;应该是。
因此,在头文件中定义常量不是C++实现的做法 可以扩展任何特殊的慈善事业。如果您正在编写公开常量的库 在其 API 标头中,bar.h
,并且您不希望该库的用户面临以下风险: 未使用的变量警告,因为无法引用 中定义的每个常量bar.h
在每个#include
它的编译中,那么您将不会定义这些 常量就像bar.h
中的const
变量一样。您将执行以下三项操作之一:
将常量定义为enum
或enum class
的成员:
enum class E : int {
F = 3
//...
};
或者,声明常量extern
bar.h
但在库源文件1中定义它们:
酒吧.H
#ifndef BAR_H
#define BAR_H
extern const int f;
#endif
酒吧.cpp
#include "bar.h"
const int f = 3;
或者,将常量定义为预处理器宏:
#define F 3
以老派的C方式。你不会,因为C++我们避开了预处理器 如果可以的话。
[1]
extern
如何避免警告?因为const
文件范围变量是 隐式static
C++(尽管不是 C 语言),编译器从不考虑 符合未使用诊断条件的extern
变量,因为您正在告诉 可以在提供给链接器的代码中引用变量 编译器看不到的。