clang 不会对 header 中的 "defined but not used" 发出警告,gcc 会
clang does not warn for "defined but not used" in header, gcc does
我发现 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
定义的翻译单元必须与文件相同
由 pre-processing 制作:
$ 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?
header 文件(和库)不是编译器的东西
认识。 预处理器 识别 header 个文件,通过:
#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++ 实现中的例行历史实践 - 但引入了
虚拟分界中的此错误。无论它真正对源做什么
代码,它与先预处理它,然后编译
预处理的输出; 应该是.
所以在header文件中定义常量不是C++实现的实践
可以扩展任何特殊的慈善机构。如果您正在编写一个公开常量的库
在其 API header、bar.h
中,并且您不希望该库的用户面临以下风险
unused-variable 因未能引用中定义的每个常量而发出警告
bar.h
在每个 #include
的编译中,那么你 将不会 定义那些
常量只是 作为 const
bar.h
中的变量。您将做其他三件事之一:
将常量定义为 enum
或 enum class
的成员:
enum class E : int {
F = 3
//...
};
或者,在bar.h
中声明常量extern
,但在库源文件中定义它们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
在 old-school C 方式中。你不会的,因为在 C++ 中我们避开了预处理器
如果我们可以。
[1] extern
如何避免警告?因为 const
文件范围变量是
在 C++ 中隐式地 static
(虽然不是在 C 中),并且编译器从不考虑
一个符合 unused 诊断条件的 extern
变量,因为你告诉
它可以在提供给链接器的代码中引用该变量
编译器看不到。
我发现 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
定义的翻译单元必须与文件相同
由 pre-processing 制作:
$ 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?
header 文件(和库)不是编译器的东西 认识。 预处理器 识别 header 个文件,通过:
#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++ 实现中的例行历史实践 - 但引入了 虚拟分界中的此错误。无论它真正对源做什么 代码,它与先预处理它,然后编译 预处理的输出; 应该是.
所以在header文件中定义常量不是C++实现的实践
可以扩展任何特殊的慈善机构。如果您正在编写一个公开常量的库
在其 API header、bar.h
中,并且您不希望该库的用户面临以下风险
unused-variable 因未能引用中定义的每个常量而发出警告
bar.h
在每个 #include
的编译中,那么你 将不会 定义那些
常量只是 作为 const
bar.h
中的变量。您将做其他三件事之一:
将常量定义为 enum
或 enum class
的成员:
enum class E : int {
F = 3
//...
};
或者,在bar.h
中声明常量extern
,但在库源文件中定义它们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
在 old-school C 方式中。你不会的,因为在 C++ 中我们避开了预处理器 如果我们可以。
[1]
extern
如何避免警告?因为 const
文件范围变量是
在 C++ 中隐式地 static
(虽然不是在 C 中),并且编译器从不考虑
一个符合 unused 诊断条件的 extern
变量,因为你告诉
它可以在提供给链接器的代码中引用该变量
编译器看不到。