c - 链接器错误内联函数

标签 c gcc linker inline

我遇到了一些编译器/链接器错误,我不知道继续的正确方法是什么。我在这种情况下:

  • 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 offoo1' 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/

相关文章:

c - 如何使用 execvp——查找文件参数

c - 如何从C文件中获取所有指针变量

c++ - GCC 和 Clang 都不会通过编译时已知的函数指针数组进行内联调用——为什么?

c - "undefined reference to ` _cmocka_run_group_tests '"运行示例 CMocka 测试时

c - strstr() 在 C 中有大小限制吗?

c++ - 我应该从 main() 返回 EXIT_SUCCESS 还是 0?

linux - 在家里安装 gcc

linker - 由于 PIC,无法从汇编中的 x86-64 对象链接共享库

c++ - 单独文件中命名空间中的函数模板可以正常编译,但链接器找不到它

c++ - 无法打开包含文件 'thread'