如何在 Autotools 中添加和实现配置标志?

发布于 2024-08-01 18:20:46 字数 1394 浏览 4 评论 0原文

我们研究小组的部分程序具有 ctemplate 库提供的辅助功能。 在我们过时的集群上,由于编译,我们无法构建软件,因此我想分离此功能并通过 configure 标志控制是否包含它,例如 --禁用-ctemplate

该软件用 C++ 编写,使用 Autotools 构建系统——我对这两个系统都没有太多经验。 我的理解是,要完成此任务,我需要执行以下操作:

  1. 通过在 configure.ac 中创建新的 AC_ARG_ENABLE 条目,向配置脚本添加新标志>.

  2. 在使用 ctemplate 库的代码以及调用的任何代码周围添加一些 #ifdef(或可能 #ifndef)语句该代码。

我认为第一步看起来像这样:

AC_ARG_ENABLE(ctemplate,
[  --disable-ctemplate    Disable HTML output],
[case "${enableval}" in
  yes) ctemplate=false ;;
  no)  ctemplate=true ;;
  *) AC_MSG_ERROR(bad value ${enableval} for --disable-ctemplate) ;;
esac],[ctemplate=true])
AM_CONDITIONAL(NOCTEMPLATE, test x$ctemplate = xfalse)

虽然我不知道逻辑是否正确,因为我已经从使用 --enable-FLAG 而不是 的示例中改编了这个示例>--禁用-FLAG

对于第二步,我将把各个部分包装在预处理器标志中,例如

#ifndef NOCTEMPLATE
void Class::MethodUsingCtemplate(...)
{
    ...
}
#endif

如果我执行 configure --disable-ctemplate,这会正确“连接”所有内容吗?

另外,这是否可以确保程序不会进入ctemplate库进行编译? 如果没有,那么这一切都是白费; 我必须能够阻止编译 ctemplate 库和依赖组件。

我要重申一下,我对 C++ 和 Autotools 都不熟悉; 我首先采取了一种非常幼稚的方法来解决这个问题。 如果您有这方面的经验,我非常感谢您的更正和您可以提供的任何解释。

A portion of our research group's program has auxiliary functionality provided by ctemplate library. On our dated cluster, we can not build the software due to compilation , so I would like to separate this functionality and control whether it is included or not via a configure flag, such as --disable-ctemplate.

The software, written in C++, uses the Autotools build system—neither of which I have much experience with. My understanding is that to accomplish this task, I need to do the following:

  1. Add a new flag to the configure script by creating a new AC_ARG_ENABLE entry in configure.ac.

  2. Add some #ifdef (or possibly #ifndef) statements around the code that uses the ctemplate library, and around any code that calls that code.

I think the first step would look something like this:

AC_ARG_ENABLE(ctemplate,
[  --disable-ctemplate    Disable HTML output],
[case "${enableval}" in
  yes) ctemplate=false ;;
  no)  ctemplate=true ;;
  *) AC_MSG_ERROR(bad value ${enableval} for --disable-ctemplate) ;;
esac],[ctemplate=true])
AM_CONDITIONAL(NOCTEMPLATE, test x$ctemplate = xfalse)

though I don't know if the logic is correct, since I've adapted this example from examples that used --enable-FLAG instead of --disable-FLAG.

For the second step, I'll wrap sections in preprocessor flags such as

#ifndef NOCTEMPLATE
void Class::MethodUsingCtemplate(...)
{
    ...
}
#endif

Would this correctly "wire up" everything if I did configure --disable-ctemplate?

Also, will this ensure that the program does not go into the ctemplate library for compilation? If not, then all of this is for nothing; it's imperative that I can prevent compilation of the ctemplate library and dependent components.

I'll re-iterate that I'm unfamiliar with both C++ and Autotools; I have taken a very naive first approach at solving this problem. If you have experience in this area, I would really appreciate your corrections and any explanations you could offer.

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

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

发布评论

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

评论(2

念三年u 2024-08-08 18:20:46

这是我在阅读文档、教程、帮助线程、邮件列表等后整理出来的解决方案,并且只是简单地尝试直到它们起作用,我可以解释它们为什么起作用。

configure.ac 中,我放置了以下代码行

# This adds the option of compiling without using the ctemplate library,
# which has proved troublesome for compilation on some platforms
AC_ARG_ENABLE(ctemplate,
  [ --disable-ctemplate   Disable compilation with ctemplate and HTML output],
  [case "${enableval}" in
     yes | no ) WITH_CTEMPLATE="${enableval}" ;;
     *) AC_MSG_ERROR(bad value ${enableval} for --disable-ctemplate) ;;
   esac],
  [WITH_CTEMPLATE="yes"]
)

