gcc - 2个版本,内联函数的不同处理
最近我在项目中遇到了一个问题。我通常在 gcc-4 中编译它,但是在尝试在 gcc-3 中编译之后,我注意到内联函数的处理方式有所不同。为了说明这一点,我创建了一个简单的示例:
main.c:
#include "header.h"
#include <stdio.h>
int main()
{
printf("f() %i\n", f());
return 0;
}
file.c:
#include "header.h"
int some_function()
{
return f();
}
header.h
inline int f()
{
return 2;
}
当我使用以下命令编译 gcc-3.4.6 中的代码时:
gcc main.c file.c -std=c99 -O2
我收到链接器错误(f 的多个定义),如果我删除 -O2
标志。我知道编译器如果不想内联任何内容,则不必内联任何内容,因此我假设它将 f 放在目标文件中,而不是在 main.c
和 的情况下内联它file.c
,因此多重定义错误。显然,我可以通过将 f
设为静态来解决此问题,然后在最坏的情况下,在二进制文件中包含一些 f
。
但我尝试在 gcc-4.3.5 中编译此代码:
gcc main.c file.c -std=c99 -O2
一切正常,所以我假设在两种情况下都使用较新的 gcc 内联 f
并且没有函数 f
完全在二进制文件中(在 gdb 中检查过,我是对的)。
但是,当我删除 -O2
标志时,我得到了两个对 int f()
的未定义引用。 在这里,我真的不明白发生了什么。看起来 gcc 假定 f
会被内联,因此它没有将其添加到目标文件中,但后来(因为没有 -O2
)它决定生成调用这些函数而不是内联,这就是链接器错误的来源。
现在的问题是:我应该如何定义和声明简单的小函数,我想要内联它们,以便它们可以在整个项目中使用,而不用担心在各种编译器中出现问题?让它们全部静态是正确的做法吗?或者也许 gcc-4 被破坏了,我不应该在几个翻译单元中拥有内联函数的多个定义,除非它们是静态的?
Recently I've come across a problem in my project. I normally compile it in gcc-4, but after trying to compile in gcc-3, I noticed a different treatment of inline functions. To illustrate this I've created a simple example:
main.c:
#include "header.h"
#include <stdio.h>
int main()
{
printf("f() %i\n", f());
return 0;
}
file.c:
#include "header.h"
int some_function()
{
return f();
}
header.h
inline int f()
{
return 2;
}
When I compile the code in gcc-3.4.6 with:
gcc main.c file.c -std=c99 -O2
I get linker error (multiple definition of f), the same if I remove the -O2
flag. I know the compiler does not have to inline anything if it doesn't want to, so I assumed it placed f in the object file instead of inlining it in case of both main.c
and file.c
, thus multiple definition error. Obviously I could fix this by making f
static, then, in the worst case, having a few f
's in the binary.
But I tried compiling this code in gcc-4.3.5 with:
gcc main.c file.c -std=c99 -O2
And everything worked fine, so I assumed the newer gcc inlined f
in both cases and there was no function f
in the binary at all (checked in gdb and I was right).
However, when I removed the -O2
flag, I got two undefined references to int f()
.
And here, I really don't understand what is happening. It seems like gcc assumed f
would be inlined, so it didn't add it to the object file, but later (because there was no -O2
) it decided to generate calls to these functions instead of inlining and that's where the linker error came from.
Now comes the question: how should I define and declare simple and small functions, which I want inline, so that they can be used throughout the project without the fear of problems in various compilers? And is making all of them static the right thing to do? Or maybe gcc-4 is broken and I should never have multiple definitions of inline functions in a few translation units unless they're static?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,从 gcc-4.3 开始,行为已经改变。 gcc 内联文档 (http://gcc.gnu.org/ onlinedocs/gcc-4.1.2/gcc/Inline.html)详细介绍了这一点。
短篇故事:普通内联仅用于告诉 gcc(无论如何在旧版本中)
来自同一文件范围的内联调用。然而,它并没有告诉 gcc
所有调用者都来自文件范围,因此 gcc 还保留可链接版本
的
f()
around: 这解释了上面的重复符号错误。Gcc 4.3 更改了此行为以与 c99 兼容。
并且,回答您的具体问题:
如果您想要跨 gcc 版本的可移植性,请使用静态内联。
Yes, the behavior has been changed from gcc-4.3 onwards. The gcc inline doc (http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Inline.html) details this.
Short story: plain inline only serves to tell gcc (in the old version anyway) to
inline calls to the from the same file scope. However, it does not tell gcc that
all callers would be from the file scope, thus gcc also keeps a linkable version
of
f()
around: which explains your duplicate symbols error above.Gcc 4.3 changed this behavior to be compatible with c99.
And, to answer your specific question:
If you want portability across gcc versions use static inline.