目标C:内联函数 - 找不到符号 [英] Objective C: Inline function - symbol not found

查看:881
本文介绍了目标C:内联函数 - 找不到符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库,它使用内联C函数,
和编译只是罚款。
当另一个库引用该库时,它仍然编译良好。
但是如果一个实际的应用引用库,它无法编译,说_functionNameHere(是的,函数名以下划线开头) - 符号未找到。



如果我删除内联说明符,它都编译正常。但是只要函数在库内部是内联的,应用程序就不能编译!



有什么想法为什么?



我非常了解编译器,但是对Objective-C来说是新的,我知道的任何东西只不过是有教育意义的猜测。即使这样,我不能想到一个原因,它会这样的行为。



事情是,内联函数是INLINE,这意味着没有符号所有,它被编译inline无论它被调用,并且引用应用程序不应该甚至不知道它曾经存在。
这是真的,只要内联函数总是可内联的,并且是库的私有,没有特殊的编译器配置阻止内联,所有这些都是真的。



所以,任何人都请来指点我在做什么/想错了...



谢谢: - )

解决方案

如果函数仅适用于内联函数定义,则应将 static 相同的翻译单元(内部链接),

  static inline int add(int x,int y){return x + y;} 
int s = add(1,2);

如果在上面的示例中省略了 static 没有执行优化,并且没有执行内联),链接器将退出,错误抱怨_add被引用,但没有找到符号。

$ c
$ b

在C99中,没有任何存储说明符的函数声明将不会生成实际的可调用函数体,因此链接器在需要时会产生错误。



如果您的内联函数可能被其他模块,它更复杂。一个常见的做法是在头文件中包含内联函数定义(无extern),并在正好一个模块中包含
,只有一个extern的原型声明。



在C99中解释内联函数的好文章: http://www.drdobbs.com/184401540 p>

I have a library which uses an inline C function, and is compiling just fine. When another library references that library, it still compiles fine. But if an actual app references the library, it fails to compile, saying that the _functionNameHere (yes, function name beginning with underline) - symbol is not found.

If I remove the inline specifier, it all compiles fine. But as long as a function is inline inside the library, the app can't compile!

Any ideas as to why that is?

I know pretty much about compilers, but am new to the Objective-C, and anything I know about it is nothing more than educated guesses. And even that way I can't think of a reason that it will behave that way.

The thing is, the inline function is INLINE, which means there's no symbol at all, it is compiled inline wherever it is called, and the referencing app should not even know it ever existed. That's true as long as the inline function is always inline-able, and is private to the library, and there's no special compiler configuration which prevents inlining, and all of those are true.

So, anyone please come and point the finger at what I'm doing/thinking wrong...

Thanks :-)

解决方案

You should add the static specifier to your inline function definition if the function is intended for use only in the same translation unit (internal linkage),

static inline int add(int x, int y) {return x+y;}
int s = add(1, 2);

If you omit static in the example above (in Debug configuration, where no optimization is performed and no inlining is carried out), the linker will exit with error complaining _add is referenced but Symbol(s) not found.

In C99, an inline function declaration without any storage specifier will not generate an actual callable function body so the linker gives an error when it needs one.

If your inline function may be used by other modules, it's more complicated. A common practice is to include the inline function definition (without extern) in the header file and include exactly one prototype declaration with extern in exactly one module.

A good article explaining inline functions in C99: http://www.drdobbs.com/184401540

这篇关于目标C:内联函数 - 找不到符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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