天天看点

windows下使用gnustep编译objective-c程序

主要来源于网络收集的,实验通过。

1. 搭建GNUStep

官方网址:http://www.gnustep.org/experience/Windows.html

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

主要的保有两个,一个是GNUstep System,其实就是MinGW和MSYS,一个是GNUstep Core,这才是我们需要GNUstep相关的东西。

装完后,在开始菜单里面,有一个GNUstep的菜单,点击shell就可以进入MSYS交互环境了。

2. 建立源文件和GNUmakefile

建立helloworld.m文件

内容为

#import <Foundation/Foundation.h>

int main(int argc, char**argv)

{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSLog(@”headfile dir is ok\n”);

[pool release];

return 0;

}

这个程序我们使用了Objective-C的Foundation库。

建立一个叫GNUmakefile的文件

(建立此文件可以使用make命令进行编译,不建立该文件需要输入gcc的编译指令)

文件内容

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME=MyHelloWorldExec <-目标文件名

Test_OBJC_FILES=MyHelloWorld.m <-源文件名

include $(GNUSTEP_MAKEFILES)/tool.make

3. 编译exe

(1)键入命令

gcc -o helloworld helloworld.m \

-fconstant-string-class=NSConstantString \

-I /GNUstep/System/Library/Headers/ \

-L /GNUstep/System/Library/Libraries/ \

-lobjc \

-lgnustep-base

(2)使用GNUmakefile文件

通过gnustep shell进入home目录make一下就行了

注意事项:

编译中提示提示

‘error: #error The current setting for native-objc-exceptions does not match that of gnustep-base … please correct this.’

解决方法:

在安装目录下找到GSConfig.h文件中的下面一行

#define BASE_NATIVE_OBJC_EXCEPTIONS 1

(1)使用gcc指令进行编译时

更改为

#define BASE_NATIVE_OBJC_EXCEPTIONS 0

(2)使用GNUmakefile文件

需要改为

#define BASE_NATIVE_OBJC_EXCEPTIONS 1

具体原因还没有发现。