The library has only one header file (called HelloWorld.h), which is the following:
#ifndef _HELLOWORLD_H_ #define _HELLOWORLD_H_ #include <Foundation/Foundation.h> @interface HelloWorld : NSObject + (void) printMessage; @end #endif /* _HELLOWORLD_H_ */(This header file quite simply says that HelloWorld is a subclass of NSObject, and implements a single class method printMessage [a class method is what in java would be called a static method]; the #ifdefs are the standard way of protecting a C header file from multiple inclusions).
The source code of our class is in the file HelloWorld.m, and is the following:
#include "HelloWorld.h" @implementation HelloWorld + (void) printMessage { printf ("Hello World!\n"); } @end(This implements the printMessage class method for the class HelloWorld, and all what this method does is printing out Hello World!.)
To compile our library, we create a GNUmakefile as follows:
include $(GNUSTEP_MAKEFILES)/common.make LIBRARY_NAME = libHelloWorld libHelloWorld_HEADER_FILES = HelloWorld.h libHelloWorld_HEADER_FILES_INSTALL_DIR = /HelloWorld libHelloWorld_OBJC_FILES = HelloWorld.m include $(GNUSTEP_MAKEFILES)/library.make
The main differences with the GNUmakefile for a tool or an application are that we include library.make instead of tool.make or application.make, and that we set the xxx_HEADER_FILES variable to tell the make system which are the library header files. This is quite important because the header files will be installed with the library when the library is installed.
In order to do things cleanly, each library should install its headers in a different directory, so headers from different libraries don't get mixed and confused; this is why we specify that our header file has to be installed in a HelloWorld directory:
libHelloWorld_HEADER_FILES_INSTALL_DIR = /HelloWorld(the / at the beginning is only for compatibility with older versions of gnustep-make, you can omit with newer ones). As a consequence, an application or a tool which needs to use the library will include the header file by using
#include "HelloWorld/HelloWorld.h"because we have installed it into the HelloWorld directory.
As usual, to compile type make and to install type make install.
#include <Foundation/Foundation.h> #include <HelloWorld/HelloWorld.h> int main (void) { [HelloWorld printMessage]; return 0; }(We invoke the printMessage method of the HelloWorld class, then exit.).
We write our usual GNUmakefile (but including GNUmakefile.preamble):
include $(GNUSTEP_MAKEFILES)/common.make TOOL_NAME = HelloWorldTest HelloWorldTest_OBJC_FILES = main.m include GNUmakefile.preamble include $(GNUSTEP_MAKEFILES)/tool.make
Then, here is the GNUmakefile.preamble, in which we tell the make package about the library we want to link against:
HelloWorldTest_TOOL_LIBS += -lHelloWorld
If you have correctly installed the library HelloWorld, this is all you need to do. If you needed to link against more than one library, you would simply put them on the same line, as in:
HelloWorldTest_TOOL_LIBS += -lHelloWorld -lHelloMoonwhich links against the two libraries HelloWorld and HelloMoon.
If HelloWorld were an application, you would need to use
HelloWorldTest_GUI_LIBS += -lHelloWorld(the difference is GUI instead of TOOL).
HelloWorldTest_TOOL_LIBS += -lNicola HelloWorldTest_INCLUDE_DIRS += -I/opt/nicola/include/ HelloWorldTest_LIB_DIRS += -L/opt/nicola/libs/where I am linking against the library libNicola, which is in the directory /opt/nicola/libs/ and whose headers are in /opt/nicola/include/.
This case is quite simple - you write a usual GNUmakefile for your library:
include $(GNUSTEP_MAKEFILES)/common.make LIBRARY_NAME = libNicola libNicola_OBJC_FILES = two.m include GNUmakefile.preamble include $(GNUSTEP_MAKEFILES)/library.makeand add a GNUmakefile.preamble in which you tell the make package that this library depends on the library libHelloWorld:
libNicola_LIBRARIES_DEPEND_UPON += -lHelloWorld