xcode 内联函数的链接错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12844729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:47:16  来源:igfitidea点击:

Linking error for inline functions

objective-cxcodelinkerclangosx-mountain-lion

提问by alecail

I am trying to compile the sample code "SonofGrab" using XCode 4.5.1 on OS X 10.8.

我正在尝试在 OS X 10.8 上使用 XCode 4.5.1 编译示例代码“SonofGrab”。

One function is defined like this in controller.m

在 controller.m 中定义了一个函数

inline uint32_t ChangeBits(uint32_t currentBits, uint32_t flagsToChange, BOOL setFlags);

This leads to this error message:

这会导致此错误消息:

Undefined symbols for architecture x86_64:
"_ChangeBits", referenced from:
-[Controller awakeFromNib] in Controller.o
[...]
ld: symbol(s) not found for architecture x86_64

Removing the inlining of the function ChangeBits solves the problem, but why does the linker not find Changebits with the original definition ?

去掉函数 ChangeBits 的内联就解决了问题,但是为什么链接器找不到原始定义的 Changebits 呢?

回答by mattjgalloway

That to me, looks like a bug. This simple case exhibits the same error:

对我来说,这看起来像一个错误。这个简单的案例表现出相同的错误:

inline void foo() {}
int main() {
    foo();
}

Yields:

产量:

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

That has got to be wrong!? Unless I'm totally missing something about inline.

那一定是错的!?除非我完全遗漏了一些关于inline.

Edit:Oh no, wait, check out this - http://clang.llvm.org/compatibility.html#inline

编辑:哦不,等等,看看这个 - http://clang.llvm.org/compatibility.html#inline

Basically it appears I didn't understand inlinefully, either. And nor did the person writing that sample code at Apple!

基本上看来我也没有inline完全理解。在 Apple 编写示例代码的人也没有!

The inlineon the ChangeBitsfunction means that that definition is to be used only for inlining. Not that the function should always be inlined. There must be another, non-inline definition available elsewhere in the application otherwise it's illegal. Hence the link error as no non-inline ChangeBitsis provided.

inlineChangeBits仅用于内联功能是指该定义将被使用。并不是说该函数应该始终内联。在应用程序的其他地方必须有另一个非内联定义,否则它是非法的。因此没有提供非内联链接错误ChangeBits

The real solution is to declare ChangeBitsas static inlinesince that tells the compiler that the definition is local to that translation unit only and there does not therefore need to be a non-inline definition.

真正的解决方案是声明ChangeBitsasstatic inline因为它告诉编译器该定义仅针对该翻译单元是本地的,因此不需要非内联定义。

More information on the LLVM page I linked to, though. Hope that helps!

不过,有关我链接到的 LLVM 页面的更多信息。希望有帮助!