2010-10-14 87 views
0

我試圖從http://www.otierney.net/objective-c.html編譯下面的例子。它由三個文件:在Linux上編譯目標c

Fraction.h

#import <Foundation/NSObject.h> 

@interface Fraction: NSObject { 
     int numerator; 
     int denominator; 
} 

-(void) print; 
-(void) setNumerator; 
-(void) setDenominator; 
-(int) numerator; 
-(int) denominator; 
@end 

Fraction.m

#import <stdio.h> 

@implementation Fraction 
-(void) print { 
     printf("%i/%i", numerator, denominator); 
} 

-(void) setNumerator: (int) n { 
     numerator = n; 
} 

-(void) setDenominator: (int) d { 
     denominator = d; 
} 

-(int) denominator { 
     return denominator; 
} 

-(int) numerator { 
     return numerator; 
} 
@end 

的main.m

#import <stdio.h> 
#import "Fraction.h" 

int 
main(int argc, char **argv) 
{ 
    //Create a new instance 
    Fraction *frac = [[Fraction alloc] init]; 

    //set the values 
    [frac setNumerator: 1]; 
    [frac setDenominator: 3]; 

    //print it 
    printf("The fraction is: "); 
    [frac print]; 
    printf("\n"); 

    //Frac memory 
    [frac release]; 
    return 0; 
} 

使用下列編譯

$ gcc `gnustep-config --objc-flags` -lobjc -lgnustep-base main.m -o main 

這將產生一個main.d文件,這只是ASCII

$ cat main.d 

main: main.m Fraction.h /usr/include/GNUstep/Foundation/NSObject.h \ 
/usr/include/GNUstep/Foundation/NSObjCRuntime.h \ 
/usr/include/GNUstep/GNUstepBase/GSVersionMacros.h \ 
/usr/include/GNUstep/GNUstepBase/GSConfig.h \ 
/usr/include/GNUstep/GNUstepBase/preface.h \ 
/usr/include/GNUstep/GNUstepBase/GSObjCRuntime.h \ 
/usr/include/GNUstep/ObjectiveC2/runtime.h \ 
/usr/include/GNUstep/ObjectiveC2/Availability.h \ 
/usr/include/GNUstep/Foundation/NSZone.h \ 
/usr/include/GNUstep/GNUstepBase/GNUstep.h \ 
/usr/include/GNUstep/Foundation/NSDate.h \ 
/usr/include/GNUstep/GNUstepBase/NSObject+GNUstepBase.h 

Fraction.h: 

/usr/include/GNUstep/Foundation/NSObject.h: 

/usr/include/GNUstep/Foundation/NSObjCRuntime.h: 

/usr/include/GNUstep/GNUstepBase/GSVersionMacros.h: 

/usr/include/GNUstep/GNUstepBase/GSConfig.h: 

/usr/include/GNUstep/GNUstepBase/preface.h: 

/usr/include/GNUstep/GNUstepBase/GSObjCRuntime.h: 

/usr/include/GNUstep/ObjectiveC2/runtime.h: 

/usr/include/GNUstep/ObjectiveC2/Availability.h: 

/usr/include/GNUstep/Foundation/NSZone.h: 

/usr/include/GNUstep/GNUstepBase/GNUstep.h: 

/usr/include/GNUstep/Foundation/NSDate.h: 

/usr/include/GNUstep/GNUstepBase/NSObject+GNUstepBase.h: 
+0

請重新格式化。點擊大橙色有說明嗎?就在編輯窗口的上方。 – Jonathan 2010-10-15 18:21:09

+0

問題是什麼? – mathk 2011-05-03 07:30:51

回答

1

我有你一個答案。以下是你需要什麼:
1.設置GNUstep的就是喜歡在這個網站:http://camelek.wikidot.com/wprowadzenie-do-objective-c
2.與內容創建GNUmakefile

include $(GNUSTEP_MAKEFILES)/common.make 
TOOL_NAME = Fraction 
$(TOOL_NAME)_OBJC_FILES = main.m Fraction.m 
include $(GNUSTEP_MAKEFILES)/tool.make 

這是你的編譯將會看起來怎麼樣,如何工作你的新計劃:

[email protected]:~/.../Fraction$ gs_make 

This is gnustep-make 2.6.2. Type 'make print-gnustep-make-help' for help. 
Making all for tool Fraction... 
Compiling file main.m ... 
Compiling file Fraction.m ... 
Linking tool Fraction ... 
[email protected]:~/.../Fraction$ file ./obj/Fraction 
./obj/Fraction: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0xd25682721d14fd4e0fb48bb999797da28d4f8940, not stripped 
[email protected]:~/.../Fraction$ ./obj/Fraction 
The fraction is: 1/3 
[email protected]:~/.../Fraction$ 

我使用Debian喘息7.1