C++ 禁止编译器警告函数声明从未被引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11124895/
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
Suppress Compiler warning Function declared never referenced
提问by Jtello
So i have some code like this:
所以我有一些这样的代码:
void foo (int, int);
void bar ( )
{
//Do Stuff
#if (IMPORTANT == 1)
foo (1, 2);
#endif
}
When doing a compile without "IMPORTANT" I get a compiler Warning that foo is defined and never referenced. Which got me thinking (that is the problem).
在没有“重要”的情况下进行编译时,我收到一个编译器警告,指出 foo 已定义且从未被引用。这让我思考(这就是问题所在)。
So to fix this i just added the same #if (IMPORTANT == 1)
around the function definition etc... to remove the warning, and then I started to wonder if there was a different way to suppress that warning on that function. I was looking at "unused" GCC attrib and didn't know if functions had the same attribute i could set? Is there even another way to suppress it that suppresses that warning for only that function and not the file?
所以为了解决这个问题,我只是#if (IMPORTANT == 1)
在函数定义等周围添加了相同的内容......以删除警告,然后我开始想知道是否有不同的方法来抑制该函数的警告。我正在查看“未使用”的 GCC 属性,但不知道函数是否具有我可以设置的相同属性?是否还有另一种方法来抑制它,只为该函数而不是文件抑制该警告?
回答by Jonathan Wakely
I'm fairly sure the relevant warning option is this one:
我相当确定相关的警告选项是这个:
-Wunused-function
Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall.
-Wunused-function
每当声明静态函数但未定义或未使用非内联静态函数时发出警告。此警告由-Wall启用。
So the warning should only be given for a static
function, interesting. Makes sense. If a function is static
it can only be used within the current file, so its definition must also be in this file.
所以警告应该只针对一个static
有趣的函数给出。说得通。如果一个函数是static
它只能在当前文件中使用,那么它的定义也必须在这个文件中。
And declaring it static inline
avoids the warning, without resorting to ugly macros or compiler-specific pragmas or attributes.
并声明它static inline
避免了警告,而无需求助于丑陋的宏或特定于编译器的编译指示或属性。
回答by Nawaz
...then I started to wonder if there was a different way to suppress that warning on that function.
...然后我开始怀疑是否有不同的方法来抑制该函数的警告。
There might be compiler option(s) to suppress this warning. However, one trick is this:
可能有编译器选项来抑制此警告。然而,一个技巧是这样的:
(void)foo; //cast it to void.
It should suppress this warning.
它应该抑制这个警告。
You could write a macro:
你可以写一个宏:
#define SUPPRESS_WARNING(a) (void)a
void foo(int thisIsAlsoAnUnsedParameter, int usedParameter)
{
SUPPRESS_WARNING(foo); //better do this inside the definition itself :D
SUPPRESS_WARNING(thisIsAlsoAnUnsedParameter);
}
As you can see, the definition of foo
itself suppresses the warning.
如您所见,foo
自身的定义抑制了警告。
回答by TartanLlama
In C++17 you can declare your function with [[maybe_unused]]
:
在 C++17 中,您可以使用以下命令声明您的函数[[maybe_unused]]
:
[[maybe_unused]] void foo (int, int);
This will suppress the warning and is the correct, idiomatic way to express a possibly unused function in C++17.
这将抑制警告,并且是在 C++17 中表达可能未使用的函数的正确惯用方式。
回答by David Hammen
One solution is via function attributes.
一种解决方案是通过函数属性。
void foo (int, int) __attribute__ ((unused));
This will tell gcc not to issue an unused function warning for the function foo
. If you're worried about portability, you can define a macro UNUSED_FUNCTION_ATTRIBUTE
that expands to __attribute__ ((unused))
with compilers that support attributes, but expands to nothing otherwise.
这将告诉 gcc 不要为函数发出未使用的函数警告foo
。如果您担心可移植性,您可以定义一个宏UNUSED_FUNCTION_ATTRIBUTE
,该宏扩展为__attribute__ ((unused))
支持属性的编译器,但在其他情况下扩展为空。
回答by Cheers and hth. - Alf
A good way to encapsulate compiler- and system-dependent stuff is to factor it out into headers. Then you adjust the include path depending on the compiler and system and perhaps other things. You can do the same for source code files.
封装编译器和系统相关内容的一个好方法是将其分解为头文件。然后根据编译器和系统以及可能的其他内容调整包含路径。您可以对源代码文件执行相同的操作。
In this case the declaration doesn't seem to depend on compiler- or system, so just add the following common header:
在这种情况下,声明似乎不依赖于编译器或系统,因此只需添加以下通用标头:
// [foo.h]
#pragma once
void foo( int, int );
With implementation file
带实现文件
// [foo.cpp]
#include <foo.virtual.cpp>
Then for the build where something should happen, add to the include path a directory containing
然后对于应该发生某些事情的构建,将包含的目录添加到包含路径
// [foo.virtual.cpp]
#include <foo.h>
void foo( int const a, int const b )
{
// Do the thing.
}
And for the build where nothing should happen, add to the include path a directory containing
对于不应该发生任何事情的构建,将包含的目录添加到包含路径
// [foo.virtual.cpp]
#include <foo.h>
void foo( int, int ) {}
If you are afraid that the call of an empty function will be very time consuming, like, a nano-second wasted, then simply move the definitions to headers and add the word inline
.
如果您担心调用空函数会非常耗时,例如浪费一纳秒,那么只需将定义移动到标题并添加单词inline
.
If foo
is also used for other purposes, define a function bar
that calls it for the should-or-should-not-happen thing, and do the above for bar
instead of for foo
.
如果foo
还用于其他目的,请定义一个函数bar
,为应该或不应该发生的事情调用它,并执行上述 forbar
而不是 for foo
。
Then, you have removed all the preprocessor stuff.
然后,您已经删除了所有预处理器的内容。
Remember that preprocessor directives in codeare ungood.
请记住,代码中的预处理器指令是不好的。
回答by Ameen
I find a way to do that globally and it works also in c
我找到了一种在全球范围内做到这一点的方法,它也适用于 c
#define SUPPRESS_UNUSED_WARN(var) \
int _dummy_tmp_##var = ((int)(var) & 0)
then you use it like:
然后你像这样使用它:
static int foo(int a, int b)
{
// ....
}
SUPRESS_UNUSED_WARN(foo);
- it can be used on functions and global variables
- it should be placed globally in order to work work
- it can't be used for local variables
- 它可以用于函数和全局变量
- 它应该被放置在全球范围内才能工作
- 它不能用于局部变量
回答by Amit
For ARM target platform while using, ARM compiler, Use the following compiler directive around the target function to suppress "Warning[Pe177]: function declared but never referenced" warning messages:
对于 ARM 目标平台,在使用 ARM 编译器时,请在目标函数周围使用以下编译器指令来抑制“警告 [Pe177]:函数已声明但从未被引用”警告消息:
#pragma diag_suppress=Pe177
void foo(void)
{
/* does something but is not being called for the current build */
}
回答by u11938675
#define SUPPRESS_UNUSED_WARN(var) \
int _dummy_tmp_##var = ((int)(var) & 0)
not work in IAR, change to this will work:
在 IAR 中不起作用,更改此将起作用:
#define SUPPRESS_UNUSED_WARN(var) \
void _dummy_tmp_##var(void) { (void)(var); }
回答by Shivaraj Bhat
You can also define _CRT_SECURE_NO_DEPRECATE macro in Visual studio project settings.
您还可以在 Visual Studio 项目设置中定义 _CRT_SECURE_NO_DEPRECATE 宏。
Goto project Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions
转到项目属性 -> 配置属性 -> C/C++ -> 预处理器 -> 预处理器定义
Add _CRT_SECURE_NO_DEPRECATE.
添加_CRT_SECURE_NO_DEPRECATE。
Thats it.!
就是这样。!