dnl Make sure we register this option with Automake, so we know whether to
dnl descend into ctemplate for more configuration or not
AM_CONDITIONAL([WITH_CTEMPLATE], [test "x$WITH_CTEMPLATE" = "xyes"])

# Define CTEMPLATE in config.h if we're going to compile against it
if test "x$WITH_CTEMPLATE" = "xyes"; then
    AC_DEFINE([CTEMPLATE], [], ["build using ctemplate library"])
    AC_MSG_NOTICE([ctemplate will be used, HTML output enabled])
else
    AC_MSG_NOTICE([ctemplate will not be used, HTML output disabled])
fi

在下一步中,我将顶层的 Makefile.am 更改为以下内容:

if WITH_CTEMPLATE
  MAYBE_CTEMPLATE = ctemplate
endif

SUBDIRS = boost libgsl $(MAYBE_CTEMPLATE) libutil ...

在较低级别 Makefile.ams,我添加了

if WITH_CTEMPLATE
    # some change to the configuration
else
    # some other change to the configuration
endif

最后,我必须确保关键的 C++ 头文件之一(包含在代码的其他部分中)具有以下内容:

#ifdef HAVE_CONFIG_H
#    include "config.h"
#endif

config.h包含使用 AC_DEFINE 创建的任何新定义,因此此文件必须包含在检查此路由创建的宏定义是否已定义(或未定义)的部分中。

这花了我很多时间,也让我克服了挫败感。 我只能希望在这里记录这个解释可以让其他人免于同样的命运。

Here is the solution that I put together after reading through documentation, tutorials, help threads, mailing lists, etc., and just plain trying things until they worked I could explain why they worked.

In configure.ac, I placed the following lines of code

# This adds the option of compiling without using the ctemplate library,
# which has proved troublesome for compilation on some platforms
AC_ARG_ENABLE(ctemplate,
  [ --disable-ctemplate   Disable compilation with ctemplate and HTML output],
  [case "${enableval}" in
     yes | no ) WITH_CTEMPLATE="${enableval}" ;;
     *) AC_MSG_ERROR(bad value ${enableval} for --disable-ctemplate) ;;
   esac],
  [WITH_CTEMPLATE="yes"]
)

dnl Make sure we register this option with Automake, so we know whether to
dnl descend into ctemplate for more configuration or not
AM_CONDITIONAL([WITH_CTEMPLATE], [test "x$WITH_CTEMPLATE" = "xyes"])

# Define CTEMPLATE in config.h if we're going to compile against it
if test "x$WITH_CTEMPLATE" = "xyes"; then
    AC_DEFINE([CTEMPLATE], [], ["build using ctemplate library"])
    AC_MSG_NOTICE([ctemplate will be used, HTML output enabled])
else
    AC_MSG_NOTICE([ctemplate will not be used, HTML output disabled])
fi

In the next step, I changed the Makefile.am at the top level to the following:

if WITH_CTEMPLATE
  MAYBE_CTEMPLATE = ctemplate
endif

SUBDIRS = boost libgsl $(MAYBE_CTEMPLATE) libutil ...

In lower level Makefile.ams, I added

if WITH_CTEMPLATE
    # some change to the configuration
else
    # some other change to the configuration
endif

Finally, I had to make sure that one of the key C++ header files (included by other parts of the code) had the following:

#ifdef HAVE_CONFIG_H
#    include "config.h"
#endif

config.h contains any new definitions one creates with AC_DEFINE, so this file must be included in parts that check whether a macro definition created by this route is defined (or undefined).

This took a lot of time and pushing through frustration on my part; I can only hope documenting this explanation here saves someone else from the same fate.

空宴 2024-08-08 18:20:46

为了防止构建尝试在 ctemplate 子目录中进行编译,您需要执行以下操作:

if CTEMPLATE
SUBDIRS += ctemplate
endif

在 Makefile.am 中

To prevent the build from attempting to compile in the ctemplate subdirectory, you'll want to do something like:

if CTEMPLATE
SUBDIRS += ctemplate
endif

in Makefile.am

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