windows 下调试ObjectC

来源:互联网 发布:bv哪里买最便宜 知乎 编辑:程序博客网 时间:2024/05/22 09:26

转自:http://www.itivy.com/iphone/archive/2012/1/31/objective-c-windows-gnustep.html

1:到GNUstep官方网站上下载,三个软件包:GNUstep MSYS SystemGNUstep CoreGNUstep Devel。其中,前两个软件包是必须要安装的,第三个软件包是安装一些开发工具,比如:gcc、g++等,所以如果是学习Objective C的话,这个包也是必须要安装,

GNUstep就是提供类似Cocoa(苹果OS的开发框架)的API和工具,目前支持GNU/Linux and GNU/HURD, Solaris, NetBSD, OpenBSD, FreeBSD, Darwin和Windows,免费使用的。这个项目使Objective C能在多数流行平台上开发和运行。有关详细的介绍开始参考GNUstep的官方网站。

下载安装那三个package,可以,我试过!

http://www.gnustep.org/experience/Windows.html

下载上述软件包后,点击安装就可以了,不分顺序。然后,需要确认是否安装成功,并且是否能成功编译Objective C代码。

2:在“开始”菜单中“所有程序”下可以找到“GNUstep”->“shell”,就会出console窗口,可以试试一些Linux命令(ls,cd,mkdir等)。

#import <Foundation/Foundation.h>  

int main (int argc, const char *argv[]) {    

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

  NSLog(@"Hello World!");    

  [pool drain];    

  return 0; 

 }   

 在Windows环境下用文本编辑器(Editplus,UE等),编写上述代码,并且保存到GNUstep安装目录下的/home下,比如我把GNUstep安装在D:\AppleDevelop\下面,则你的文件应该放在GNUstep\msys\1.0\home\主机名 下面,具体路径可以在console下面运行pwd命令查看,取名为helloworld.m。在GNUstep的console窗口命令行下,

    1、cd /home

    2、gcc -o helloworld helloworld.m -I/GNUstep/System/Library/Headers -fconstant-string-class=NSConstantString -L/GNUstep/System/Library/Libraries -lobjc -lgnustep-base

    3、运行helloworld.exe

   说明:第二步中的一些参数明说,如果熟悉Linux/Unix下C/C++编译的话,上述参数应该很熟悉,-I表示头文件查找的路径,-L表示库文件查找路径,-l表示需要链接的库文件。但是,-fconstant-string-class=NSConstantString  对于这个参数可能比较陌生,这个参数主要是指定常量字符串所使用的class。  

自己写了一个简单的脚本,要是嫌编译源代码麻烦,可以建一个文件,比如lc.sh,然后把下面的内容复制进去:

#!/bin/sh

gcc -o $1 $2 -I/GNUstep/System/Library/Headers -fconstant-string-class=NSConstantString -L/GNUstep/System/Library/Libraries -lobjc -lgnustep-base

然后在console下面运行如下命令:chmod +x lc.sh

以后要编译程序的时候,就在命令行下面输入:./lc.sh helloworld helloworld.m

文件中的$1和$2分别表示命令行中的helloworld 和 helloworld.m

    helloworld.exe编译并运行成功的话,说明windows下Objective C开发环境就搭建好了,这样就可以开始以廉价方式的学习Objective C。


按上述情况安装后,发生问题:


Objective-C代码,文件名main.m(m是扩展名):

#import <Foundation/Foundation.h>int main(int arvc, char* argv[]) {NSLog(@"Hello world!http://ju2ender.cnblogs.com");return 0;}

在shell中输入如下命令出错:

gcc -o main main.m -I/GNUstep/System/Library/Headers/ -fconstant-string-class=NSConstantString -L/GNUstep/System/Library/Libraries -lobjc -lgnustep-base

C:/GNUstep/GNUstep/System/Library/Headers/Foundation/NSException.h:44:2: error:
#error The current setting for native-objc-exceptions does not match that of gnu
step-base ... please correct this.

说现在对native-objc-exceptions的设置与gnustep-base不匹配,native-objc-exceptions、gnustep-base是什么东东?又要在哪里设置呢?

遇到问题我们一定不要灰心、害怕,要坚信没有解决不了的问题!

问题解决

我们要在C:\GNUstep\GNUstep\System\Library\Headers\GNUstepBase(请根据你的安装路径自行修改)中找到GSConfig.h文件:

用记事本、EditPlus、Notepad++等等打开,找到BASE_NATIVE_OBJC_EXCEPTIONS(没错,这就是上面的native-objc-exceptions),将其键值由1改为0:

保存并关闭。这时再次在shell中输入:

gcc -o main main.m -I/GNUstep/System/Library/Headers/ -fconstant-string-class=NSConstantString -L/GNUstep/System/Library/Libraries -lobjc -lgnustep-base

没有看到错误提示,哈哈,好像成功了,再输入ls命令看到生成了main.exe,打开home目录也确实生成了:

没错,我们看到又出错了!我们执行main.exe,它却说命令没有找到。。。

没事儿,很好解决,我们在前面加上./,如下:

./main.exe

这样就成功了!如上图,输入./main也是可行的。




原创粉丝点击