我遇到了一些编译器/链接器错误,我不知道继续的正确方法是什么。我在这种情况下:
- a.h:在这个文件中定义了一个声明为“inline”的函数,例如:inline void foo1();
- b.h: 在此文件中定义了一个声明为“inline”的函数,该函数调用 foo1():inline void foo2();
- main.c:有一些foo1和foo2()的函数调用。
现在,如果我将 a.h 和 b.h 中的 foo1 和 foo2 声明为 extern inline void,我会收到以下错误:
prj/src/b.o: In function
foo1': (.text+0x0): multiple definition of
foo1' prj/src/main.o:(.text+0x0): first defined here make: * [kernel] Error 1
在我描述的情况下,允许无错误/警告地编译和链接的方式是什么?
最佳答案
来自 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.
换句话说,如果没有 static
,它会为您的内联函数发出一个符号。如果您碰巧在 header 中定义该函数并将其包含在多个编译单元中,那么您最终会得到多个(重新定义的)符号。如果要在 header 中包含定义,则应将其设为 static
。
关于c - 链接器错误内联函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15013687/