简体   繁体   English

配置文件模板以生成makefile

[英]configure file template to generate makefile

Almost all the opensource c++ project in Linux has a 'configure' file to generate Makefile before building the source code. 几乎Linux中的所有opensource c ++项目都有一个'configure'文件,用于在构建源代码之前生成Makefile。

I am writing a project from scratch, is there any template for this 'configure' file? 我正在从头开始编写一个项目,这个'configure'文件有没有模板?

Most of the time, the configure files are not created by hand, but generated by tools like autoconf . 大多数情况下,配置文件不是手动创建的,而是由autoconf等工具生成的。 There are more modern alternatives however, which are mostly easier to use. 然而,有更多的现代替代品,它们更容易使用。 Have a look at cmake , qmake for Qt, or scons . 看看cmakeqmake for Qt,或scons

Also have a look at previous questions, eg C++ build systems . 还要看看以前的问题,例如C ++构建系统

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

The configure file is generated through autoconf from a configure.ac file. configure文件通过从autoconf生成configure.ac文件。 Here's my minimal template for configure.ac with C++: 这是使用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

The dnl lines are comments (explanations) for you, you can remove them from your file. dnl行是您的注释(解释),您可以从文件中删除它们。

It assumes you are using automake to generate the Makefile as well. 它假设您正在使用automake来生成Makefile If you don't want that, you need to prepare a Makefile.in by hand (and remove the AM_INIT_AUTOMAKE line). 如果您不想这样,则需要手动准备Makefile.in (并删除AM_INIT_AUTOMAKE行)。

The configuration results will be written to config.h . 配置结果将写入config.h You usually include it like: 你通常包括它:

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

And my template for Makefile.am (which is used by automake): 我的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

After creating those two templates, you usually run autoreconf -vi rather than random tools yourself. 创建这两个模板后,通常自己运行autoreconf -vi而不是随机工具。 This is going to guess what you need and run it. 这将猜测你需要什么并运行它。

Feel free to let you know if you need something more, I'll be happy to explain. 如果您需要更多的东西,请随时告诉您,我很乐意解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM