在Linux上安装和使用GNUstep和Objective-C

 The basics of Objective-C are supported by the GNU compiler collection. In order to utilize the full power of Objective-C together with the Cocoa /openStep environments on Linux, and to work with many of the examples covered in this book, it is necessary to install gcc, the gcc Objective-C support package and the GNUstep environment.

The gcc Objective-C support can be installed on Linux simply by installing thegcc-objc package which is available with all Linux distributions. There are, however, two different paths to installing GNUstep. On those Linux distributions for which pre-built GNUstep packages are provided this is simply a matter of issuing the appropriate command to install GNUstep. On Linux distributions for which pre-built packages are not available, the process involves downloading the GNUstep source code and then building and installing the packages manually.

This chapter will look at one distribution for which pre-built packages are available (Ubuntu Linux) and outline the corresponding installation process.

The next chapter entitled Building and Installing GNUstep on Linux provides an overview of how to obtain and build the necessary GNUstep packages from the source code.

Contents

[hide]
Installing GNUstep on Ubuntu

Fortunately for Ubuntu users, all the GNUstep and gcc Objective-C packages are available and ready to install from the Ubuntu repositories. To install GNUstep, therefore, open a Terminal window on your Ubuntu desktop (Applications->Accessories->Terminal) and enter the following command (together with your password when prompted):

sudo apt-get install gnustep

The apt-get utility will gather together a list of all the packages required for a successful GNUstep runtime installation and ask for confirmation that the installation is to proceed. Once confirmation is provided, the packages will be downloaded and installed onto the system.

The next step is to install the GNUstep development packages. This is achieved using the following command:

sudo apt-get install gnustep-devel

Once again apt-get will list the packages required and seek confirmation before performing the installation process.

Once the installation is complete, it can be tested by opening your favorite editor (if you don't have a favorite try GEdit by selecting Applications->Accessories->Text Editor) and entering some Objective-C code:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

        NSLog (@"hello world");
        [pool drain];
        return 0;
}

Objective-C source files have a .m file name extension, so save the file ashello.m in a suitable folder such that you will be able to locate the file in the next section.

Compiling Objective-C Code

Before an Objective-C program can be run it must first be compiled. Compilation is a process whereby the human readable Objective-C code in the source file (in our case hello.m) is converted to the machine code understood by the CPU. Prior to performing this compilation, however, the GNUstep environment must first be set up. Thankfully, a shell script is provided to perform this task for us and may be executed as follows:

. /usr/share/GNUstep/Makefiles/GNUstep.sh

Note that this setup script will need to be executed each time you start a new Terminal window with the intention of compiling Objective-C. For this reason, you may find it advantageous to add it to the .bashrc file in your home directory so that it gets executed automatically. Failure to execute this script prior to compiling Objective-C code in a Terminal window will result in the compiler reporting errors similar to the following:

error: cannot find interface declaration for ‘NXConstantString’


From within a Terminal window change directory to the where you saved the hello.m source file and execute the following command to compile it:

gcc `gnustep-config --objc-flags` -lgnustep-base hello.m -o hello

If all goes well, the compilation should complete silently and the test application is ready to run. If, on the other hand, you see an error message similar to the following then an additional package needs to be installed on your system:

gcc: error trying to exec 'cc1obj': execvp: No such file or directory

The missing package is called gobjc and may be installed from a Terminal window using the following command:

sudo apt-get install gobjc

Once the installation is complete, attempt to compile the test application again. Assuming a successful compilation is achieved, the program may be executed as follows:

./hello

Executing the program will result in output similar to the following:

2009-09-15 10:48:39.772 prog1[12906] hello world

Assuming you see output similar to the above example, Objective-C and GNUstep are successfully installed on your Linux system and you are ready to continue with the remainder of this book.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
簡單來說, GNUstep 是實作 OpenStep 介面的開放軟體 (Open Source) 計劃, 目標為提供跨平台的物件導向程式開發環境. 早在 1985 年, Steve Jobs 離開蘋果電腦 (Apple) 後成立了 NeXT 公司, 並於 1988 年推出了 NeXT 電腦, 使用 NeXTStep 為作業系統. 在當時, NeXTStep 是相當先進的系統. 以 Unix (BSD) 為基礎, 使用 PostScript 提供高品質的使用者圖形介面, 並以 Objective-C 語言提供完整的物件導向環境. 儘管 NeXT 在軟體上的優異, 其硬體銷售成績不佳, 不久之後, NeXT 便轉型為軟體公司. 1994 年, NeXT 與昇陽 (Sun Microsystem) 合作推出 OpenStep 介面, 目標為跨平台的物件導向程式開發環境. NeXT 接著推出實作 OpenStep 介面的 OPENSTEP 系統, 可在 Mach, Microsoft Windows NT, Sun Solaris 及 HP/UX 上執行. 1996 年, 蘋果電腦買下 NeXT, 做為蘋果電腦下一代作業系統的基礎, OPENSTEP 系統便演進成為 MacOS X 的 Cocoa 環境. 在 1995 年, 自由軟體基金會 (Free Software Fundation) 開始了 GNUstep 計劃, 目的在實作 OpenStep 介面, 以提供 Linux/BSD 系統一個完整的程式發展環境. 但由於 OpenStep 介面過於龐大, 開發人力不足, 及許多技術在當時尚未成熟 (如 Display PostScript), 所以直到目前為止, GNUstep 才算是一個完整的程式開發環境. 儘管 OpenStep 早在 1994 年便提出, 其介面及架構在現今仍相當先進及實用, 使得開發 GNUstep 程式相當容易. GNUstep 使用 Objective-C 語言, 是 C 語言加上 SmallTalk 的物件導向的功能. 結合兩者的優點, 又不至於像 C++ 如此複雜. GNUstep 提供兩個主要的程式庫, Foundation 及 AppKit. Foundation 處理非圖形介面的部份, 如字串, 檔案, 網路, 基本資料結構, 多行緒等, 又稱之為 GNUstep Base. AppKit 則處理圖形介面的部份, 包含視窗, 使用者介面等, 又稱之為 GNUstep GUI. 由於 GNUstep 具有跨平台的特性, 有關繪圖及字型的部份, 則交由 GNUstep Back 來處理. 使用者可依所使用的作業系統, 選擇適當的後端處理 (Backend). GNUstep GUI 會自行處理與 Back 相關的功能, 程式開發者只要使用 GUI 程式庫, 便可適用於各種後端上, 完全不用考慮平台問題. 目前 GNU GCC 3.x 支援 Objective-C 語言, GNUstep 則提供 GNUstep Make 來簡化編譯 Objective-C 程式. GNUstep Make 提供類似 Makefile 的功能, 稱為 GNUmakefile. 與 Makefile 相比較之下 GNUmakefile 簡單許多.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值