Objective C: Inline function - symbol not found

前端 未结 2 1870
猫巷女王i
猫巷女王i 2021-01-04 10:16

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 refe

相关标签:
2条回答
  • 2021-01-04 10:28

    just add FOUNDATION_EXPORT to function on the header (.h). Remember import Foundation/Foundation.h (OS X) or UIKit/UIKit.h (ios).

    Example:

    //.h
    #import <UIKit/UIKit.h>
    
    FOUNDATION_EXPORT inline NSString * myInlineFunction(void);
    
    0 讨论(0)
  • 2021-01-04 10:37

    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

    0 讨论(0)
提交回复
热议问题