2

Almost all the opensource c++ project in Linux has a 'configure' file to generate Makefile before building the source code.

I am writing a project from scratch, is there any template for this 'configure' file?

2

3 Answers 3

5

Most of the time, the configure files are not created by hand, but generated by tools like autoconf. There are more modern alternatives however, which are mostly easier to use. Have a look at cmake, qmake for Qt, or scons.

Also have a look at previous questions, e.g. C++ build systems.

4
  • I haven't used scons for a while, but I recall it didn't used to produce makefiles. Has that changed? Commented Aug 23, 2012 at 7:18
  • No, scons doesn't produce makefiles: scons.org/wiki/FrequentlyAskedQuestions#Compatibility_with_make
    – Jakob
    Commented Aug 23, 2012 at 7:21
  • 1
    Autoconf is 'modern' and maintained. And it is easier to use if you intend to have a correctly working build system. Other systems are most like 'reinvent build system yourself'. Commented Aug 23, 2012 at 7:31
  • 1
    I did not mean downplay the role of autoconf. I just found e.g. cmake easier to use, and also see a lot of open source packages move over to cmake instead of using autotools these days.
    – Jakob
    Commented Aug 23, 2012 at 7:56
4

It's generated by a program called Autoconf.

1
  • Sometimes, the configure script is written manually (e.g. for Ocaml or Perl). Commented Aug 23, 2012 at 7:02
4

The configure file is generated through autoconf from a configure.ac file. Here's my minimal template for configure.ac with C++:

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.

It assumes you are using automake to generate the Makefile as well. If you don't want that, you need to prepare a Makefile.in by hand (and remove the AM_INIT_AUTOMAKE line).

The configuration results will be written to 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):

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.