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 privileges of the examples covered in this book, it is necessary
Install GCC, the GCC objective-C support package and the gnustep environment.
The GCC objective-C support can be installed on Linux simply by installingGcc-objcPackage 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]
- 1 installing gnustep on Ubuntu
- 2 compiling objective-C 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
TheApt-GetUtility 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 encounter 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. MFile name extension, so save the fileHello. mIn 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 specify Ming
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. 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 savedHello. mSource 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 shoshould 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 calledGobjcAnd 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.