How to suppress “unused parameter” warnings in C?

2020-01-23 15:24发布

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.

11条回答
叛逆
2楼-- · 2020-01-23 15:54

With gcc with the unused attribute:

int foo (__attribute__((unused)) int bar) {
    return 0;
}
查看更多
祖国的老花朵
3楼-- · 2020-01-23 15:54

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.

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();

So the solution is pretty clear. Adding -Wno-unused as gcc/clang CFLAG will suppress all "unused" warnings, even thought you have -Wall set.

In this way, you DO NOT NEED to change any code.

查看更多
三岁会撩人
4楼-- · 2020-01-23 16:00

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);
    ...
}
查看更多
对你真心纯属浪费
5楼-- · 2020-01-23 16:00

Seeing that this is marked as gcc you can use the command line switch 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.

查看更多
爷、活的狠高调
6楼-- · 2020-01-23 16:03

In gcc, you can label the parameter with the unused attribute.

This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC will not produce a warning for this variable.

In practice this is accomplished by putting __attribute__ ((unused)) just before the parameter. For example:

void foo(workerid_t workerId) { }

becomes

void foo(__attribute__((unused)) workerid_t workerId) { }
查看更多
登录 后发表回答