内联函数的链接错误 [英] Linking error for inline functions

查看:800
本文介绍了内联函数的链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在OS X 10.8上使用XCode 4.5.1编译示例代码SonofGrab。

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

一个函数在controller.m中定义如下: / p>

One function is defined like this in 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

删除函数的内联ChangeBits解决问题,但为什么链接器找不到原始定义的Changebits?

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

推荐答案

。这个简单的例子显示相同的错误:

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

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

产生:

$ 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)

这必须是错的!除非我完全错过了 inline

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

编辑等待,请查看此 - http://clang.llvm.org/compatibility.html#inline

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

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

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

上的 inline ChangeBits 函数意味着该定义仅用于内联。不是函数应该始终内联。在应用程序的其他地方必须有另一个非内联定义,否则它是非法的。因此,提供了链接错误,因为没有非内联 ChangeBits

The inline on the ChangeBits function 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 ChangeBits is provided.

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

The real solution is to declare ChangeBits as static inline since 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.

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

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

这篇关于内联函数的链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