Windows下Object-C编译环境的搭建其实就是搭建一个类似于linux的Cygwin和MinGW的编译环境;
其实Cygwin和MinGW两者相比较我还是比较喜欢Cygwin的环境(更贴近于linux),下面我们就直接说怎么编译吧!
谢谢大家贡献的资源!
下面是我收集的仅限学习使用:
例子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
编译器设定
使用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
(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
(6) Search directories->Complier添加头文件目录: C:\GNUstep\GNUstep\System\Library\Headers
添加源文件格式支持
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
设置为可编译链接
1) .m文件右键->Properties
2) 选择build,选中 Compile file 和 Link file
3) 选择general,去除对File is read-only的选中
4) 注意,.h文件不要设置Compile file 和 Link file
分享到:
相关推荐
windows下object-c环境搭建需要的.文件大于50M,分两部分。第二部分是windows下object-c环境搭建2
windows下object-c环境搭建需要的.文件大于50M,分两部分。第二部分是windows下object-c环境搭建2
NULL 博文链接:https://hjy2099.iteye.com/blog/1562336
Windows下安装开发Object-C 及配置环境, 简化了编译运行过程 ! 点点鼠标就可以完成全部过程啦!!
Object-C语言基础: 学习Object-C语言的基本语法和特性,了解Objective-C编程范式和iOS开发规范。 列表选择功能实现: 掌握实现iOS应用中列表选择功能的方法和技巧,包括列表的展示、数据的加载和选择状态的管理等...
Object-C语言教程 Object-C语言教程 Object-C语言教程 Object-C语言教程
这篇文章为您呈现了一份详尽的Object-C语言学习资源概览,旨在帮助初学者和经验丰富的开发者系统地学习Object-C编程,并通过实践案例和项目资源提升实际编程能力。 文章首先聚焦于Object-C语言教程,这是学习Object...
Windows下安装Object-C开发环境,包含所需的各种环境,在Windows上也能轻松编程
Object-Oriented Programming with Object-C Object-C 进阶书籍---面向对象
Object-C语言教程:Objective-C 简介
Object-C语言是一种面向对象的程序设计语言,常用于iOS和macOS应用程序的开发。在iOS开发中,经常需要将JSON数据转换为模型对象,以便更方便地处理数据。为了简化这一过程,开发者可以使用开源库"MJExtension"来实现...
object-c基础教程object-c基础教程object-c基础教程
Object-C语言教程0220.zipObject-C语言教程0220.zipObject-C语言教程0220.zipObject-C语言教程0220.zipObject-C语言教程0220.zipObject-C语言教程0220.zipObject-C语言教程0220.zip
Object-C_在iOS上使用Object-C进行RSA算法的加密+解密实现
Object-C语言实现列表选择项目源码.zipObject-C语言实现列表选择项目源码.zipObject-C语言实现列表选择项目源码.zip
阅读本文档,您最好有一定的C语言、JAVA语言的基础。 文档内容: 1. GNUStep的安装 2. 类定义 3. Object-C中的布尔类型 4. Object-C中的null 5. 与C混合编写 6. 对象的初始化 7. Object-C的description方法 ...
Object-C期末复习资料,包含多道题目助力期末复习,成功考试过关