【问题标题】:configure file template to generate makefile配置文件模板生成makefile
【发布时间】:2012-08-18 15:36:16
【问题描述】:

Linux 中几乎所有的开源 c++ 项目都有一个“配置”文件,用于在构建源代码之前生成 Makefile。

我正在从头开始编写一个项目,这个“配置”文件有模板吗?

【问题讨论】:

标签: c++ linux makefile


【解决方案1】:

大多数时候,配置文件不是手动创建的,而是由autoconf 等工具生成的。然而,还有更现代的替代品,它们大多更容易使用。查看cmakeqmake(Qt)或scons

还可以查看以前的问题,例如C++ build systems.

【讨论】:

  • 我有一段时间没有使用 scons,但我记得它没有用于生成 makefile。有变化吗?
  • Autoconf 是“现代的”并得到维护。如果您打算拥有一个正常工作的构建系统,它会更容易使用。其他系统更像是“自己重新发明构建系统”。
  • 我不是说淡化 autoconf 的作用。我刚刚发现例如cmake 更容易使用,而且现在很多开源包都转向 cmake 而不是使用 autotools。
【解决方案2】:

它是由一个名为Autoconf 的程序生成的。

【讨论】:

  • 有时,configure 脚本是手动编写的(例如,用于 Ocaml 或 Perl)。
【解决方案3】:

configure 文件是通过 autoconf 从configure.ac 文件生成的。这是我用 C++ 编写的configure.ac 的最小模板:

dnl Minimal autoconf version supported. 2.60 is quite a good bias.
AC_PREREQ([2.60])
dnl Set program name and version. It will be used in output,
dnl and then passed to program as #define PACKAGE_NAME and PACKAGE_VERSION.
AC_INIT([program-name], [program-version])
dnl Keep helpers in build-aux/ subdirectory to not leave too much junk.
AC_CONFIG_AUX_DIR([build-aux])
dnl Enable generation of Makefile.in by automake.
dnl Minimal automake version: 1.6 (another good bias IMO).
dnl foreign = disable GNU rules requiring files like NEWS, README etc.
dnl dist-bzip2 = generate .tar.bz2 archives by default (make dist).
dnl subdir-objects = keep .o files in subdirectories with source files.
AM_INIT_AUTOMAKE([1.6 foreign dist-bzip2 subdir-objects])

dnl Enable silent rules if supported (i.e. CC something.o instead of gcc call)
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES])

dnl Find a good C++ compiler.
AC_PROG_CXX

dnl Write #defines to config.h file.
dnl You need to generate config.h.in, for example using autoheader.
AC_CONFIG_HEADERS([config.h])
dnl Generate Makefile from Makefile.in.
AC_CONFIG_FILES([Makefile])
dnl This generates fore-mentioned files.
AC_OUTPUT

dnl 行是您的 cmets(解释),您可以将它们从文件中删除。

假设您也使用 automake 来生成 Makefile。如果您不想这样,您需要手动准备一个Makefile.in(并删除AM_INIT_AUTOMAKE 行)。

配置结果将写入config.h。你通常包括它:

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

还有我的 Makefile.am 模板(由 automake 使用):

# Programs which will be compiled and installed to /usr/bin (or similar).
bin_PROGRAMS = myprogram

# Source files for program 'myprogram'.
myprogram_SOURCES = src/f1.cxx src/f2.cxx
# Linked libraries for program 'myprogram'.
myprogram_LDADD = -llib1 -llib2

创建这两个模板后,您通常会运行autoreconf -vi,而不是自己运行随机工具。这将猜测您需要什么并运行它。

如果您需要更多信息,请随时告诉您,我很乐意为您解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 2020-01-12
    相关资源
    最近更新 更多