CMake:How To Write Platform Checks: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
If you want to write software which compiles and runs on different operating systems, you have to take care for the special properties of the different platforms. On different operating systems there are subtle differences, e.g. on FreeBSD you should not use malloc.h, while it is perfectly ok to use it on Linux. These differences are usually handled by providing a header file which contains a bunch of define-statements according to the platform properties, usually named config.h: | If you want to write software which compiles and runs on different operating systems, you have to take care for the special properties of the different platforms. On different operating systems there are subtle differences, e.g. on FreeBSD you should not use malloc.h, while it is perfectly ok to use it on Linux. These differences are usually handled by providing a header file which contains a bunch of define-statements according to the platform properties, usually named config.h: | ||
<pre> | |||
#define HAVE_MALLOC_H 1 | #define HAVE_MALLOC_H 1 | ||
/* #undef HAVE_SYS_MNTTAB_H 1 */ | /* #undef HAVE_SYS_MNTTAB_H 1 */ | ||
/* #undef HAVE_SYS_MNTENT_H 1 */ | /* #undef HAVE_SYS_MNTENT_H 1 */ | ||
#define HAVE_SYS_MOUNT_H 1 | #define HAVE_SYS_MOUNT_H 1 | ||
</pre> | |||
This header file is then included in the source files and the handled appropriately: | This header file is then included in the source files and the handled appropriately: | ||
foo.c: | foo.c: | ||
<pre> | |||
#include "config.h" | #include "config.h" | ||
Line 22: | Line 24: | ||
... | ... | ||
} | } | ||
</pre> | |||
The contents of config.h depend on the platform where the sources are compiled, so there needs to be a way to generate this header file before the actual compilation process starts. If you are using autotools-based software, you probably know the <tt>./configure</tt> step, which has to be executed before starting <tt>make</tt>. The <tt>./configure</tt> script does some system introspection and generates from the gathered information the <tt>config.h</tt> header file. | |||
CMake is able to do the same, and I'll show you how to do it. | |||
Addtionally to the builtin commands (see http://www.cmake.org/HTML/Documentation.html ), cmake offers more commands implemented by cmake script files, called modules. These files are located in the cmake module directory, on UNIX systems is this by default <tt>/usr/local/share/CMake/Modules </tt>. | |||
Revision as of 20:56, 27 January 2006
If you want to write software which compiles and runs on different operating systems, you have to take care for the special properties of the different platforms. On different operating systems there are subtle differences, e.g. on FreeBSD you should not use malloc.h, while it is perfectly ok to use it on Linux. These differences are usually handled by providing a header file which contains a bunch of define-statements according to the platform properties, usually named config.h:
#define HAVE_MALLOC_H 1 /* #undef HAVE_SYS_MNTTAB_H 1 */ /* #undef HAVE_SYS_MNTENT_H 1 */ #define HAVE_SYS_MOUNT_H 1
This header file is then included in the source files and the handled appropriately:
foo.c:
#include "config.h" #ifdef HAVE_MALLOC_H #include <malloc.h> #else #include <stdlib.h> #endif void bar() { void *buf=malloc(1024); ... }
The contents of config.h depend on the platform where the sources are compiled, so there needs to be a way to generate this header file before the actual compilation process starts. If you are using autotools-based software, you probably know the ./configure step, which has to be executed before starting make. The ./configure script does some system introspection and generates from the gathered information the config.h header file. CMake is able to do the same, and I'll show you how to do it.
Addtionally to the builtin commands (see http://www.cmake.org/HTML/Documentation.html ), cmake offers more commands implemented by cmake script files, called modules. These files are located in the cmake module directory, on UNIX systems is this by default /usr/local/share/CMake/Modules .