GNU 自动工具:调试/发布目标?

发布于 2024-10-09 17:23:57 字数 410 浏览 0 评论 0原文

我一直在寻找这个有一段时间了:我目前正在将一个中型程序转换为自动工具,来自基于 Eclipse 的方法(带有 makefile)

我总是习惯于进行“调试”构建,所有调试符号且无优化,以及“发布”版本,无调试符号和最佳优化。

现在我尝试使用自动工具以某种方式复制此内容,因此我(也许)可以执行以下操作:

./configure
make debug

其中将具有所有调试符号且没有优化,并且其中:

./configure
make

将导致“发布”版本(默认)

PS:我已经阅读过有关 --enable-debug 标志/功能的信息,但在我当前的(简单)设置​​中,configure 无法识别使用它

I've been looking for this for a while: I'm currently converting a medium-size program to autotools, coming from an Eclipse-based method (with makefiles)

I'm always used to having a "debug" build, with all debug symbols and no optimizations, and a "release" build, without debug symbols and best optimizations.

Now I'm trying to replicate this in some way with autotools, so I can (perhaps) do something like:

./configure
make debug

Which would have all debug symbols and no optimizations, and where:

./configure
make

Would result in the "release" version (default)

PS: I've read about the --enable-debug flag/feature, but in my current (simple) setup, using that is unrecognized by configure

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

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

发布评论

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

