Search code examples
cmakec-preprocessorconfig

Unexpected result from #cmakedefine substitution


Suppose that in my CMakeLists.txt I have:

check_function_exists(getopt HAVE_GETOPT)
configure_file(config.h.in config.h @ONLY)

and in config.h.in I have:

#cmakedefine HAVE_GETOPT @HAVE_GETOPT@
#cmakedefine STDLIB_HAS_GETOPT @HAVE_GETOPT@

I expect to see

#define HAVE_GETOPT 1
#define STDLIB_HAS_GETOPT 1

after the pass. instead, I get:

#define HAVE_GETOPT 1 
/* #undef STDLIB_HAS_GETOPT */

why the difference in behavior?

Notes:

  • Using cMake v3.5.1 and GNU/Linux Mint 18.3 with a kernel 4.10.

Solution

  • To get the desired behavior, don't use cmakedefine, but use cmakedefine01 instead (note the trailing 01). In your example:

    #cmakedefine01 HAVE_GETOPT @HAVE_GETOPT@
    #cmakedefine01 STDLIB_HAS_GETOPT @HAVE_GETOPT@
    

    Documentation: https://cmake.org/cmake/help/latest/command/configure_file.html