windows 下调试ObjectC

来源:互联网 发布:穷人突破阶级知乎 编辑:程序博客网 时间:2024/05/22 02:00

windows 下调试ObjectC


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

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


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等)。


编写一个Objective C代码进行编译运行测试。在这里就用经典的“hello world”来说明:

#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下.取名为helloworld.m.

具体路径可以在console下面运行pwd命令查看。

我的路径为C:\Documents and Settings\Administrator在下建立home目录


在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


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

注意:helloworld.m必须出现在-lobjc和-lgnustep-base的前面,否则会出错。

此时会出现一些info提示信息,不过不碍事,终于成功了生成了可执行文件,执行./helloworld.exe看结果。


    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是扩展名):

[cpp] view plaincopy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. int main(int arvc, char* argv[]) {  
  4.     NSLog(@"Hello world!http://ju2ender.cnblogs.com");  
  5.     return 0;  
  6. }  

在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也是可行的。



下面是我收集的仅限学习使用:

例子1:

hello.m的源码:
                 

C代码  收藏代码
  1.     
  2. #import <stdio.h>  
  3.    int main(int argc,const char *argv[]){  
  4.    printf("hello world/n");  
  5.    return 0;  
  6.    }  

   
    编译运行:

yaoming168@coco ~
$ gcc hello.m

yaoming168@coco ~
$ ls
a.exe  hello.m

yaoming168@coco ~
$ ./a.exe
hello world

yaoming168@coco ~

例子2:

代码:  包含3个文件。
1) Fraction.h:
C代码  收藏代码
  1. #import <Foundation/NSObject.h>  
  2.   
  3. @interface Fraction: NSObject {  
  4.     int numerator;  
  5.     int denominator;  
  6. }  
  7.   
  8. -(void) print;  
  9. -(void) setNumerator: (int) d;  
  10. -(void) setDenominator: (int) d;  
  11. -(int) numerator;  
  12. -(int) denominator;  
  13. -(void) setNumerator: (int) n ddd: (int)d;  
  14. -(void) setNumerator: (int)n : (int)d :(int) a;  
  15. // 这里,有3个setNumerator函数, 是重载。   
  16. @end  


2)Fraction.m
C代码  收藏代码
  1. #import "Fraction.h"  
  2. #import <stdio.h>  
  3.   
  4. @implementation Fraction  
  5. -(void) print {  
  6.     printf( "%i/%i", numerator, denominator );  
  7. }  
  8.   
  9. -(void) setNumerator: (int) n {  
  10.     numerator = n;  
  11. }  
  12.   
  13. -(void) setDenominator: (int) d {  
  14.     denominator = d;  
  15. }  
  16.   
  17. -(int) denominator {  
  18.     return denominator;  
  19. }  
  20.   
  21. -(int) numerator {  
  22.     return numerator;  
  23. }  
  24.   
  25. -(void) setNumerator: (int) n ddd: (int)d {  
  26.     numerator = n;  
  27.     denominator = d;    
  28. }  
  29. -(void) setNumerator: (int)n : (int)d :(int) a {  
  30.         numerator = n;  
  31.         denominator = d;   
  32.         printf("+++++a = %d +++ /n", a);   
  33. }  
  34. @end  


3) main.m
C代码  收藏代码
  1. #import <stdio.h>  
  2. #import <Foundation/Foundation.h>  
  3. #import "Fraction.h"  
  4.   
  5. int main( int argc, const char *argv[] ) {  
  6.     // create a new instance  
  7.     Fraction *frac = [[Fraction alloc] init];  
  8.       
  9.       
  10.     int x;  
  11.     int y;  
  12.   
  13.     // set the values  
  14.     [frac setNumerator: 1];  
  15.     [frac setDenominator: 3];  
  16.   
  17.     // print it  
  18.     printf( "The fraction is: " );  
  19.   
  20.     [frac print];  
  21.     printf( "/n/n" );  
  22.     NSLog(@"hello world!!!/n");     // ok  
  23.       
  24.     [frac setNumerator:34 ddd: 98];  
  25.       
  26.     [frac print];  
  27.     printf( "/n/n" );  
  28.     NSLog(@"hello world world!!!/n");     // ok  
  29.       
  30.     [frac setNumerator:44 : 55 :66];      // ok   
  31.     [frac print];  
  32.     printf( "/n/n" );  
  33.           
  34.     scanf("%d %d", &x,&y);             //scanf 函数的测试,ok  
  35.       
  36.     [frac setNumerator: x ddd: y];   //ok  
  37.     [frac print];  
  38.   
  39.     // free memory   
  40.     [frac release];  
  41.     // [frac release];         //前面已经release了,所以这里发生异常:程序崩溃。   
  42.                                //即对空指针进行release,当然不允许了。  
  43.   
  44.     return 0;  
  45. }  