评论(4

始于初秋 2024-10-16 17:23:57

ismail 的解决方案是一种常见的方法,但它存在一些严重的问题。如果用户尝试通过执行“./configure --enable-debug”来获取调试版本,则配置脚本会将 CFLAGS 设置为“-g -O2”,并且 Makefile 将使用“-g3 -O0 ... -g” -O2' 构建任何可执行文件时。在这种情况下,gcc 将使用 -O2,并且某些编译器将由于 -O 选项冲突而中止。这两种情况都不是预期的行为。

项目维护者根本不应该担心是否使用调试符号进行构建。这对于用户来说是一个问题。如果您正在构建项目并且想要进行调试构建或发布构建,则应该在配置时使用不同的选项。例如,

$ mkdir debug
$ mkdir release
$ cd debug && /path/to/configure --prefix=/dbg \
   CPPFLAGS=-DDEBUG CFLAGS="-g -O0" && make && make install
$ cd ../release && /path/to/configure CPPFLAGS=-DNDEBUG && make && make install

这将在 /dbg/bin 中安装带有“-DDEBUG”和“-g -O0”的构建(“调试构建”),并在 /usr/local/bin 中安装“release”,

您可以减少繁琐的工作使用 CONFIG_SITE 文件进行必要的键入。例如,您可以执行以下操作:

echo 'CPPFLAGS=-DDEBUG CFLAGS="-g -O0"' >> /dbg/share/config.site

然后所有将来的“configure --prefix=/dbg”调用将自动继承 CPPFLAGS 和 CFLAGS 的设置,而无需在命令行上指定。

如果作为包维护者,您希望为用户提供一种构建“调试版本”的简单方法,那么在发行版中包含一个脚本是完全可以接受的,该脚本使用适当的参数调用配置脚本并调用 制作&& make install,但是绝对没有必要在你的自动工具图元文件中添加这样的垃圾。它根本不属于那里。请注意,许多软件包都尝试添加 --enable-debug 这完全是错误的。如果用户调用configure CFLAGS="-g -O0",但得到的构建应用了意外标志,那么您就会遇到错误,并且您的软件包已损坏。这是一种非常常见的经历,如果您维护一个包(当前正在考虑 tmux 和 curl),其中用户无法获得任何合理的人会称之为调用 configure CFLAGS="-g -O0" 后“调试构建”,那么您的包就会损坏

使用自动工具维护包时必须始终记住的重要一点是,用户可能使用与您完全不同的工具链。用户的工具链完全有可能需要 -DMAKE_IT_A_DEBUG-DUSE_DEBUG-I/non/standard/path/to/headers 。也许它需要将 -O145-Q 传递给编译器,或者将 -debug 传递给链接器,或者......任何东西。作为维护者,您根本没有必要的信息,甚至无法使“调试构建”一词对所有用户都有意义。因此,不要尝试,因为您可能会导致某些用户无法构建该软件。

ismail's solution is a common approach, but it suffers from some serious problems. If the user tries to get a debug build by doing './configure --enable-debug', the configure script will set CFLAGS to '-g -O2' and the Makefile will use '-g3 -O0 ... -g -O2' when building any executables. In that case, gcc will use -O2, and some compilers will abort because of the conflicting -O options. Either scenario is not the expected behavior.

Building with debug symbols or not is NOT something the project maintainer should worry about at all. This is an issue for the user. If you are building a project and you want to make a debug build or a release build, you should use different options at configure time. For example,

$ mkdir debug
$ mkdir release
$ cd debug && /path/to/configure --prefix=/dbg \
   CPPFLAGS=-DDEBUG CFLAGS="-g -O0" && make && make install
$ cd ../release && /path/to/configure CPPFLAGS=-DNDEBUG && make && make install

This will install a build with `-DDEBUG' and '-g -O0' (a "debug build") in /dbg/bin and a 'release' install in /usr/local/bin

You can reduce the tedium of the necessary typing by using a CONFIG_SITE file. For example, you can do:

echo 'CPPFLAGS=-DDEBUG CFLAGS="-g -O0"' >> /dbg/share/config.site

and then all future invocations of 'configure --prefix=/dbg' will automatically inherit the settings to CPPFLAGS and CFLAGS without needing to be specified on the command line.

If, as the package maintainer, you want to provide the user with an easy way to build a "debug release", it is perfectly acceptable to include a script in the distribution that invokes the configure script with the appropriate arguments and invokes make && make install, but there is absolutely no need to litter your autotool metafiles with such cruft. It simply does not belong there. And be warned, many packages have made attempts to add --enable-debug which are simply wrong. If the user invokes configure CFLAGS="-g -O0" but gets a build that applies unexpected flags then you have a bug and your package is broken. This is an all too common experience, and if you maintain a package (currently thinking about tmux and curl) in which the user does not get what any reasonable person would call a "debug build" after invoking configure CFLAGS="-g -O0", then your package is broken.

An important point that must always be remembered when maintaining a package with the autotools is that the user may be using a completely different tool chain than you are. It is entirely possible that the user's tool chain will require -DMAKE_IT_A_DEBUG or -DUSE_DEBUG or -I/non/standard/path/to/headers. Perhaps it will need -O145 or -Q passed to the compiler or -debug passed to the linker, or ... anything. As the maintainer, you simply do not have the information necessary to even make the phrase "debug build" meaningful for all users. So don't try, because you might make the software unbuildable for a certain set of users.

栖迟 2024-10-16 17:23:57

在您的 configure.inconfigure.ac 文件中添加一个子句;

AC_ARG_ENABLE(debug,
AS_HELP_STRING([--enable-debug],
               [enable debugging, default: no]),
[case "${enableval}" in
             yes) debug=true ;;
             no)  debug=false ;;
             *)   AC_MSG_ERROR([bad value ${enableval} for --enable-debug]) ;;
esac],
[debug=false])

AM_CONDITIONAL(DEBUG, test x"$debug" = x"true")

现在在您的 Makefile.inMakefile.am 中;

if DEBUG
AM_CFLAGS = -g3 -O0
AM_CXXFLAGS = -g3 -O0
else
AM_CFLAGS = -O2
AM_CXXFLAGS = -O2
endif

因此,当启用调试时,您可以修改{C/CXX}FLAGS以启用调试信息。

Add a clause to your configure.in or configure.ac file;

AC_ARG_ENABLE(debug,
AS_HELP_STRING([--enable-debug],
               [enable debugging, default: no]),
[case "${enableval}" in
             yes) debug=true ;;
             no)  debug=false ;;
             *)   AC_MSG_ERROR([bad value ${enableval} for --enable-debug]) ;;
esac],
[debug=false])

AM_CONDITIONAL(DEBUG, test x"$debug" = x"true")

Now in your Makefile.inor Makefile.am;

if DEBUG
AM_CFLAGS = -g3 -O0
AM_CXXFLAGS = -g3 -O0
else
AM_CFLAGS = -O2
AM_CXXFLAGS = -O2
endif

So when debugis enabled you can modify your {C/CXX}FLAGSto enable debug information.

秋叶绚丽 2024-10-16 17:23:57

使用 autotools 创建的默认 Makefile 会生成带有调试符号的二进制文件。使用 make install-strip 生成发布目标。

The default Makefile created with autotools produces binaries with debug symbos. Use make install-strip to produce a release target.

惯饮孤独 2024-10-16 17:23:57

另一个在不编辑 Makefile.inMakefile.am 的情况下配置 CFLAGS/CXXFLAGS 的示例。将此代码添加到您的 configure.inconfigure.ac 文件中:

test -z "$SED" && SED=sed

AC_ARG_ENABLE([debug],
  [AS_HELP_STRING([--enable-debug],
                  [whether to include debug symbols (default is no)])],
  [enable_debug=$enableval],
  [enable_debug=no]
)

if test "x$enable_debug" = xyes; then
  dnl Remove all optimization flags from CFLAGS
  changequote({,})
  CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O[0-9s]*//g'`
  CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-O[0-9s]*//g'`

  CFLAGS=`echo "$CFLAGS" | $SED -e 's/-g[0-9]*//g'`
  CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-g[0-9]*//g'`
  changequote([,])

  CFLAGS="$CFLAGS -g -O0"
  CXXFLAGS="$CXXFLAGS -g -O0"
fi

echo "CFLAGS=$CFLAGS"

测试一下:

$ ./configure --enable-debug | grep CFLAGS

Another example to configure CFLAGS/CXXFLAGS without editing Makefile.in or Makefile.am. Add this code to your configure.in or configure.ac file:

test -z "$SED" && SED=sed

AC_ARG_ENABLE([debug],
  [AS_HELP_STRING([--enable-debug],
                  [whether to include debug symbols (default is no)])],
  [enable_debug=$enableval],
  [enable_debug=no]
)

if test "x$enable_debug" = xyes; then
  dnl Remove all optimization flags from CFLAGS
  changequote({,})
  CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O[0-9s]*//g'`
  CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-O[0-9s]*//g'`

  CFLAGS=`echo "$CFLAGS" | $SED -e 's/-g[0-9]*//g'`
  CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-g[0-9]*//g'`
  changequote([,])

  CFLAGS="$CFLAGS -g -O0"
  CXXFLAGS="$CXXFLAGS -g -O0"
fi

echo "CFLAGS=$CFLAGS"

Test it:

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