链接器错误内联函数 [英] Linker error inline function

查看:164
本文介绍了链接器错误内联函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些编译器/链接器错误,我不知道什么是正确的方法进行。我在这种情况下:


  • ah:在这个文件中定义了一个声明为inline的函数,例如:inline void foo1 ();

  • bh:在这个文件中定义了一个声明为inline的函数,它调用foo1():inline void foo2();
  • main.c:有一些foo1和foo2()的函数调用。



现在,如果我声明foo1和foo2在ah和bh作为extern inline void我得到了以下错误:

lock
prj / src / bo在函数 foo1 ':(.text + 0x0):
多重定义
foo1'
prj / src / main.o :( .text + 0x0):首先在这里定义make: em> *
[kernel]错误1


允许编译和链接的方式错误/警告在我描述的情况下? http://gcc.gnu.org/onlinedocs/gcc/Inline.html


当内联函数不是静态的时,编译器必须假定
可能有来自其他源文件的调用;由于全局符号
只能在任何程序中定义一次,所以函数不能在其他源文件中定义
,因此其中的调用不能被
集成。因此,非静态内联函数总是以通常的方式编译


换句话说,没有 static ,它会为你的内联函数发出一个符号。如果碰巧在一个头文件中定义该函数,并将其包含在多个编译单元中,那么最终会得到多个(重新定义的)符号。如果你想在头文件中包含定义,你应该使它 static 。


I got some compiler/linker errors and i don't know what is the correct method to proceed. I'm in this situation:

  • a.h: in this file is defined a function declared as "inline", for example: inline void foo1();
  • b.h: in this file is defined a function declared as "inline" that calls foo1(): inline void foo2();
  • main.c: there are some functions calls of both foo1 and foo2().

Now, if i declare foo1 and foo2 in a.h and b.h as extern inline void i got the following error:

prj/src/b.o: In function foo1': (.text+0x0): multiple definition offoo1' prj/src/main.o:(.text+0x0): first defined here make: * [kernel] Error 1

What is the way which allow to compile and link without errors/warning in the situation i described?

解决方案

From http://gcc.gnu.org/onlinedocs/gcc/Inline.html:

When an inline function is not static, then the compiler must assume that there may be calls from other source files; since a global symbol can be defined only once in any program, the function must not be defined in the other source files, so the calls therein cannot be integrated. Therefore, a non-static inline function is always compiled on its own in the usual fashion.

In other words, without static, it emits a symbol for your inline function. If you happen to define that function in a header and include it in more than one compilation unit, then you end up with multiple (redefined) symbols. If you want to include the definition in the header, you should make it static.

这篇关于链接器错误内联函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