1. 安装和下载code::blocks
2. 配置code::blocks环境
打开code::blocks → 菜单 → Settings → Enviroment settings
[Files extension handing]
____[Registered wildcards]
________*.m
____[Wildcard]
________*.m
打开code::blocks → 菜单 → Settings → Editor
[Syntax highlighting]
____[Syntax highlighting for]
________[Matlab]
____________[Filemasks...]
________________*.mdl,
(删除*.m)
________[C/C++]
____________[Filemasks...]
________________,*.m,
(添加,*.m,)
____________[Keywords...]
________________ @interface @implementation @end @class @selector @protocol @public @protected @private id BOOL YES NO SEL nil NULL self
(添加在末尾)
打开code::blocks → 菜单 → Settings → Compiler
在Selected compliler选中GNU GCC Compiler,然后单击Copy,输入名称 GNU GCC Obj-C Compiler
[Compiler settings]
____[Other options]
________-std=c99 -fconstant-string-class=NSConstantString
(-std=c99 表示使用c99标准来编译,这样可以减少很多语法检验上不必要的问题)
(-fconstant-string-class=NSConstantString 表示 文件中的字符串是NSConstantString类型的)
[Linker settings]
____[Link libraries]
________D:\program\GNUstep\GNUstep\System\Library\Libraries\libobjc.dll.a
________D:\program\GNUstep\GNUstep\System\Library\Libraries\libgnustep-base.dll.a
(注意:如果这里配置了,可以不用写下面的-lobjc -lgnustep-base,反之亦然)
____[Other linker options]
________-lobjc -lgnustep-base
(-lobjc 和 -lgnustep-base 是缩写对应GNUstep\System\Library\Libraries目录下面的libobjc.dll.a和libgnustep-base.dll.a两个文件 )
(-I /GNUstep/System/Library/Headers/ 是从哪儿引入头文件. 在此处不用添加,被Search directories的[Compiler]取代了)
(-L /GNUstep/System/Library/Libraries/ 是从哪儿引入链接库 在此处不用添加,被Search directories的[Linker]取代了)
____[Compiler Flags]
________Produce debugging symbols [-g]
________Enable all compiler warnings (overrides many other settings) [-Wall]
(将以上两条选项打勾,便于调试)
[Search directories]
____[Compiler]
________D:\program\GNUstep\GNUstep\System\Library\Headers
[Linker]
________D:\program\GNUstep\GNUstep\System\Library\Libraries
[Toolchain executables]
____[Compiler's installation directory]
________D:\program\GNUstep
3. 编写测试代码测试
工程目录如下:
main.m:
#import <Foundation/Foundation.h>
#import "ttt.h"
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"%@", @"hello world2");
[Ttt1 Main];
[pool drain];
return 0;
}
ttt.h:
@interface Ttt1 : NSObject
{
int m_nVal;
@private
int m_nVal1;
@protected
int m_nVal2;
@public
int m_nVal3;
}
@property (nonatomic,readonly) int m_nVal3;
- (int) add:(int)a Two:(int)b;
+ (void) Main;
+ (int) Add:(int)a Two:(int)b;
@end
ttt.m:
#import <Foundation/Foundation.h>
#import "ttt.h"
@implementation Ttt1
@synthesize m_nVal3;
- (int) add:(int)a Two:(int)b
{
return (int)(a+b);
}
+ (void) Main
{
NSLog(@"sonikk");
}
+ (int) Add:(int)a Two:(int)b
{
return (int)(a+b);
}
@end
最后,为了确保编译通过,更改文件的属性:
鼠标右键main.m、ttt.h、ttt.m 三个文件执行相同操作:
→ Properties... → Build → [√]Compile file 和 [√]Link file
对于main.m 和 ttt.m:
→ Properties... → Advanced → Compiler variable → CC 或者 CPP
对于ttt.h
→ Properties... → Advanced → Compiler variable → OBJC 或者 非CC和CPP皆可
否则会出现错误:
error: stray '@' in program
error: 'interface' does not name a type
error: expected unqualified-id before '-' token
error: expected unqualified-id before '+' token
error: 'end' does not name a type
编译、运行后的效果:
OK !大功告成^ ^,这样就可以实现在windows下面快速编译objective-c语言了,不用写一大堆命令而且,也不需要GNUmakefile了code::blocks就帮你完成了所有的工作,很了不起吧~
----------------------------------------------------
2013-4-3 23:04:51补充:
由于切换了工作环境,在另外一台PC上进行环境搭建后,本以为一切顺利,结果发现每次编译完成后,运行编译出来的.exe程序居然要花5.0s以上,时间非常难熬,对于程序员5s的累积叠加结果就是各种身体和精神上的长期折磨,于是发现有时候控制台输出的信息当中会有久违的红色斑迹:
cannot open output file bin\Debug\TestObjC1.exe Permission denied
怎么办呢?发现那个程序在任务管理器当中居然长期贮存,挥之不去,于是就用任务管理器把它kill了,结果再次编译,旧事重演,就在万分恼怒成咻的时刻,突然发现是安全卫士360所为,于是立即帮安全卫士360的进程斩杀!接着,奇迹就出现了~
编译之后运行只需要0.094s,差不多了~就是这样,喵″
特此记录^_^*