[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2. Preparation

To use GPGME, you have to perform some changes to your sources and the build system. The necessary changes are small and explained in the following sections. At the end of this chapter, it is described how the library is initialized, and how the requirements of the library are verified.

2.1 Header  What header file you need to include.
2.2 Building the Source  Compiler options to be used.
2.3 Using Automake  Compiler options to be used the easy way.
2.4 Library Version Check  Getting and verifying the library version.
2.5 Multi Threading  How GPGME can be used in an MT environment.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.1 Header

All interfaces (data types and functions) of the library are defined in the header file `gpgme.h'. You must include this in all programs using the library, either directly or through some other header file, like this:

 
#include <gpgme.h>

The name space of GPGME is gpgme_* for function names, Gpgme* for data types and GPGME_* for other symbols. Symbols internal to GPGME take the form _gpgme_*.

Because GPGME links to the Assuan library, linking to GPGME will also use the assuan_* and _assuan_* name space indirectly.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.2 Building the Source

If you want to compile a source file including the `gpgme.h' header file, you must make sure that the compiler can find it in the directory hierarchy. This is accomplished by adding the path to the directory in which the header file is located to the compilers include file search path (via the `-I' option).

However, the path to the include file is determined at the time the source is configured. To solve this problem, gpgme ships with a small helper program gpgme-config that knows about the path to the include file and other configuration options. The options that need to be added to the compiler invocation at compile time are output by the `--cflags' option to gpgme-config. The following example shows how it can be used at the command line:

 
gcc -c foo.c `gpgme-config --cflags`

Adding the output of `gpgme-config --cflags' to the compilers command line will ensure that the compiler can find the GPGME header file.

A similar problem occurs when linking the program with the library. Again, the compiler has to find the library files. For this to work, the path to the library files has to be added to the library search path (via the `-L' option). For this, the option `--libs' to gpgme-config can be used. For convenience, this option also outputs all other options that are required to link the program with GPGME (in particular, the `-lgpgme' option). The example shows how to link `foo.o' with the GPGME library to a program foo.

 
gcc -o foo foo.o `gpgme-config --libs`

Of course you can also combine both examples to a single command by specifying both options to gpgme-config:

 
gcc -o foo foo.c `gpgme-config --cflags --libs`


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.3 Using Automake

It is much easier if you use GNU Automake instead writing your own Makefiles. If you do that you don't have to worry about finding and invoking the gpgme-config script at all. GPGME provides an extension to Automake that does all the work for you.

Macro: AM_PATH_GPGME ([minimum-version], [action-if-found], [action-if-not-found])
Check whether GPGME (at least version minimum-version, if given) exists on the host system. If it is found, execute action-if-found, otherwise do action-if-not-found, if given.

Additionally, the function defines GPGME_CFLAGS to the flags needed for compilation of the program to find the `gpgme.h' header file, and GPGME_LIBS to the linker flags needed to link the program to the GPGME library.

You can use the defined Autoconf variables like this in your `Makefile.am':

 
AM_CPPFLAGS = $(GPGME_CFLAGS)
LDADD = $(GPGME_LIBS)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.4 Library Version Check

Function: const char * gpgme_check_version (const char *required_version)
The function gpgme_check_version has three purposes. It can be used to retrieve the version number of the library. In addition it can verify that the version number is higher than a certain required version number. In either case, the function initializes some sub-systems, and for this reason alone it must be invoked early in your program, before you make use of the other functions in GPGME.

If required_version is NULL, the function returns a pointer to a statically allocated string containing the version number of the library.

If required_version is not NULL, it should point to a string containing a version number, and the function checks that the version of the library is at least as high as the version number provided. In this case, the function returns a pointer to a statically allocated string containing the version number of the library. If REQUIRED_VERSION is not a valid version number, or if the version requirement is not met, the function returns NULL.

If you use a version of a library that is backwards compatible with older releases, but contains additional interfaces which your program uses, this function provides a run-time check if the necessary features are provided by the installed version of the library.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.5 Multi Threading

The GPGME library is not entirely thread-safe, but it can still be used in a multi-threaded environment if some care is taken. If the following requirements are met, there should be no race conditions to worry about:


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by John Goerzen on November, 19 2002 using texi2html