[CMake] cmake config.h
luxInteg
lux-integ at btconnect.com
Sat Nov 27 21:39:23 EST 2010
On Saturday 27 November 2010 14:58:15 Michael Jackson wrote:
> 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
Thanks I maybe had done a blind-man stubmle into a bastardised version
thereof before I say your email. I will give your perhaps more elegant
offering a run sometimes.
More information about the CMake
mailing list