由内联extern引起的重复符号

时间:2012-02-20 08:50:22

标签: objective-c import inline extern

FMParallaxChild.h

typedef struct {
    // ...
} FMParallaxSetting;

inline extern FMParallaxSetting FMParallaxSettingMake(CGPoint ratio, CGPoint startPos, CGPoint offset, CGPoint relVel, int zOrder);
inline extern FMParallaxSetting FMParallaxSettingMake(CGPoint ratio, CGPoint startPos, CGPoint offset, CGPoint relVel, int zOrder) {
    // ...
}

FMParallax.h

#import "FMParallaxChild.h"
....

MyController.h

#import "FMParallax.h"
....

AppDelegate.m

#import "MyController.h"
....

从这个简化的设置中我得到一个重复的符号链接器错误:

ld: duplicate symbol _FMParallaxSettingMake in MyController.o and AppDelegate.o

我找不到任何导入循环,我只在一个地方导入FMParallaxChild.h,所以我做错了什么?

1 个答案:

答案 0 :(得分:4)

将c或objc声明为static inline

static inline FMParallaxSetting
  FMParallaxSettingMake(CGPoint ratio,
                        CGPoint startPos,
                        CGPoint offset,
                        CGPoint relVel,
                        int zOrder) {
  // ...
}
对于c ++或objc ++,只需{p>或inline

inline FMParallaxSetting
  FMParallaxSettingMake(CGPoint ratio,
                        CGPoint startPos,
                        CGPoint offset,
                        CGPoint relVel,
                        int zOrder) {
  // ...
}

当然,使用c ++和objc ++,在这种情况下,您的程序将依赖于One Definition Rule(这是一个很好的默认值)。

问题是,它将被导出为每个可见的翻译(#include d)。