博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Objective-C在windows开发环境的搭建
阅读量:7218 次
发布时间:2019-06-29

本文共 2699 字,大约阅读时间需要 8 分钟。

转自 

安装GNUstep

GNUstep Windows Installer提供了Windows平台下的Objective-C的模拟开发环境,一共有四个软件包,其中和是必装的,和是选装的。甭管必装选装,一次性全安上,免得以后麻烦。

编写Hello, World!

安装完成后,在开始菜单里的GNUstep选项里执行shell,就能打开命令行,在这里就可以使用vi编写Object-C程序了,不过操作起来总有些繁琐,其实也可以直接在Windows里进入C:\GNUstep\home\username目录,在这里用你喜欢的工具编写Object-C程序,然后再进入shell里编译。

直接给出helloworld.m文件内容,取自Programming in Objective-C 2.0一书:

#import <Foundation/Foundation.h>

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

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello World!");
[pool drain];

return 0;

}

第一次编译:

gcc -o helloworld helloworld.m

结果出现错误信息,找不到头文件:

helloworld.m:1:34: Foundation/Foundation.h: No such file or directory

helloworld.m: In function `main’:
helloworld.m:4: error: `NSAutoreleasePool’ undeclared (first use in this function)
helloworld.m:4: error: (Each undeclared identifier is reported only once
helloworld.m:4: error: for each function it appears in.)
helloworld.m:4: error: `pool’ undeclared (first use in this function)
helloworld.m:5: error: cannot find interface declaration for `NXConstantString’

第二次编译:

gcc -o helloworld helloworld.m \

-I /GNUstep/System/Library/Headers/

结果出现错误信息,找不到接口声明:

helloworld.m: In function `main’:

helloworld.m:5: error: cannot find interface declaration for `NXConstantString’

第三次编译:

gcc -o helloworld helloworld.m \

-fconstant-string-class=NSConstantString \
-I /GNUstep/System/Library/Headers/

结果出现错误信息,找不到链接库:

helloworld.m:(.text+0×33): undefined reference to `_objc_get_class’

helloworld.m:(.text+0×45): undefined reference to `_objc_msg_lookup’
helloworld.m:(.text+0×64): undefined reference to `_objc_msg_lookup’
helloworld.m:(.text+0×80): undefined reference to `_NSLog’
helloworld.m:(.text+0×93): undefined reference to `_objc_msg_lookup’
helloworld.m:(.text+0xbc): undefined reference to `___objc_exec_class’
helloworld.m:(.data+0×74): undefined reference to `___objc_class_name_NSAutoreleasePool’
helloworld.m:(.data+0×78): undefined reference to `___objc_class_name_NSConstantString’
collect2: ld returned 1 exit status

第四次编译:

gcc -o helloworld helloworld.m \

-fconstant-string-class=NSConstantString \
-I /GNUstep/System/Library/Headers/ \
-L /GNUstep/System/Library/Libraries/ \
-lobjc \
-lgnustep-base

注意:helloworld.m必须出现在-lobjc和-lgnustep-base的前面,否则会出错。

此时会出现一些info提示信息,不过不碍事,终于成功了生成了可执行文件,执行./helloworld.exe看结果。

快捷方式:

如果每次使用gcc的时候,都要输入这么长的命令,无疑是很恼火的事儿,我们可以做一个快捷方式:

编辑C:\GNUstep\bin\gcc.sh的文件,内容如下:

#!/bin/sh

if [ $# -ne 1 ]; then

echo "Usage: $0 name"
exit 1
fi

gcc -g -o $1 $1.m \

-fconstant-string-class=NSConstantString \
-I /GNUstep/System/Library/Headers/ \
-L /GNUstep/System/Library/Libraries/ \
-lobjc \
-lgnustep-base

exit 0

其中,gcc加入了-g参数,方便gdb调试,使用时就很方便了,注意别带扩展名m:

gcc.sh helloworld

参考链接:

转载地址:http://qptym.baihongyu.com/

你可能感兴趣的文章
C# 真正完美的 汉字转拼音
查看>>
Dedecms5.7搜索结果页空白无内容的解决方法
查看>>
通过字符串找到具体枚举(涉及到反射)
查看>>
函数对象、 函数对象与容器、函数对象与算法
查看>>
[转]在cocos2d-x中让一个项目适配iphone、iphone retina、ipad、ipad retina四种分辨率
查看>>
顶点(vertexs) 图元(primitives) 片元(fragments片断) 像素(pixels)
查看>>
Mysql INSERT、REPLACE、UPDATE的区别
查看>>
php5.6.11编译安装报错configure: error: Don't know how to define struct flock on this system
查看>>
前端工程师现在需要掌握的是什么?
查看>>
DQL、DML、DDL、DCL的概念
查看>>
html元素的分类有哪些?
查看>>
js中常用的对象—String的属性和方法
查看>>
(转)面向对象的 JavaScript 编程:dojo.declare 详解
查看>>
Eclipse Color Themes
查看>>
Promise笔记
查看>>
how to create view (windows)
查看>>
应届前端面试——看这篇就够了(一)
查看>>
Eclipse 安装 lombok
查看>>
Spring Security 之集群Session配置
查看>>
不要一口吃个胖子
查看>>