在我声明内联函数后,什么可能再次导致多重定义错误?

发布于 2024-12-21 01:13:23 字数 197 浏览 1 评论 0原文

我有这个包含很多内联函数的头文件,编译后它说: 函数***的多个定义,函数如下所示:

inline int testab(int a, int b)
{
        return a>b;
}

在内联前面添加静态后,错误消失了。这是正确的做法吗?或者我错过了什么?我想我可以在这样的标题中设置内联函数。

I had this header file that contain a lot of inline functions,after compile it says:
multiple definition of function***,the function which looks like this:

inline int testab(int a, int b)
{
        return a>b;
}

after I add static in front of inline, the error is gone. Is it the right way to do it? Or am I missing something? I thought I could set up inline functions in a header like that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

丿*梦醉红颜 2024-12-28 01:13:23

无论是否内联,一旦标头被复制到至少两个文件中,您就无法再链接这些文件。

在标头中安全实现函数的唯一方法是使用static。这样,该函数的每个副本对于其他副本都是不可见的。

请注意,一起使用它们没有限制,因此您可以安全地编写:

static inline bool testab(int a, int b)
{
        return a>b;
}

编辑:更多详细信息

inline 告诉编译器认为该函数足够小,可以内联。也就是说,您告诉编译器您认为内联函数的额外空间并不重要,而不是它的(轻微)(可能)性能增益。然而,大多数编译器都足够聪明,可以根据自己的关键字和您的关键字来决定,它们只是倾向于更多地内联,而不一定总是听您的。当然,这取决于编译器。有些编译器可能完全忽略该关键字。

另一方面,static 意味着无论静态变量定义在什么范围内,它在外部都是不可见的。如果函数中有一个static变量,那么它在函数外部是不可见的。如果文件中有一个static变量(即静态全局变量),那么它在文件外部是不可见的,这意味着编译后该符号不存在,链接器会看到并感到困惑。这就是为什么,如果您编写的库中存在不应该在库外部可见的全局变量或函数,则应该将它们声明为static

编辑2:更正

显然,根据 this< /a> 答案,内联 函数不应为链接器导出其标识符。尽管如此,人们可以以任何一种方式将static附加到它上面,只是为了让它更清楚。同样显然,某些编译器无论如何都会导出标识符,因此在这些情况下 static 确实是必要的。

inline or not, once the header gets copied in at least two files, you cannot link the files anymore.

The only way you can safely implement a function in a header is to use static. This way, each copy of the function would be invisible to the other ones.

Note that there is no restriction to using them together, so you can safely write:

static inline bool testab(int a, int b)
{
        return a>b;
}

Edit: More details

inline tells the compiler that you think the function is small enough to be inlined. That is you tell the compiler that you don't think the extra space for inlining the function matters much as opposed to the (slight) (possible) performance gain for it. Most compilers however are intelligent enough to decide that on their own and with your keyword, they would just tend a bit more to inline, and not necessarily always listen to you. This depends on the compiler, of course. Some compilers may completely ignore the keyword.

static on the other hand, means that whatever scope a static variable is defined, it will be invisible outside it. If you have a static variable in a function, it would be invisible outside it. If you have a static variable in a file (that is, a static global variable), it will be invisible outside it, which means the symbol is not there after compiling for the linker to see and get confused. That is why, if you have written a library in which there are global variables or functions that are not supposed to be visible outside the library, you should declare them as static.

Edit 2: Correction

Apparently, according to this answer, inline functions should not have its identifiers exported for the linker. Nonetheless, one can attach static to it either way just to make it more clear. Also apparently, some compilers export the identifier anyway, so in those cases static is indeed necessary.

苍暮颜 2024-12-28 01:13:23

如果在头文件中定义函数,则需要指定static,否则会为该函数创建多个符号表定义(每个 .c 文件中都有一个),并且当您尝试链接各自的符号表时会发生冲突对象文件。
要在标头中定义内联函数,我相信您需要:

static inline int foo(int x) __attribute__((always_inline))
    {
    return x+1;
    }

但不确定这是否完全正确;请参阅: http://gcc.gnu.org/onlinedocs /gcc-4.5.0/gcc/Inline.html#Inline

You need to specify static if you define a function in a header file, otherwise multiple symbol table definitions are created for the function (one in each .c file) and will collide when you try to link the respective object files.
To define a inline function in a header, I belive you need:

static inline int foo(int x) __attribute__((always_inline))
    {
    return x+1;
    }

not sure if this is quite right though; see: http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Inline.html#Inline.

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