在其中一个C源代码文件中,我找到了以下行(宏):
#define USE(x) (x) = (x)它是这样使用的:
int method(Obj *context)
{
USE(context);
return 1;
}在搜索它之后,我发现了以下描述:
//宏以消除一些编译器警告
你能告诉我更多关于这个宏的信息吗?
谢谢你的回答!
发布于 2010-11-15 07:43:02
有些编译器抱怨变量从来没有实际用于任何事情。例如:
int main(int argc, char **argv) {
return 0;
}给予:
Output from llvm C/C++/Fortran front-end (llvm-gcc)
/tmp/webcompile/_7618_1.c: In function 'main':
/tmp/webcompile/_7618_1.c:9: warning: unused parameter 'argc'
/tmp/webcompile/_7618_1.c:9: warning: unused parameter 'argv'奇怪的是,我可以使用宏消除这些警告:
#define USE(x) (x) = (x)
int main(int argc, char **argv) {
USE(argc); /* get rid of warnings */
USE(argv); /* get rid of warnings */
return 0;
}发布于 2010-11-15 08:13:48
对于gcc,您可以使用__attribute__((unused))来压制警告。
发布于 2010-11-15 07:43:48
如果在定义局部变量的函数中没有使用局部变量,那么大多数(如果不是全部)主要编译器都会提供警告。我设想宏对某个变量执行任意操作,以确保对该变量没有任何警告。
void func1(void)
{
int unusedVariable = 0;
/* do stuff without using unusedVariable */
} /* warning about not using unusedVariable */
void func2(void)
{
int unusedVariable = 0;
USE(unusedVariable);
/* do stuff without using unusedVariable */
} /* no warning is issued */https://stackoverflow.com/questions/4182342
复制相似问题