要寫 Objective-C 的程式理所當然的要在 Mac OS X 上 XD
不過有時候想學而手邊沒有 Mac 的話也可以在 Linux / Windows 開發
這一切都要感謝 GNUStep 的計畫
在 Gentoo 上如果只是要開發 non-GUI 的程式只要安裝 gnustep-base/gnustep-base 即可
會連帶安裝 gnustep-base/gnustep-make
都裝好後我們就可以開始寫啦!
一個最簡單的 Objective-C 如下
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
int main(int argc, const char * argv[]){ | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
NSLog(@"Programming is fun!"); | |
[pool drain]; | |
return 0; | |
} |
至於要編譯他的方法指令如下
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gcc `gnustep-config --debug-flags` -o main main.m `gnustep-config --base-libs` |
很簡單吧!
如果想要看完整的參數請參考 gnustep-config –help
但是這樣每次要 build 好像都很麻煩要多打好多東西
所以建立一個簡單的 script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
這樣下次要 build 只要打 build_objc.sh file
如果想要開發圖形介面 需要多安裝 gnustep-base/gnustep-gui
然後編譯變成要打 gcc `gnustep-config –debug-flags` -o main main.m `gnustep-config –gui-libs`