Skip to content

Autotools template for new projects in one page

LIU Hao edited this page May 10, 2023 · 7 revisions

Commands to build an autotools project

autoreconf -i    # add `-f` to overwrite existing files
./configure      #   # append for example `CFLAGS='-O3 -g'` if you need more options
make -j$(nproc)

configure.ac

AC_INIT([my_project], [version], [bug-report], [tarname], [url])

AC_LANG([C])                            # use `C++` if you want C++
AC_CONFIG_SRCDIR([my_project/main.c])   # specify any source file
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIR([m4])
AC_PROG_CC                              # use `AC_PROG_CXX` if you want C++

LT_INIT([])        # delete this if you need not build libraries
LT_LANG([C])       # delete this if you need not build libraries

AM_INIT_AUTOMAKE([foreign subdir-objects])
AM_SILENT_RULES([yes])

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

Makefile.am

ACLOCAL_AMFLAGS = -I m4
END =

## Compiler options
AM_CPPFLAGS = -pipe -Wall -Wextra   # C pre-processor flags
AM_CFLAGS = -std=c99                # C compiler and linker flags
AM_CXXFLAGS = std=c++14             # C++ compiler and linker flags

AM_DEFAULT_SOURCE_EXT = .c    # use a proper suffix for test source files, to allow implicit rules
LDADD =                       # add libraries that are to be built, mainly for implicit rules

## Initialization
noinst_LIBRARIES =
noinst_LTLIBRARIES =

include_HEADERS =
lib_LIBRARIES =
lib_LTLIBRARIES =
bin_PROGRAMS =

check_HEADERS =
check_LIBRARIES =
check_LTLIBRARIES =
check_PROGRAMS =

BUILT_SOURCES =
CLEANFILES =
EXTRA_DIST =
TESTS = ${check_PROGRAMS}

## Programs and libraries
include my_project/Makefile.inc.am

## Tests
include test/Makefile.inc.am

my_project/Makefile.inc.am

## Installable binary files
lib_LTLIBRARIES += lib/libmylibrary.la
bin_PROGRAMS += bin/myprogram

## Installable headers
##   These should include generated files.
nobase_include_HEADERS =  \
  %reldir%/header_1.h  \
  %reldir%/header_2.h  \
  ${END}

## Source files and non-installable headers
##   These should include generated files. `noinst_LIBRARIES` (not
##   `noinst_LTLIBRARIES`) shall be used for non-installable static
##   libraries.
lib_libmylibrary_la_SOURCES =  \
  %reldir%/mylibrary_source_1.c  \
  %reldir%/mylibrary_source_2.c  \
  ${END}

bin_myprogram_SOURCES =  \
  %reldir%/myprogram_source_1.c  \
  %reldir%/myprogram_source_2.c  \
  ${END}

test/Makefile.inc.am

check_PROGRAMS +=  \
  %reldir%/test_program_1.test  \
  %reldir%/test_program_2.test  \
  ${END}

.gitignore

*~
.*
*.lo
*.o
*.so
*.la
*.a
*.trs
*.test
*.log
*.gch
*.exe
*.dll

/aclocal.m4
/autom4te.cache/
/build-aux/
/m4/
/configure
/Makefile
/Makefile.in
/config.h
/config.h.in
/config.status
/libtool
/stamp-h1
/my_project/version.h
/my_project/precompiled.x*

/lib/
/bin/
/sbin/
/tmp/
/var/

*.save*
*.swp
*.*~
*.orig
*.rej
*.out
*.tar
*.tar.*
*.deb
*.stackdump
core.*
callgrind.*