---
title: "[objective-c] win下搭建Objective-c开发环境"
author: "helpbs"
date: "2012-03-24T09:32:00Z"
tags: []
slug: "objective-c-win-objective-c"
draft: false
lastmod: "2012-04-20T18:27:52Z"
id: 1491948
url: "https://helpbs.iteye.com/blog/1491948"
---

首先，目前windows下没有Objective-C的IDE存在，ObjectiveEClipse是一款可选择的插件，搭配Eclipse3.5+CDT6.0,但是已经停止更新。[GNUstep](http://www.gnustep.org/)是提供类似Cocoa(苹果OS的开发框架)的API和工具，目前支持GNU/Linux and GNU/HURD, Solaris, NetBSD, OpenBSD, FreeBSD, Darwin和Windows，免费使用。这个项目使Objective C能在多数流行平台上开发和运行。

　　在Windows下搭建Objective C开发环境，需要到GNUstep官方网站上下载，四个软件包：[GNUstep MSYS System](http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/gnustep-msys-system-0.25.1-setup.exe)、[GNUstep Core](http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/gnustep-core-0.25.0-setup.exe)、[GNUstep Devel](http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/gnustep-devel-1.1.1-setup.exe)、[Cairo Backend](http://ftpmain.gnustep.org/pub/gnustep/binaries/windows/gnustep-cairo-0.22.1-setup.exe)。其中，前两个软件包是必须要安装的，第三个软件包是安装一些开发工具，比如：gcc、g++等，所以如果是学习Objective C的话，这个包也是必须要安装，第四个软件包是安装glib等库，这个包安装不安装根据具体情况而定。安装路径不建议出现中文，安装后在环境变量PATH中增加:C:\\GNUstep\\GNUstep\\System\\Tools;C:\\GNUstep\\bin;C:\\GNUstep\\mingw\\bin，安装后运行GNUstep shell也就是安装目录下的msys.bat。测试一下gcc与make命令。

**测试程序**

**编译链接**

* * *

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如下：

注：其中TOOL\_NAME定义为工程名称test，test\_OBJC\_FILES定义编译文件列表

在GNUmakefile目录下执行make命令，得到可执行文件。

**3：搭配IDE，选用CodeBlocks**

**编译器设定**

* * *

使用GNUStep安装的gcc，在C:\\GNUstep\\bin目录下。

1\) *Settings-&gt;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*-&gt;*Other options*添加-fconstant-string-class=NSConstantString

5\) *Linker Settings*-&gt;*Other Link Options*中添加-lobjc -lgnustep-base选项。

　　如果出现问题，则可以选用另一种方式，去掉-lobjc -lgnustep-base选项，在*Linker Settings*-&gt;Link libraries中添加：

　　C:/GNUstep/GNUstep/System/Library/Libraries/libobjc.dll.a

　　C:/GNUstep/GNUstep/System/Library/Libraries/libgnustep-base.dll.a

6\) Search directories-&gt;Complier添加头文件目录: C:\\GNUstep\\GNUstep\\System\\Library\\Headers

## 添加源文件格式支持

* * *

*1） Environment...，*选择**Files extension handling**添加**\*.m**和**\*.mm**

2）*Project-&gt;Project tree, file types & categories...在*Source**中添加\*.m和\*.mm

## 高亮显示

* * *

1)*Settings-&gt;Editor-&gt;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文件右键-&gt;Properties

2\) 选择build，选中**Compile file**和**Link file**

3\) 选择general，去除对**File is read-only**的选中

4\) 注意，.h文件**不要**设置**Compile file**和**Link file**