我正在尝试在 Ubuntu 12 Linux 上编译 Objective-C 代码。
main.m 看起来像这样:
#import <Foundation/Foundation.h>
#import "CEFoo/CEFoo.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (@"hello world");
[pool drain];
return 0;
}
在 CEFoo.h 中,我有这样的定义:
struct StructName{ // line 86
BOOL first;
...
...
};
@interface StructName :NSObject // line 92
BOOL first; // line 93
...
...
@end // 96
当我去编译时
gcc main.m `gnustep-config --objc-flags` -lgnustep-base -o main.bin
我收到这条消息:
Foo/CEFoo.h:93:1: error: redefinition of ‘struct StructName’
Foo/CEFoo.h:86:8: note: originally defined here
我读到这可能是由于重新定义结构两次,或者在使用 include 而不是 import 时递归导入引起的。
grep -r "struct StructName" *
只显示一次定义。
我还搜索了项目中的每个 include 语句,并没有发现 include vs import 的明显用途,或者 CEFoo.h 的双重 include/imports(包含正在定义/导入的结构的文件 more不止一次)。
我怎样才能进一步查明造成这种情况的原因?我假设我导入了两次——如果我是,有没有办法通过第一次定义的详细或日志来观察它?
我可以做些什么来解决这个问题?还有其他想法吗?
TIA
最佳答案
定义一个类意味着为它创建一个结构,等等。而您碰巧有一个与您的类(class)完全一样的结构。例如,在 http://ideone.com/7kvFa 处检查以下代码“实时”
#import <objc/objc.h>
struct name {};
@interface name
@end
int
main() {
return 0;
}
关于objective-c - 错误 : redefinition of ‘struct StructName’ message when compiling in Objective-C on Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11426997/