使用 autoconf AC_ARG_WITH 将变量传递到 C 程序中

发布于 2025-01-18 08:47:45 字数 530 浏览 4 评论 0原文

我正在尝试获取在./configure调用到C代码上提供的值,以便可以打印它。它应该像./configure一样传递到配置中-Allow-text =“某些文本”

我到目前为止的内容:

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

但是我不知道下一步该怎么做以及如何访问main.c中的变量。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浴红衣 2025-01-25 08:47:45

AC_DEFINE_UNQUOTED 宏可用于扩展 shell 变量,例如:

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

$txt shell 变量的值将被放置在 TEXT 宏中在 AC_CONFIG_HEADERS([config.h]) 命名的 config.h 文件中,例如:

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

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文