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.
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.
分享到:
相关推荐
总结来说,在Windows系统下搭建Objective-C开发环境虽然不像在Mac OS X上那样直接便捷,但通过使用GNUstep等工具,我们依然可以实现在Windows系统下的Objective-C开发。希望这份详细的指南能够帮助那些需要或希望在...
17. 编译和构建环境:文中提到了多样的开发环境和构建系统,比如Linux/FreeBSD、GNUStep、XCode、Cygwin等,这些工具支持在不同的操作系统上构建和运行Objective-C程序。 18. Objective-C与C/C++的混合编程:...
在Windows平台上搭建Objective-C开发环境是一项相对复杂但可行的任务,主要依赖于GNUstep项目,它是一个开源实现的Objective-C环境,旨在提供与Apple的Mac OS X上的Cocoa类似的功能。以下是一个详细的步骤指南: 1....
- **设定环境**:在不同操作系统上配置Objective-C开发环境,如在Linux/FreeBSD上安装GNUStep,Mac OS X上安装Xcode,Windows上安装cygwin或mingw和GNUStep。 2. **Objective-C基础**: - **@interface**:定义类...
- **与C语言兼容**:Objective-C能够与C语言无缝集成,这意味着可以在Objective-C代码中使用C语言的语法和功能,同时也允许在C代码中调用Objective-C编写的函数。 - **强大的工具支持**:由于Objective-C主要用于...
- **历史背景**:Objective-C 最初由 Brad Cox 和 Tom Love 在 1983 年开发,后来被苹果公司采纳并作为 Mac OS X 和 iOS 应用开发的主要语言之一。 - **应用领域**:Objective-C 主要用于开发 macOS 和 iOS 应用...
Objective-C是一种面向对象的编程语言,它是在C语言基础上添加面向对象特性的扩展而形成的。Objective-C的发展历史可以追溯至20世纪80年代早期,由Brad J. Cox设计,它借鉴了Smalltalk-80语言的特性。由于其基础是...
### 搭建Linux下的Objective-C开发环境 #### 背景介绍 Objective-C是一种通用、...通过这种方式,即使没有苹果设备,您也可以在Linux环境下熟悉和实践Objective-C编程,为将来在苹果平台上开发应用打下坚实的基础。
在编译和环境设定方面,教程指出不同操作系统环境下设置Objective-C开发环境的方法,如在Linux或FreeBSD上安装GNUStep,MacOSX上安装XCode等。这为初学者提供了开发环境的搭建指南。 Objective-C中的类定义分为接口...
尽管现在Swift已成为苹果官方推荐的主要开发语言,但Objective-C仍然在许多现有的项目中被广泛使用。对于想要深入了解Objective-C的开发者来说,Steve Kochan的《Programming in Objective-C》是一本极佳的资源,它...
它继承了C语言的所有特性,并在此基础上增加了面向对象的功能和动态特性。 - **应用场景**: 主要用于开发苹果公司的操作系统上的应用程序,包括iOS和macOS。 #### 二、环境搭建 - **Linux/FreeBSD**: - 安装...
**Objective-C**是一种面向对象的编程语言,它是在标准C的基础上发展而来的,并且增加了面向对象的功能。Objective-C是苹果公司开发iOS和macOS应用的主要语言之一,在移动开发领域具有重要的地位。 #### 二、环境...
- **引用计数**:Objective-C使用引用计数的方式进行内存管理,主要包括`retain`和`release`操作。 - **析构方法**:`dealloc`方法会在对象被销毁之前调用,用于释放对象占用的资源。 - **自动释放池**:自动释放池...
Objective-C是一种面向对象的编程语言,基于C语言并扩展了许多特性,主要用于开发苹果的macOS和iOS操作系统下的应用程序。Objective-C支持动态消息传递机制,使得它可以实现运行时多态性。 #### 二、环境搭建与配置...
对于想要在 Windows 上使用 Objective-C 的开发者来说,GNUStep 提供了一个可行的解决方案。 1. **安装 GNUStep**:访问官方提供的 Windows 安装页面,下载并安装以下四个文件: - gnustep-msys-system-xxx.exe -...
自由软件基金会的GNU开发环境在1992年增加了对Objective-C的支持,形成了GNUStep和LinuxSTEP等开源实现。 随着1996年苹果公司收购NeXT,Objective-C成为了苹果操作系统OS X的基础,苹果将其开发环境命名为Cocoa,...
GNUstep的目标是跨平台,使得开发者能够在不同的操作系统上,如Linux、FreeBSD、Windows等,构建和运行Objective-C应用。 GNUstep的核心组件包括一个编译器(GCC)、一个运行时系统、一系列基础类库(Foundation和...
可以选择使用其他IDE,如GNUStep可以在Linux/FreeBSD环境下使用,而在Windows NT 5.x(如Windows 2000、Windows XP)环境下,则需要先安装cygwin或mingw,然后安装GNUStep才能进行Objective-C的开发。 #### 四、...
- **定义**: Objective-C是一种面向对象的编程语言,它是在C语言基础上扩展而成,增加了面向对象的功能。 - **特点**: 继承自C语言,拥有C语言的所有特性,并在此基础上增加了一些面向对象的特性。 - **应用领域**: ...