CodeBlocks集成Objective-C开发

时间:2022-05-18 16:27:42
转自:http://kingj.iteye.com/blog/1558869 <1>

Objective-C是苹果软件的编程语言,想要上机学习、调试,有一个集成开发环境(IDE)方便很多。有三类方法搭建Objective-C的集成开发环境:

<!--[if !supportLists]-->1)    <!--[endif]-->使用苹果的平台,集成开发环境使用Xcode。但如果没有苹果平台,想在Windows环境下学习Objective-C,可以采用以下两种方法:

<!--[if !supportLists]-->2)    <!--[endif]-->在Windows环境下设置一个苹果虚拟机,但这对个人电脑的性能要求较高,不是所有个人电脑都可以,而且虚拟机的运行速度也较慢;

<!--[if !supportLists]-->3)    <!--[endif]-->采用Codeblocks IDE开发环境,对其进行配置,搭建成支持Object-C的编译、调试、运行的集成开发环境。这种方法对个人电脑的性能几乎没有要求,可以快速构建,本文介绍的是这一种方法。

 

 

 

1、安装Object-C的编译器

Objective-C的编译器有很多,本文介绍使用GnuStep,网址是http://www.gnustep.org/experience/Windows.html,从这里可以下载Windows版本的gcc编译器:

 

进入下载页面,下载上面3个软件包,进行安装,例如安装到D:\GNUstep。关于这3个软件包的作用,可以在网上查询,不再赘述。

 

2、安装Object-C的集成开发环境

我们选择用CodeBlocks IDE作为Objective-C的集成开发环境,下载地址是:http://www.codeblocks.org/

 

3、开发环境配置

通过对Code blocks的配置,一步步完成Objective-C开发环境的搭建。CodeBlocks,可以看见这样的画面:

 

 

 


CodeBlocks集成Objective-C开发

 

第一步:配置编译器

进入Settings->Compiler and debugger...,选择GNU GCC Compiler编译器,按“Copy”按钮,并重新命名为“GNUstep MinGW Compiler并保存。


之后进入Other Options 分页,录入

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


 第二步:连接器设置 Linker stettings

在连接库(Link Libraries)中添加两个文件,如图。

它们在D:\GNUstep\GNUstep\System\Library\Libraries下面:

libgnustep-base.dll.a

libobjc.dll.a

 


CodeBlocks集成Objective-C开发
 第三步:指定搜索目录Search directories(需要预先安装好GNUstep)

 

<!--[if !supportLists]-->1)   <!--[endif]-->Compiler(编译器)设置为D:\GNUstep\GNUstep\System\Library\Headers;


CodeBlocks集成Objective-C开发
 2)   Linker(连接器)设置为D:\GNUstep\GNUstep\System\Library\Libraries;


CodeBlocks集成Objective-C开发
 第四步:添加Objective-C文件类型支持

<!--[if !supportLists]--> 1)   <!--[endif]-->进入Settings->Environment...,选择 Files extension handling 添加*.m。如图:


CodeBlocks集成Objective-C开发
 2)   进入 Project->Project tree->Edit file types & categories... ,在Sources, 下面添加 *.m到文件类型列表中。如图:


CodeBlocks集成Objective-C开发
 3)   进入 Settings->Editor...,选择 Syntax highlighting,点击“Filemasks....”按钮,在弹出框尾部添加*.m 到文件类型。如图:


CodeBlocks集成Objective-C开发
 4)       点击“Keywords...”按钮 (紧靠Filemasks...按钮) 添加下面Object-C的关键字到Edit Keywords列表中。如图。

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

 


CodeBlocks集成Objective-C开发
 

 

 

 

<!--[if !supportLists]-->4.    <!--[endif]-->代码测试

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

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

#import <Foundation/Foundation.h>

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

{

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

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

    [pool drain];

    return 0;

}

如图:


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

 


CodeBlocks集成Objective-C开发

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


CodeBlocks集成Objective-C开发
 如果顺利编译通过并运行,那么恭喜object-c的windows开发环境已经配置好了,如果你想了解并运行cocoa框架的话

 还需要对codeblocks进行下一步配置。

 


声明:转自:http://www.jxva.com/blog/201112/208.html <2>
1.首先安装Objective-C编译器
GNUstep Windows Installer提供了Windows平台下的Objective-C的模拟开发环境,一共有四个软件包,其中GNUstep System和GNUstep Core是必装的,GNUstep Devel和Cairo Backend是选装的。甭管必装选装,一次性全安上,免得以后麻烦。
四个文件都安装到C:GNUstep下
http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/gnustep-msys-system-0.25.1-setup.exe http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/gnustep-core-0.25.0-setup.exe http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/gnustep-devel-1.1.1-setup.exe http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/gnustep-cairo-0.22.1-setup.exe

2.为Code::Blocks添加编译器
打开Code::Blocks,点击菜单Settings>Compiler and debugger>Global compiler settings 在Selected compiler下拉框下面点击Copy, 在弹出窗口中填入: GNUstep MinGW Compiler 之后,点击Toolchain executables选项卡,将Compiler’s installation directory选择为C:GNUstepmingwbin

3.创建Objective-C工程 创建一个Console的C工程,将main.c删除,新建main.m文件,内容如下: #import <Foundation/Foundation.h> int main (int argc, const char *argv[]) {     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     NSLog(@"Hello World!");     [pool drain];     return 0; }
右击main.m点击property,选择Build选项卡,将Compile file与Link file都打上勾, 选择Advanced,将Compiler variable的内容改为CC
4.设置编译选项
(1)方法一 右击将创建的工程,选择Build options…,Selected compiler编译器选择GNUstep MinGW Compiler, 选择Compiler settings>Other options中填入-fconstant-string-class=NSConstantString  选择Linker settings选项卡,点击Add,增加如下两行内容: C:/GNUstep/GNUstep/System/Library/Libraries/libobjc.dll.a C:/GNUstep/GNUstep/System/Library/Libraries/libgnustep-base.dll.a 选择Search directories>Compiler,点击Add,增加如下内容: C:/GNUstep/GNUstep/System/Library/Headers
(2)方法二
右击将创建的工程,选择Build options…,Selected compiler编译器选择GNUstep MinGW Compiler, 选择Compiler settings>Other options中填入-fconstant-string-class=NSConstantString -IC:/GNUstep/GNUstep/System/Library/Headers  -LC:/GNUstep/GNUstep/System/Library/Libraries
选择Linker settings选项卡,在Other linker options中输入-lobjc -lgnustep-base即可

5.增加.m文件类型高亮及编辑器关联 (1)点击Settings>Editors>Syntax highlighting, 选择Syntax highlighting for: C/C++,点击Filemasks…,在弹出窗口里面加入*.m,点OK 选择Matlab,点击Filemasks…,将里面的*.m删除 (2)点击Settings>Environment>Files extension handling,点击*.m,在To open the file中选择 Open it in a Code::Blocks editor即可
更多参考信息:http://wiki.codeblocks.org/index.php?title=Installing_Objective-C_Compiler