Home > other >  Pass variable using autoconf AC_ARG_WITH into C program
Pass variable using autoconf AC_ARG_WITH into C program

Time:04-02

I'm trying to get a value provided on the ./configure invocation through to C code so it can be printed. It should be passed into configure like ./configure --allow-text="Some text"

What I have so far:

AC_PREREQ([2.69])
AC_INIT([proj], [0.0.1], [])
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])

AC_PROG_CC
AM_PROG_CC_C_O


txt=something

AC_ARG_WITH([text],
            AS_HELP_STRING([A string to be printed]),
            [txt="$withval"], [])

AC_DEFINE([TEXT])


AC_CONFIG_FILES([Makefile])
AC_OUTPUT

But I don't know what to do next and how to access the variable in main.c.

CodePudding user response:

The AC_DEFINE_UNQUOTED macro can be used to expand shell variables, for example:

AC_PREREQ([2.69])
AC_INIT([proj], [0.0.1], [])
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])

AC_PROG_CC
AM_PROG_CC_C_O


txt=something

AC_ARG_WITH([text],
            [AS_HELP_STRING([--with-text=STRING],
                            [Specify a string to be printed])],
            [txt="$withval"], [])

AC_DEFINE_UNQUOTED([TEXT],["${txt}"],[A string to be printed])


AC_CONFIG_FILES([Makefile])
AC_OUTPUT

The value of the $txt shell variable will be placed in the TEXT macro in the config.h file named by AC_CONFIG_HEADERS([config.h]), for example:

/* A string to be printed */
#define TEXT "Hello world"
  • Related