[CMake] cmake config.h

Michael Jackson mike.jackson at bluequartz.net
Sat Nov 27 09:58:15 EST 2010


You use a combination of some CMake macros and "configure_file()" command.

First, in your CMakeLists.txt file (or another cmake file) you would have lines such as:

 
# In this file we are doing all of our 'configure' checks. Things like checking
# for headers, functions, libraries, types and size of types.
INCLUDE (${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
INCLUDE (${CMAKE_ROOT}/Modules/CheckTypeSize.cmake)
INCLUDE (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
INCLUDE (${CMAKE_ROOT}/Modules/CheckCXXSourceCompiles.cmake)
INCLUDE (${CMAKE_ROOT}/Modules/TestBigEndian.cmake)
INCLUDE (${CMAKE_ROOT}/Modules/CheckSymbolExists.cmake)

# To check for an include file you do this:
CHECK_INCLUDE_FILE("stdint.h"        HAVE_STDINT_H)

# To check the size of a primitive type:
CHECK_TYPE_SIZE("int"   SIZEOF_INT)

And you continue doing these types of checks for each item that you want to check. You can even do custom "try-compiles/Try-run" for more elaborate setups.

Now that you have your variables define you add lines like this to your CMakeLists.txt file:

configure_file ("${PROJECT_SOURCE_DIR}/Headers/config.h.in"
                "${PROJECT_BINARY_DIR}/config.h" )
# Now make sure that you the the build directory on your "Include" path when compiling
include_directories(${PROJECT_BINARY_DIR})

Lastly, you need to create a file in your project inside "Headers" called "config.h.in" with the following content:

/*--------------------------------------------------------------------------
 * This file is autogenerated from config.h.in 
 * during the cmake configuration of your project. If you need to make changes
 * edit the original file NOT THIS FILE.
 * --------------------------------------------------------------------------*/
#ifndef _CONFIGURATION_HEADER_GUARD_H_
#define _CONFIGURATION_HEADER_GUARD_H_

/* Define to 1 if you have the <stdint.h> header file. */
#cmakedefine HAVE_STDINT_H @HAVE_STDINT_H@

/* The size of `long', as computed by sizeof. */
#define SIZEOF_INT @SIZEOF_INT@

...... 

#endif

When you run CMake a new file in your build directory will be generated with the name "config.h" and it will have the content appropriate for inclusion into a C/C++ program.

 Now, this is the "basic" setup. If you use this exactly as it appears AND you include other projects into your own projects then you _may_ run into conflicts as some projects will already have these preprocessor statements defined. So what I usually do is prefix the "HAVE_*" and "SIZEOF_*" variables with something from my own project. I have a project template that I pull from located at <href="http://scm.bluequartz.net/support-libraries/cmp"> Look in CoreTests/cmpConfigureChecks.cmake and ConfiguredFiles/cmpConfiguration.h.in
   Most of the code is gathered from other CMake based projects such as CMake itself, vtk, paraview and few others and then made more consistent with how I want my projects setup.

Hope all that helps
___________________________________________________________
Mike Jackson                      www.bluequartz.net
Principal Software Engineer       mike.jackson at bluequartz.net 
BlueQuartz Software               Dayton, Ohio   



On Nov 26, 2010, at 10:24 PM, luxInteg wrote:

> Greetings,
> 
> I am learning cmake.  
> 
> I have   a package that compiles fine with autoconf but fails with cmake. (The 
> former generates automatically  a  required header-file config.h
> ) I have checked the cmake mailing  list to see if the autoheader utility has 
> been ported to cmake.  This seems unclear.  So I would be gateful if someone 
> on list could  verify if this facility is/isNOT (..yet)  available using 
> cmake.  (And if it is -how  it  is used)
> 
> sincerely
> luxInteg



More information about the CMake mailing list