Objective-C composite object 合成物件問題
巴扎黑
巴扎黑 2017-04-24 09:12:38
0
2
413

最近看《Objective-C 程式設計》 ,11章有個合成物件

自己寫了一個例子 Xcode沒報錯,編譯時報錯了


//"Rectangle.h" @interface Rectangle : NSObject @property int width,height; -(void) setWidth: (int) w andHeight:(int) h; - (int) area; @end #import "Rectangle.h" @interface Square_c : NSObject { Rectangle *rect; } - (instancetype) init; - (instancetype) initWithSide:(int) s; - (void) setSide:(int) s; - (int) side; -(int) area; @end @implementation Square_c - (instancetype) init { self = [super init]; if (self) { rect = [[Rectangle alloc] init]; } return [self initWithSide:0]; } - (instancetype) initWithSide:(int) s { self = [super init]; if (self) [rect setWidth:s andHeight:s]; return self; } - (void) setSide:(int) s { [rect setWidth:s andHeight:s]; } - (int) side { return rect.width; } -(int) area { return [rect area]; } @end int main (int argc,char * argv[]) { @autoreleasepool { Square_c *square_c = [[Square_c alloc] initWithSide:20]; } }

Xcode編譯後報錯

Ld /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Products/Debug/prog1 normal x86_64
    cd /Project/prog1
    export MACOSX_DEPLOYMENT_TARGET=10.10
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Products/Debug -F/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Products/Debug -filelist /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/prog1.LinkFileList -mmacosx-version-min=10.10 -fobjc-arc -fobjc-link-runtime -Xlinker -dependency_info -Xlinker /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/prog1_dependency_info.dat -o /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Products/Debug/prog1

duplicate symbol _OBJC_IVAR_$_Square2.rect in:
    /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/main.o
    /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/Square_composite.o
duplicate symbol _OBJC_CLASS_$_Square2 in:
    /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/main.o
    /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/Square_composite.o
duplicate symbol _OBJC_METACLASS_$_Square2 in:
    /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/main.o
    /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/Square_composite.o
ld: 3 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

求熟手幫忙看下哪裡出問題了

巴扎黑
巴扎黑

全部回覆(2)
阿神

解決了,把composite的那個.m檔裡的@interface拆分出.h
main.m 裡再引入這個.h 就可以成功build了。

但是類別的分類 檔案只用.m 不分出.h 是可以build的

左手右手慢动作

error: linker command failed with exit code 1 (use -v to see invocation)
這個錯誤訊息的意思是link出錯了,如果你不詳細的出錯訊息貼出來的話,那麼它並沒有什麼卵用。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板