如何在Windows上编译Objective-C

Objective-C如今几乎已经变成了苹果的专利了,能够直接在苹果的Xcode上编译Objective-C程序,可是在Windows平台下的编译工具就寥寥无几了,自己这种语言用的人就不是不少。今天在网上忽然看到了有人发帖,能够在Windows平台下编译Objective-C,就抱着好奇的心态试了试。没想到,竟然成功了,如今就把怎样搭建Objective-C编译平台的经验拿出来和你们分享。php

一、安装GNUstephtml

GNUstep Windows Installer提供了Windows平台下的Object-C的模拟开发环境,一共有四个软件包,其中GNUstep System和GNUstep Core是必装的,GNUstep Devel和Cairo Backend是选装的。只安装前两个就够了。objective-c

二、编写Objective-C代码shell

安装完成后,在开始菜单里的GNUstep选项里执行shell,就能打开命令行。直接在Windows里进入C:/GNUstep/home/Administrator(个人是Administrator,可能有的不同)目录,在这里用你喜欢的工具(如今UltraEdit和Notepad++编辑器好像能够代码高亮)编写Object-C程序。
如:HelloWorld.mwindows

#import <Foundation/Foundation.h> 
   
 int main (int argc, const char *argv[]) {  
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
    NSLog(@"Hello World!");  
    [pool drain];  
 
    return 0;  
}

三、配置环境变量iphone

这一步很重要。GNUstep.sh是用来设置GNUstep开发环境变量的,若是没有执行,就会有不少头文件,库文件,命令找不到编辑器

在一个目录里写好了源代码之后,编写一个make配置文件,名字必须叫GNUmakefile,内容是wordpress

include $(GNUSTEP_MAKEFILES)/common.make  
TOOL_NAME=Test 
Test_OBJC_FILES=HelloWorld.m  
include $(GNUSTEP_MAKEFILES)/tool.make

能够修改上面的黑体部分工具

而后就是学习

make


命令运行成功就能够看到新增了一个obj目录,里面就有你要的可执行文件和.o文件。

OK 搞定了。

小结:如何在Windows编译Objective-C的内容介绍完了,但愿本文对你有所帮助!

 

1.下载GNUStep

http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/

下载

gnustep-msys-system-x.x.x-setup.exe

gnustep-core-x.x.x-setup.exe

gnustep-cairo-x.x.x-setup.exe

gnustep-devel-x.x.x-setup.exe

将下载的GNUStep安装,好比C:\GNUStep

2. 下载JEdit

http://www.jedit.org/index.php?page=download

JEdit 是Freeware,能够用来编辑 .m 文件 .m 是Object C缺省后缀。 .m 至关于 .c 文件

3. 一个Object C教材

http://www.otierney.net/objective-c.html

———————————————

4. 安装后,执行msys.bat 启动 GNUStep 环境 (类Linux环境)

5. 编写示例程序

fraction.h

#import <Foundation/NSObject.h>

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

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

fraction.m

#import "fraction.h"
 #import 

@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 
#import "fraction.h"

int main( int argc, const 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" );

    // free memory
     [frac release];

    return 0;
 }

6. 编写Makefile

在当前目录下建立GNUmakefile

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = Hello
 Hello_OBJC_FILES = main.m fraction.m

include $(GNUSTEP_MAKEFILES)/tool.make

6. 编译程序

$ make

将建立 obj目录 运行  hello.exe

The fraction is: 1/3

这样环境就搭好了,你就能够继续学习 Object C了

最终写iphone程序通常仍是要Mac OS.

--------------------------------------------------------------------

http://mobile.51cto.com/iphone-281182.htm

http://www.imobilebbs.com/wordpress/?p=3081

相关文章
相关标签/搜索