windows 下调试ObjectC
来源:互联网 发布:穷人突破阶级知乎 编辑:程序博客网 时间:2024/05/22 02:00
windows 下调试ObjectC
转自:http://www.itivy.com/iphone/
1:到GNUstep官方网站上下载,四个软件包:GNUstep MSYS System、GNUstep Core、GNUstep 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是扩展名):
- #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也是可行的。
下面是我收集的仅限学习使用:
例子1:
hello.m的源码:
- #import <stdio.h>
- int main(int argc,const char *argv[]){
- printf("hello world/n");
- return 0;
- }
编译运行:
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:
- #import <Foundation/NSObject.h>
- @interface Fraction: NSObject {
- int numerator;
- int denominator;
- }
- -(void) print;
- -(void) setNumerator: (int) d;
- -(void) setDenominator: (int) d;
- -(int) numerator;
- -(int) denominator;
- -(void) setNumerator: (int) n ddd: (int)d;
- -(void) setNumerator: (int)n : (int)d :(int) a;
- // 这里,有3个setNumerator函数, 是重载。
- @end
2)Fraction.m
- #import "Fraction.h"
- #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;
- }
- -(void) setNumerator: (int) n ddd: (int)d {
- numerator = n;
- denominator = d;
- }
- -(void) setNumerator: (int)n : (int)d :(int) a {
- numerator = n;
- denominator = d;
- printf("+++++a = %d +++ /n", a);
- }
- @end
3) main.m
- #import <stdio.h>
- #import <Foundation/Foundation.h>
- #import "Fraction.h"
- int main( int argc, const char *argv[] ) {
- // create a new instance
- Fraction *frac = [[Fraction alloc] init];
- int x;
- int y;
- // set the values
- [frac setNumerator: 1];
- [frac setDenominator: 3];
- // print it
- printf( "The fraction is: " );
- [frac print];
- printf( "/n/n" );
- NSLog(@"hello world!!!/n"); // ok
- [frac setNumerator:34 ddd: 98];
- [frac print];
- printf( "/n/n" );
- NSLog(@"hello world world!!!/n"); // ok
- [frac setNumerator:44 : 55 :66]; // ok
- [frac print];
- printf( "/n/n" );
- scanf("%d %d", &x,&y); //scanf 函数的测试,ok
- [frac setNumerator: x ddd: y]; //ok
- [frac print];
- // free memory
- [frac release];
- // [frac release]; //前面已经release了,所以这里发生异常:程序崩溃。
- //即对空指针进行release,当然不允许了。
- return 0;
- }
编译方法:
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
- 1 #import <Foundation/Foundation.h>
- 2 int main (int argc, const char *argv[]) {
- 3 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- 4 NSLog(@"Hello World!");
- 5 [pool drain];
- 6 return 0;
- 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
第一步:配置编译器
(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
如果出现问题,则可以选用另一种方式,去掉-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和*.mm2) 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文件- windows 下调试ObjectC
- windows 下调试ObjectC
- Windows下FFmpeg调试
- Windows下FFmpeg调试
- Windows下FFmpeg调试
- ubuntu下编译objectc 与object c++
- ubuntu下编译objectc 与object c++
- windows下调试linux程序
- windows 下的 调试技巧
- windows下安装调试mysql
- 【FFmpeg】Windows下FFmpeg调试
- windows下调试linux0.11
- 【FFmpeg】Windows下FFmpeg调试
- 【FFmpeg】Windows下FFmpeg调试
- Windows下DLL文件调试
- Coreseek Windows下安装调试
- windows下Eclipse调试ffmpeg
- 在windows下调试linux内核
- Javascript检测访问的浏览器的操作系统
- Find longest increasing sequence (LIS)
- WPF、WCF、WF打造Hello World程序
- [IPhone] Missing file解决办法
- sizeof与#pragma预编译指令
- windows 下调试ObjectC
- dwr基础:原理,配置,使用,异常
- Android基于XMPP Smack Openfire开发IM(2)登录openfire
- 字节序(Endian),大端(Big-Endian),小端(Little-Endian)
- 跟我一起写Windows JS脚本(二):内置对象与任意COM组件
- WPF天天见系列
- Java枚举的7种用法
- MD5加密算法解密算法
- WPF基础到企业应用及优化系列