CodeBlocks集成Objective-C

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即可

你可能感兴趣的:(Objective-C)