编译方法:
1)将main.m编译成main.o :
gcc -fconstant-string-class=NSConstantString -c main.m -I /GNUstep/System/Library/Headers

2) 将Fraction.m编译成Fraction.o :
gcc -c Fraction.m -I /GNUstep/System/Library/Headers

3) 将.o编译成可执行程序,名为main(最后生成的是main.exe)
gcc -o main main.o Fraction.o -L /GNUstep/System/Library/Libraries/ -lobjc -lgnustep-base
注意:这时会有warning出现,但可以不用管它,毕竟,我们的可执行程序已经编译出来了.

运行结果:
yaoming168@coco ~/objc/fraction
$ ./main.exe
The fraction is: 1/3

2010-08-13 16:29:01.515 main[1212] hello world!!!
34/98

2010-08-13 16:29:01.515 main[1212] hello world world!!!
+++++a = 66 +++

/n44/55
/n/n


yaoming168@coco ~/objc/fraction  



例子3:

test.m


C代码  收藏代码
  1. 1 #import <Foundation/Foundation.h>  
  2. int main (int argc, const char *argv[]) {  
  3. 3 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  4. 4 NSLog(@"Hello World!");  
  5. 5 [pool drain];  
  6. return 0;  
  7. 7 }  



编译链接

1)  直接gcc编译链接方式
gcc -o test test.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString
其中:
-I /GNUstep/System/Library/Headers  指明编译期间头文件包含目录
-L /GNUstep/System/Library/Libraries 指明连接的库文件
-lobjc链接属性,这样就不必显示的链接libobjc.a库,gcc收到这个链接属性会为我们完成这些事。
-fconstant-string-class=NSConstantString指定常量字符串类型为NSConstantString


2) GNUmakefile方式
写GNUmakefile如下:
GNUSTEP_MAKEFILES=/GNUstep/System/Library/Makefiles
include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME = test
test_OBJC_FILES = ./main.m
include $(GNUSTEP_MAKEFILES)/tool.make
解释:其中TOOL_NAME定义为工程名称test,test_OBJC_FILES定义编译文件列表
在GNUmakefile目录下执行make命令,得到可执行文件。


3)      搭配IDE,选用CodeBlocks

第一步:配置编译器

使用GNUStep安装的gcc,在C:\GNUstep\bin目录下。
(1)   Settings->Compiler and debugger...
(2)   选择GNU GCC Compiler点击copy,重新命名,例如"GNU GCC Obj-C Compiler"
(3)   设定GNU GCC Compiler的Toolchain executables路径为C:\GNUstep\bin,也就是GNUstep的gcc所在目录。

(4)   Compile settings->Other options添加-fconstant-string-class=NSConstantString

或者 -fconstant-string-class=NSConstantString-std=c99

第二步:连接器设置 Linkerstettings

(5)    Linker Settings->Other Link Options中添加-lobjc -lgnustep-base选项。
如果出现问题,则可以选用另一种方式,去掉-lobjc -lgnustep-base选项,在Linker Settings->Link libraries中添加:
C:/GNUstep/GNUstep/System/Library/Libraries/libobjc.dll.a 

C:/GNUstep/GNUstep/System/Library/Libraries/libgnustep-base.dll.a


 第三步:指定搜索目录Searchdirectories(需要预先安装好GNUstep)

1)   Compiler(编译器)设置为D:\GNUstep\GNUstep\System\Library\Headers;

2)   Linker(连接器)设置为D:\GNUstep\GNUstep\System\Library\Libraries;

(

第四步:添加Objective-C文件类型支持

1)     Environment...,选择Files extension handling添加 *.m和*.mm
2)     Project->Project tree, file types & categories...在Source中添加*.m和*.mm
高亮显示
1)     Settings->Editor->Syntax highlighting
2)     选择Filemasks...,添加*.m和*.mm
3)     选择 Keywords... 添加Keywords到列表框中
Keywords:

@interface @implementation @end @class @selector @protocol @public @protected @private id BOOL YES NO SEL nil NULL self



4.    代码测试

上述开发环境配置完成后,就可以开始代码测试了。

首先,新建一个工程,选择File->New->Project…,会出现一个工程类型窗口,选择Console Application,然后按照工程建立指引,建立一个mytest的工程,并将main.c的文件更名为main.m,录入以下代码:

#import <Foundation/Foundation.h>

int main (int argc,constchar *argv[])

{

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

    NSLog(@"%@",@"hello world");

    [pool drain];

    return 0;

}


对每一个.m文件设置为可编译链接
1)     .m文件右键->Properties
2)     选择build,选中 Compile file 和 Link file
3)     选择general,去除对File is read-only的选中

4)     注意,.h文件不要设置Compile file 和 Link file

 否则不会编译.m文件


之后再开始编译运行:Buid –> Run… 如果出现以下窗口,恭喜你,你已经成功的搭建了Windows下的Objective-C的集成开发环境

原创粉丝点击