C语言 如何在 C 中抑制“未使用的参数”警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3599160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to suppress "unused parameter" warnings in C?
提问by nixgadget
For instance:
例如:
Bool NullFunc(const struct timespec *when, const char *who)
{
return TRUE;
}
In C++ I was able to put a /*...*/comment around the parameters. But not in C of course, where it gives me the error error: parameter name omitted.
在 C++ 中,我能够/*...*/在参数周围添加注释。但当然不是在 C 中,它给了我错误error: parameter name omitted。
回答by Job
I usually write a macro like this:
我通常写一个这样的宏:
#define UNUSED(x) (void)(x)
You can use this macro for all your unused parameters. (Note that this works on any compiler.)
您可以将此宏用于所有未使用的参数。(请注意,这适用于任何编译器。)
For example:
例如:
void f(int x) {
UNUSED(x);
...
}
回答by Philip Potter
In gcc, you can label the parameter with the unusedattribute.
在 gcc 中,您可以使用unused属性标记参数。
This attribute, attached to a variable, means that the variable is meant to be possiblyunused. GCC will not produce a warning for this variable.
附加到变量的此属性意味着该变量可能未使用。GCC 不会为此变量生成警告。
In practice this is accomplished by putting __attribute__ ((unused))just before the parameter. For example:
实际上,这是通过__attribute__ ((unused))在参数之前放置来实现的。例如:
void foo(workerid_t workerId) { }
becomes
变成
void foo(__attribute__((unused)) workerid_t workerId) { }
回答by ideasman42
You can use gcc/clang's unused attribute, however I use these macros in a header to avoid having gcc specific attributes all over the source, also having __attribute__everywhere is a bit verbose/ugly.
您可以使用 gcc/clang 的未使用属性,但是我在标头中使用这些宏以避免在源代码中包含 gcc 特定属性,而且__attribute__到处都有有点冗长/丑陋。
#ifdef __GNUC__
# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
# define UNUSED(x) UNUSED_ ## x
#endif
#ifdef __GNUC__
# define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
#else
# define UNUSED_FUNCTION(x) UNUSED_ ## x
#endif
Then you can do...
那么你可以做...
void foo(int UNUSED(bar)) { ... }
I prefer this because you get an error if you try use barin the code anywhere so you can't leave the attribute in by mistake.
我更喜欢这样,因为如果您尝试bar在代码中的任何地方使用都会出错,因此您不能错误地保留该属性。
and for functions...
和功能...
static void UNUSED_FUNCTION(foo)(int bar) { ... }
Note 1):
As far as I know, MSVC doesn't have an equivalent to __attribute__((__unused__)).
注 1):
据我所知,MSVC 没有相当于__attribute__((__unused__)).
Note 2):
The UNUSEDmacro won't work for arguments which contain parenthesis,
so if you have an argument like float (*coords)[3]you can'tdo,float UNUSED((*coords)[3])or float (*UNUSED(coords))[3], This is the only downside to the UNUSEDmacro I found so far, in these cases I fall back to (void)coords;
注2):
该UNUSED宏将用于包含括号,参数不工作,
所以如果你有一个参数一样float (*coords)[3],你不能做,float UNUSED((*coords)[3])或者float (*UNUSED(coords))[3],这是唯一的缺点UNUSED宏,我发现到目前为止,在这些情况下,我回落到(void)coords;
回答by Teddy
With gcc with the unused attribute:
使用带有未使用属性的 gcc:
int foo (__attribute__((unused)) int bar) {
return 0;
}
回答by Paul Hutchinson
Seeing that this is marked as gcc you can use the command line switch Wno-unused-parameter.
看到这被标记为 gcc 你可以使用命令行开关Wno-unused-parameter。
For example:
例如:
gcc -Wno-unused-parameter test.c
Of course this effects the whole file (and maybe project depending where you set the switch) but you don't have to change any code.
当然,这会影响整个文件(可能还会影响项目,具体取决于您设置开关的位置),但您不必更改任何代码。
回答by Calm
A gcc/g++ specific way to suppress the unused parameter warning for a block of source code is to enclose it with the following pragma statements:
抑制源代码块的未使用参数警告的 gcc/g++ 特定方法是将其包含在以下 pragma 语句中:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
<code with unused parameters here>
#pragma GCC diagnostic pop
回答by user2452561
Labelling the attribute is ideal way. MACRO leads to sometime confusion. and by using void(x),we are adding an overhead in processing.
标记属性是理想的方式。宏有时会导致混乱。通过使用 void(x),我们增加了处理开销。
If not using input argument, use
如果不使用输入参数,请使用
void foo(int __attribute__((unused))key)
{
}
If not using the variable defined inside the function
如果不使用函数内部定义的变量
void foo(int key)
{
int hash = 0;
int bkt __attribute__((unused)) = 0;
api_call(x, hash, bkt);
}
Now later using the hash variable for your logic but doesn't need bkt. define bkt as unused, otherwise compiler says'bkt set bt not used".
现在稍后将哈希变量用于您的逻辑,但不需要 bkt。将 bkt 定义为未使用,否则编译器会说“bkt set bt not used”。
NOTE: This is just to suppress the warning not for optimization.
注意:这只是为了抑制警告而不是为了优化。
回答by landerlyoung
I got the same problem. I used a third-part library. When I compile this library, the compiler (gcc/clang) will complain about unused variables.
我遇到了同样的问题。我使用了第三方库。当我编译这个库时,编译器 (gcc/clang) 会抱怨未使用的变量。
Like this
像这样
test.cpp:29:11: warning: variable 'magic' set but not used [-Wunused-but-set-variable] short magic[] = {
test.cpp:84:17: warning: unused variable 'before_write' [-Wunused-variable] int64_t before_write = Thread::currentTimeMillis();
test.cpp:29:11: 警告:变量 'magic' 设置但未使用 [-Wunused-but-set-variable] short magic[] = {
test.cpp:84:17: 警告:未使用的变量 'before_write' [-Wunused-variable] int64_t before_write = Thread::currentTimeMillis();
So the solution is pretty clear. Adding -Wno-unusedas gcc/clang CFLAG will suppress all "unused" warnings, even thought you have -Wallset.
所以解决方案很明确。添加-Wno-unused为 gcc/clang CFLAG 将抑制所有“未使用”警告,即使您已-Wall设置。
In this way, you DO NOT NEED to change any code.
这样,您无需更改任何代码。
回答by Bwana The Master
In MSVC to suppress a particular warning it is enough to specify the it's number to compiler as /wd#. My CMakeLists.txt contains such the block:
在 MSVC 中,为了抑制特定警告,将编译器的编号指定为 /wd# 就足够了。我的 CMakeLists.txt 包含这样的块:
If (MSVC)
Set (CMAKE_EXE_LINKER_FLAGS "$ {CMAKE_EXE_LINKER_FLAGS} / NODEFAULTLIB: LIBCMT")
Add_definitions (/W4 /wd4512 /wd4702 /wd4100 /wd4510 /wd4355 /wd4127)
Add_definitions (/D_CRT_SECURE_NO_WARNINGS)
Elseif (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUC)
Add_definitions (-Wall -W -pedantic)
Else ()
Message ("Unknown compiler")
Endif ()
Now I can not say what exactly /wd4512 /wd4702 /wd4100 /wd4510 /wd4355 /wd4127 mean, because I do not pay any attention to MSVC for three years, but they suppress superpedantic warnings that does not influence the result.
现在我不能确切地说出 /wd4512 /wd4702 /wd4100 /wd4510 /wd4355 /wd4127 是什么意思,因为我三年没有关注 MSVC,但是他们抑制了不影响结果的重复警告。
回答by Iustin
I've seen this style being used:
我见过这种风格被使用:
if (when || who || format || data || len);

