简单的C内联链接器错误

简单的问题:

给出以下程序:

#include  inline void addEmUp(int a, int b, int * result) { if (result) { *result = a+b; } } int main(int argc, const char * argv[]) { int i; addEmUp(1, 2, &i); return 0; } 

我收到链接器错误…

 Undefined symbols for architecture x86_64: _addEmUp", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 

看起来似乎没有费心去编译它。

根据我所读到的内容,它不应该是static ,我不会想到:
链接器错误内联函数 (因为它在不同的对象中,处理2个定义而不是零)

这是一个相关的链接,但它是c ++,我不认为在std C中将代码放在标题中是好的做法:
内联函数链接器错误

编译信息:

 cc --version Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn) Target: x86_64-apple-darwin12.3.0 Thread model: posix 

编译示例:

 # cc main.c Undefined symbols for architecture x86_64: "_addEmUp", referenced from: _main in main-sq3kr4.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocatio 

第6.7.4节第7段说:

具有内部链接的任何函数都可以是内联函数。 对于具有外部链接的函数,以下限制适用:如果使用inline函数说明符声明函数,则它也应在同一转换单元中定义。 如果转换单元中函数的所有文件范围声明都包含不带externinline函数说明符,则该转换单元中的定义内联定义内联定义不提供函数的外部定义,也不禁止另一个转换单元中的外部定义。 内联定义提供了外部定义的替代方案,翻译器可以使用该定义在同一翻译单元中实现对该function的任何调用。 未指定对函数的调用是使用内联定义还是使用外部定义

您的文件不包含addEmUp的外部定义,编译器选择在main中的调用中使用外部定义。

提供外部定义,或将其声明为static inline

尝试在编译器命令中添加“-O”选项。 仅在启用优化时才会启用内联。