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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4553735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:43:46  来源:igfitidea点击:

GNU autotools: Debug/Release targets?

c++autotoolsautoconfautomake

提问by cpf

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)

我一直在寻找这个:我目前正在将一个中等大小的程序转换为 autotools,来自基于 Eclipse 的方法(带有 makefile)

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:

现在我正在尝试使用 autotools 以某种方式复制它,所以我可以(也许)执行以下操作:

./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

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

采纳答案by ismail

Add a clause to your configure.inor configure.acfile;

在您的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")

Now in your Makefile.inor Makefile.am;

现在在您的Makefile.inMakefile.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.

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

回答by William Pursell

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.

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

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

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

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

您可以通过使用 CONFIG_SITE 文件来减少必要键入的乏味。例如,您可以执行以下操作:

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.

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

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-debugwhich 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 tmuxand 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.

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

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_DEBUGor -DUSE_DEBUGor -I/non/standard/path/to/headers. Perhaps it will need -O145or -Qpassed to the compiler or -debugpassed 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.

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

回答by crenate

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

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

回答by serghei

Another example to configure CFLAGS/CXXFLAGSwithout editing Makefile.inor Makefile.am. Add this code to your configure.inor configure.acfile:

另一个配置CFLAGS/CXXFLAGS无需编辑Makefile.inMakefile.am. 将此代码添加到您的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"

Test it:

测试一下:

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