• Aucun résultat trouvé

Compiling an Object

Dans le document For Mary (Page 152-158)

A class definition is made up of two source files. The Objective-C language is designed for a.hheader file to contain the interface definition of the class and a .m source file to contain the implementation of the methods of the class. In the following example, the header fileSpeak.hspecifies the interface of a class namedSpeakthat is capable of storing a character string internally and then displaying it to standard output on request:

/* Speak.h */

#import <objc/Object.h>

@interface Speak : Object {

char *string;

USINGTHECOMPILERCOLLECTION

Suffix File Contains

.a A library (archive file) containing object files for static linking

.h A header file

.m An Objective-C source file that is to be preprocessed .mi An Objective-C source file that is not to be preprocessed

.o An object file in a format appropriate to be supplied to the linker .so A library containing object files for dynamic linking

Table 6-1. File Name Suffixes in Objective-C Programming

}

- setString: (char *) str;

- say;

- free;

@end

The#importdirective is used to read the header file namedObject.h, which contains the definition of theObjectclass. TheObjectclass is the super class of all Objective-C classes. The definition of theSpeakclass is surrounded by the compiler directives@interfaceand@end. Inside the definition is a block set off with braces where the data definitions are stored (in this example, the only data is the pointer to the string). The data block is followed by the list of methods defined for the class. Each method is specified by a minus sign, the name of the method, and the list of the types of arguments passed to it (if any).

The actual method bodies of theSpeakclass are defined in fileSpeak.m, as follows:

/* Speak.m */

TheSpeak.hheader file is imported so that the definitions of all the data and methods are available. The@implementationcompiler directive specifies that this source file contains the implementation of the methods of theSpeakclass. Method

USINGTHECOMPILERCOLLECTION body definitions preceded by a minus sign are instance methods and can only be called after an object already exists, and those preceded by a plus sign are class variables and can be called any time.

The form of declaration of a method matches the one in the header file, with the addition of a method body inside a pair of braces. Unless some specific data type is being returned by a method, the return type is always assumed to be anid(the data type that represents a generic Objective-C object). Because of this, the methods mostly returnself, which is the way an object refers to itself.

The following program uses aSpeakobject to write the"hello, world"string to the standard output:

/* helloobject.m */

#import <objc/Object.h>

#import "Speak.h"

main() {

id speak;

speak = [Speak new];

[speak setString: "hello, world"];

[speak say];

[speak free];

}

This program can be compiled by compiling each of the source files into object files and then linking them together, as follows:

$ gcc -Wno-import -c helloobject.m -o helloobject.o

$ gcc -Wno-import -c Speak.m -o Speak.o

$ gcc -helloobject.o Speak.o -lobjc -o helloobject

Alternatively, all three steps can be performed in a single command, as follows:

$ gcc -Wno-import helloobject.m Speak.m -lobjc -o helloobject

Creating a Static Library

A collection of.oobject files produced from compiling Objective-C can be stored in a library (archive) of object files. The following example creates a library named libcat.acontaining the implementation code of a class namedCat. The class has methods that will accept a sequence of character strings and concatenate them into a single string.

The fileCat.his the header file defining the interface of theCatclass:

The fileCat.mcontains the implementation of theCatclass. Theaddmethod is used to add characters onto the end of the string, andgetretrieves the current concatenated string. Theinitmethod is meant to be called just once when a new Catobject is created.

} else {

length = strlen(str) + strlen(string) + 1;

newstring = (char *)malloc(length);

strcpy(newstring,string);

strcat(newstring,str);

free(string);

string = newstring;

}

return self;

}

- (char *) get {

return string;

} - free {

if(string != NULL) free(string);

return [super free];

}

TheCat.mfile is compiled into the object fileCat.owith the following command:

$ gcc -c -Wno-import Cat.m -o Cat.o

The object file is then used to construct a library with the following command:

$ ar -r libcat.a Cat.o

The-roption replaces any existing version of the named object files with the newer version, or it will create a completely new library file if none already exists.

The following is a sample program that uses theCatclass to concatenate two strings into one, then extracts the result and displays it:

/* docat.m */

#import <objc/Object.h>

#import "Cat.h"

main() {

USINGTHECOMPILERCOLLECTION

id cat;

char *line;

cat = [Cat new];

[cat add: "Part one"];

[cat add: " and part two"];

line = [cat get];

printf("%s\n",line);

}

This program is compiled into an executable named docat with the following command:

$ gcc -Wno-import docat.m libcat.a -libobjc -o docat

Creating a Shared Library

Object files produced by compiling Objective-C can be stored in a shared library. To construct a shared library it is necessary to compile the source into a form of object code that can be loaded into any location in memory and executed from there. To do this, it is necessary to specify the-fpic(position-independent code) option on the command line. The following line will create such an object file from the class defined inCat.m:

$ gcc -fpic -Wno-import -c Cat.m -o Cat.o

The following command line will use the object file to create a shared library:

$ gcc -shared Cat.o -o cat.so

The two command lines can be combined and the shared library can be produced directly from source, as follows:

$ gcc -Wno-import -fpic -shared Cat.m -o cat.so

The following program uses an instance of theCatclass to combine three strings into one and then display the result:

/* showcat.m */

#import <objc/Object.h>

#import "Cat.h"

main() {

id cat;

char *line;

cat = [Cat new];

[cat add: "The beginning"];

[cat add: ", the middle"];

[cat add: ", and the end."];

line = [cat get];

printf("%s\n",line);

}

The following command will compile theshowcat.mprogram and link it so that it will run using the shared library:

$ gcc -Wno-import showcat.m cat.so -lobjc -o showcat

To be able to execute an application that relies on a shared library, it is necessary for the program to locate the library, as discussed in Chapter 12.

Dans le document For Mary (Page 152-158)