C 和 C++; 中的内联函数消除编译器警告
我已经嵌入了 C 代码,我正在使用 C++ 框架对其进行单元测试。 C 和 C++ 处理内联函数的方式不同,因此当我想创建在两个源文件中使用的内联函数时,我会这样做:
在头文件中:
#ifdef __cplusplus
# define INLINE inline
#else
# ifndef INLINE
# define INLINE extern inline
# endif
#endif
INLINE Uint8 my_inline_function( Uint8 stuff )
{
return stuff; // not really, but it's not germane to the example
}
在两个 .c 文件之一中:
#define INLINE
现在 C 和 C++ 编译器都满意,但是当我构建时,我收到此警告:
In file included from ../MyFile.c:28:0,
from utest_MyFile.cpp:10:
../MyFile.h:53:0: warning: "INLINE" redefined
../MyFile.c:26:0: note: this is the location of the previous definition
有没有办法可以消除此警告?
I have embedded C code which I'm unit testing with a C++ framework. C and C++ handle inline functions differently, so when I want to create inline functions which are used in two source files, I do this:
In a header file:
#ifdef __cplusplus
# define INLINE inline
#else
# ifndef INLINE
# define INLINE extern inline
# endif
#endif
INLINE Uint8 my_inline_function( Uint8 stuff )
{
return stuff; // not really, but it's not germane to the example
}
In exactly one of the two .c files:
#define INLINE
Now both C and C++ compilers are satisfied, but when I build, I get this warning:
In file included from ../MyFile.c:28:0,
from utest_MyFile.cpp:10:
../MyFile.h:53:0: warning: "INLINE" redefined
../MyFile.c:26:0: note: this is the location of the previous definition
Is there a way I can silence this warning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用#ifndef
Use #ifndef
您可能在同一个翻译单元中多次包含定义。您可以添加 includeguards:
或取消定义指令:
更困难的方法是仅沉默警告:
但不确定这是否是跨平台的。
You're probably including the define multiple times in the same translation unit. You can add include guards:
or undefine the directive:
A harder approach would be to just silence the warning:
Not sure if this is cross-platform though.
首先,正如 Charles 在评论中所说,你不应该这样做,C 和 C++ 是本质上不同的语言。特别是它们的内联函数规则是不同的。它会给你带来痛苦。
然后,你还有另一个设计缺陷。这是显而易见的,因为您正在尝试重新定义宏。您的
INLINE
有两个不同的上下文,因此它们代表两个不同的事物。我认为以下模型更容易和直接:inline
。没有宏或类似的东西,并且extern
为同一函数放置“实例化”,您应该决定您的实例化是 C 还是 C++,不要在这里玩游戏。在 C 中这样的实例化是
(C 不调用该实例化,但我们只使用与 C++ 相同的术语)
在 C++ 中就是
这样,不需要魔法:
定义适用
将使用实例化
编辑:
看到您的评论(这并不能完全说服我)我认为您最好在头文件中使用一个用于实例化的宏
,然后在一个 .c 中使用或 .C 或任何你需要说服编译器的东西
First as Charles says in his comment, you shouldn't do this, C and C++ are substantially different languages. In particular their rules for
inline
functions are different. It will cause you pain.Then, you have another design flaw. This is apparent since you are trying to redefine a macro. You have two different contexts for your
INLINE
so these represent two different things. I think the following model is much easier and direct:inline
for the header files. no macro or stuff like that and noextern
you should decide if your instantiation is C or C++, don't play games here. In C such an instantiation is
(C doesn't call that instantiation but let's just use the same term as C++)
in C++ it would be
That's it, no need for magic:
definition available
instantiation will be used
Edit:
Seeing your comment (which doesn't convince me completely) I think you would be better off by just having one macro for the instantiation in a header file
and then in one .c or .C or whatever you will need to convince the compiler
您应该将
#define INLINE...
放在自己的头文件中,并带有自己的头保护:(inline.h)
然后您应该将
#include "inline.h"
> 靠近任何需要它的文件的顶部。You should have the
#define INLINE...
in its own header file with its own header guard:(inline.h)
You should then put
#include "inline.h"
near the top of any file that needs it.我很欣赏避免结合 C 和 C++ 的建议,但我们认为更严格的类型检查和更易于使用的单元测试框架的好处胜过这些问题。鉴于此,我发现最干净的方法是在 .c 文件中替换
为
I appreciate the advice to avoid combining C and C++, but we feel the benefits of stricter type checking and simpler-to-use unit test frameworks outweigh these hiccups. Given that, the approach I find cleanest is in the .c file replacing
with
我怀疑这就是导致问题的原因
尝试将其更改为
I suspect that this is what causing the problem
Try to change this to