您的位置:首页 > 移动开发 > Objective-C

[转]在 Ubuntu 中使用 GNUstep 搭建 Objective-C 开发环境

2015-12-14 16:27 645 查看
GNUstep 介绍见 http://www.gnustep.org/


安装相关程序

直接 apt-get install 搞定。
build-essential
gnustep
gnustep-devel
gnustep-examples
gobjc
gobjc++


设置环境变量

为了方便使用 GNUstep 的各种编译工具,需要先在 .bashrc 中导入 GNUstep 的脚本。
# Setup GNUstep
GNUSTEP_MAKEFILES=/usr/share/GNUstep/Makefiles
export GNUSTEP_MAKEFILES
source $GNUSTEP_MAKEFILES/GNUstep.sh


编写 Makefile

使用 Makefile 来编译 Objective-C App,注意 Makefile 的文件名为 GNUmakefile
include $(GNUSTEP_MAKEFILES)/common.make

APP_NAME=HelloWorld
HelloWorld_OBJC_FILES=test.m

include $(GNUSTEP_MAKEFILES)/application.make


这个是编译出 Mac OS App Bundle 形式的目标,如果是编译命令行工具的话,可以使用如下 Makefile:
include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME=HelloWorld
HelloWorld_OBJC_FILES=test.m

include $(GNUSTEP_MAKEFILES)/tool.make


在编译命令行工具时,AppKit 就不会自动链接进来了,使用 NSColor 这样的类就会有问题。


HelloWorld

一个最简单的 HelloWorld 代码:
#import <Foundation/Foundation.h>

int main() {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSLog(@"HelloWorld");
[pool release];
return 0;
}


保存为 HelloWorld.m,编写 Makefile:
include $(GNUSTEP_MAKEFILES)/common.make

APP_NAME=HelloWorld
HelloWorld_OBJC_FILES=HelloWorld.m

include $(GNUSTEP_MAKEFILES)/application.make


保存 Makefile 为 GNUmakefile,执行 make,会在当前目录生成 HelloWorld.app,使用 openapp 命令运行:
openapp ./HelloWorld.app


当然如果是编译成命令行工具的目标的话,会在当前目录的 obj 目录中生成 HelloWorld 可执行文件,这个可以直接从命令行运行:
./obj/HelloWorld


参考资料

http://www.gnustep.org/
http://www.qiongbupa.com/archives/678
http://www.gnustep.org/resources/documentation/User/GNUstep/gnustep-howto_4.html#SEC8
http://www.gnustep.it/nicola/Tutorials/WritingMakefiles/node6.html
http://www.gnustep.it/nicola/Tutorials/index.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: