在Linux上编译目标c

时间:2010-10-14 18:29:37

标签: objective-c linux

我正在尝试从http://www.otierney.net/objective-c.html编译以下示例。它由三个文件组成:

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:

1 个答案:

答案 0 :(得分: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

以下是您的编辑方式,以及新程序的工作原理:

camelek@debian:~/.../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 ...
camelek@debian:~/.../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
camelek@debian:~/.../Fraction$ ./obj/Fraction
The fraction is: 1/3
camelek@debian:~/.../Fraction$ 

我正在使用Debian Wheezy 7.1