Compile Objective-C on Gentoo

要寫 Objective-C 的程式理所當然的要在 Mac OS X 上 XD

不過有時候想學而手邊沒有 Mac 的話也可以在 Linux / Windows 開發

這一切都要感謝 GNUStep 的計畫

在 Gentoo 上如果只是要開發 non-GUI 的程式只要安裝 gnustep-base/gnustep-base 即可

會連帶安裝 gnustep-base/gnustep-make

都裝好後我們就可以開始寫啦!
一個最簡單的 Objective-C 如下

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]){
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Programming is fun!");
[pool drain];
return 0;
}
view raw main.m hosted with ❤ by GitHub

至於要編譯他的方法指令如下

gcc `gnustep-config --debug-flags` -o main main.m `gnustep-config --base-libs`
view raw build.sh hosted with ❤ by GitHub

很簡單吧!

如果想要看完整的參數請參考 gnustep-config –help

但是這樣每次要 build 好像都很麻煩要多打好多東西

所以建立一個簡單的 script

#!/bin/sh
if [ $# -ne 1 ]; then
echo "Usage: $0 name"
exit 1
fi
gcc `gnustep-config --debug-flags` -o $1 $1.m `gnustep-config --base-libs`
exit 0
view raw build_objc.sh hosted with ❤ by GitHub

這樣下次要 build 只要打 build_objc.sh file


如果想要開發圖形介面 需要多安裝 gnustep-base/gnustep-gui

然後編譯變成要打 gcc `gnustep-config –debug-flags` -o main main.m `gnustep-config –gui-libs`