简单的C内联链接器错误 [英] Simple C inline linker error
问题描述
简单问题:
给出了以下程序:
#include <stdio.h>
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;
}
我收到链接器错误...
I get a linker error...
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)
似乎没有麻烦去编译它.
seems as though it doesn't bother to compile it.
根据我所读的内容,我认为它不必是static
:
链接器错误内联函数(因为它位于不同的对象中,并且处理2个定义而不是零)
it shouldn't need to be static
, I wouldn't think, based on what I have read in:
Linker error inline function (as this is in a different object, and dealing with 2 definitions rather than zero)
这是一个相关链接,但它是c ++,我认为在std C中将代码放在标头中不是一种好习惯:
This is a related link, but it is c++ and I don't think it is good practice in std C to put code in the header:
inline function linker error
编译器信息:
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段说:
Paragraph 7 of section 6.7.4 says:
任何具有内部链接的函数都可以是内联函数.对于具有外部链接的功能,适用以下限制:如果使用
inline
功能说明符声明了功能,则该功能也应在同一转换单元中进行定义.如果翻译单元中某个函数的所有文件范围声明都包含不带extern
的inline
函数说明符,则该翻译单元中的定义为内联定义. 内联定义不为函数提供外部定义,并且不禁止在另一个翻译单元中使用外部定义.内联定义提供了外部定义的替代方法,翻译器可使用该外部定义在同一翻译单元中实现对该函数的任何调用. 未指定对函数的调用是使用内联定义还是外部定义.
Any function with internal linkage can be an inline function. For a function with external linkage, the following restrictions apply: If a function is declared with an
inline
function specifier, then it shall also be defined in the same translation unit. If all of the file scope declarations for a function in a translation unit include theinline
function specifier withoutextern
, then the definition in that translation unit is an inline definition. An inline definition does not provide an external definition for the function, and does not forbid an external definition in another translation unit. An inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit. It is unspecified whether a call to the function uses the inline definition or the external definition.
您的文件不包含addEmUp
的外部定义,并且编译器选择在main
的调用中使用外部定义.
Your file does not contain an external definition of addEmUp
, and the compiler chose to use the external definition in the call in main
.
提供一个外部定义,或将其声明为static inline
.
Provide an external definition, or declare it as static inline
.
这篇关于简单的C内联链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!