CMake generate config.h as from Autoconf

When using Autotools, it usually generates a config.h file by setting the AC_CONFIG_HEADERS macro in configure.ac as follows:

 AC_CONFIG_HEADERS([config.h]) 

What is the corresponding equivalent of this when using CMake?

+3
source share
1 answer

You need to create a file similar to config.h.in . Sort of

 #cmakedefine HAVE_FEATURE_A @ Feature_A_FOUND@ #cmakedefine HAVE_FEATURE_B @ Feature_B_FOUND@ #cmakedefine HAVE_FEATURE_BITS @ B_BITSIZE@ 

Then you must declare the variables Feature_A_FOUND , Feature_B_FOUND , B_BITSIZE in your CMake code and call

 configure_file(config.h.in config.h) 

which will lead to the creation of a config.h file similar to the autotools file. If the variable is not found or set to false, the line will be commented out. Otherwise, the value will be inserted. Assume Feature_A_FOUND=A-NOTFOUND ΒΈ Feature_B_FOUND=/usr/lib/b , B_BITSIZE=64 , which leads to

 /* #undef HAVE_FEATURE_A @ Feature_A_FOUND@ */ #define HAVE_FEATURE_B /usr/lib/b #define HAVE_FEATURE_BITS 64 

HAVE_FEATURE_B will probably be better defined as #cmakedefine01 , resulting in 0 or 1 depending on the value of the variable.

In general, you can create each config.h file generated by Autotools, since CMake is more flexible. But this requires more work, and you cannot automatically get config.h, but you must write the .in file yourself.

Documentation: https://cmake.org/cmake/help/v3.6/command/configure_file.html

+7
source

Source: https://habr.com/ru/post/1270641/


All Articles