C 和 C++; 中的内联函数消除编译器警告

发布于 2024-12-22 03:31:43 字数 713 浏览 0 评论 0原文

我已经嵌入了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

亚希 2024-12-29 03:31:43

使用#ifndef

#ifndef INLINE
# ifdef __cplusplus
#  define INLINE inline
# else
#  define INLINE extern inline
# endif
#endif

Use #ifndef

#ifndef INLINE
# ifdef __cplusplus
#  define INLINE inline
# else
#  define INLINE extern inline
# endif
#endif
花期渐远 2024-12-29 03:31:43

您可能在同一个翻译单元中多次包含定义。您可以添加 includeguards:

#ifndef INLINE_DEFINED
#define INLINE_DEFINED

#ifdef __cplusplus
# define INLINE inline
#else
# ifndef INLINE
#  define INLINE extern inline
# endif
#endif

//...
#endif

或取消定义指令:

#undef INLINE

#ifdef __cplusplus
# define INLINE inline
#else
# ifndef INLINE
#  define INLINE extern inline
# endif
#endif

更困难的方法是仅沉默警告:

#pragma warning( disable : /*warning number*/ )

但不确定这是否是跨平台的。

You're probably including the define multiple times in the same translation unit. You can add include guards:

#ifndef INLINE_DEFINED
#define INLINE_DEFINED

#ifdef __cplusplus
# define INLINE inline
#else
# ifndef INLINE
#  define INLINE extern inline
# endif
#endif

//...
#endif

or undefine the directive:

#undef INLINE

#ifdef __cplusplus
# define INLINE inline
#else
# ifndef INLINE
#  define INLINE extern inline
# endif
#endif

A harder approach would be to just silence the warning:

#pragma warning( disable : /*warning number*/ )

Not sure if this is cross-platform though.

拔了角的鹿 2024-12-29 03:31:43

首先,正如 Charles 在评论中所说,你不应该这样做,C 和 C++ 是本质上不同的语言。特别是它们的内联函数规则是不同的。它会给你带来痛苦。

然后,你还有另一个设计缺陷。这是显而易见的,因为您正在尝试重新定义宏。您的 INLINE 有两个不同的上下文,因此它们代表两个不同的事物。我认为以下模型更容易和直接:

  • 对头文件使用 inline 。没有宏或类似的东西,并且
  • 在一个 C 或 C++ 文件中没有 extern 为同一函数放置“实例化”,

您应该决定您的实例化是 C 还是 C++,不要在这里玩游戏。在 C 中这样的实例化是

extern inline Uint8 my_inline_function( Uint8 stuff );

(C 不调用该实例化,但我们只使用与 C++ 相同的术语)

在 C++ 中就是

Uint8 my_inline_function( Uint8 stuff );

这样,不需要魔法:

  • 所有包含头文件的编译单元都会有一个
    定义适用
  • 于您仍然需要链接器符号的所有情况
    将使用实例化

编辑:

看到您的评论(这并不能完全说服我)我认为您最好在头文件中使用一个用于实例化的宏

#ifdef __cplusplus
# define INSTANT
#else
# define INSTANT extern inline
#endif

,然后在一个 .c 中使用或 .C 或任何你需要说服编译器的东西

INSTANT Uint8 my_inline_function( Uint8 stuff );

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:

  • use inline for the header files. no macro or stuff like that and no extern
  • in one C or C++ file place an "instantiation" for the same function

you should decide if your instantiation is C or C++, don't play games here. In C such an instantiation is

extern inline Uint8 my_inline_function( Uint8 stuff );

(C doesn't call that instantiation but let's just use the same term as C++)

in C++ it would be

Uint8 my_inline_function( Uint8 stuff );

That's it, no need for magic:

  • all compilation units that include the header file will have a
    definition available
  • for all situations where you'd still need the linker symbol the one
    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

#ifdef __cplusplus
# define INSTANT
#else
# define INSTANT extern inline
#endif

and then in one .c or .C or whatever you will need to convince the compiler

INSTANT Uint8 my_inline_function( Uint8 stuff );
三月梨花 2024-12-29 03:31:43

您应该将 #define INLINE... 放在自己的头文件中,并带有自己的头保护:

(inline.h)

#ifndef INLINE_DEFINED

#ifdef __cplusplus
# define INLINE inline
#else
# ifndef INLINE
#  define INLINE extern inline
# endif
#endif

#endif

然后您应该将 #include "inline.h" > 靠近任何需要它的文件的顶部。

You should have the #define INLINE... in its own header file with its own header guard:

(inline.h)

#ifndef INLINE_DEFINED

#ifdef __cplusplus
# define INLINE inline
#else
# ifndef INLINE
#  define INLINE extern inline
# endif
#endif

#endif

You should then put #include "inline.h" near the top of any file that needs it.

请帮我爱他 2024-12-29 03:31:43

我很欣赏避免结合 C 和 C++ 的建议,但我们认为更严格的类型检查和更易于使用的单元测试框架的好处胜过这些问题。鉴于此,我发现最干净的方法是在 .c 文件中替换

#define INLINE

#ifndef __cplusplus
# define INLINE
#endif

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

#define INLINE

with

#ifndef __cplusplus
# define INLINE
#endif
最冷一天 2024-12-29 03:31:43

我怀疑这就是导致问题的原因

#ifdef __cplusplus
# define INLINE inline

尝试将其更改为

#ifdef __cplusplus
# ifndef INLINE
# define INLINE inline
#endif

I suspect that this is what causing the problem

#ifdef __cplusplus
# define INLINE inline

Try to change this to

#ifdef __cplusplus
# ifndef INLINE
# define INLINE inline
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文