Installing and Using GNUstep and Objective-C on Linux

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Installing and using GNUstep and Objective-C on WindowsBuilding and Installing GNUstep on Linux

Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) format
Objective-C 2.0 Essentials Print and eBook (ePub/PDF/Kindle) editions contain 31 chapters.

Buy Print

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 the gcc-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 as hello.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.

Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) format
Objective-C 2.0 Essentials Print and eBook (ePub/PDF/Kindle) editions contain 31 chapters.

Buy Print



PreviousTable of ContentsNext
Installing and using GNUstep and Objective-C on WindowsBuilding and Installing GNUstep on Linux