Makefile自动生成(Automake)

Makefile file makes compiling work more efficient, especially when it turns to solve some jobs with a complex structure. But
it depends on the building environment so hard that we can't use it portably. So that a tools called automake assist us to generate makefile
automatically.

Brief

  1. autoscan
  2. vim configure.scan and mv configure.scan configure.ac
  3. aclocal
  4. autoconf
  5. vim Makefile.am
  6. automake --add-missing
  7. ./configure
  8. make

Example

Project Layout

  1. First of all, create a source code in C:
/* hello.c */
#include <stdio.h>

int main(int argc, char *argv[])
{
	fprintf(stdout, "Hello Auto Makefile\n");
	return 0;
}

then autoscan it, you will find a file named configure.scan

$ autoscan
$ ls
hello.c configure.scan
  1. edit configure.scan and rename it as configure.ac
$ vim configure.scan
$ cat configure.scan

#    -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT( hello.c)
AM_INIT_AUTOMAKE(hello, 1.0)

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT(Makefile)

$ mv configure.scan configure.ac
  1. use aclocal to create aclocal.m4. m4 is a kind of marco processor.
$ aclocal
$ ls
aclocal.m4 configure.ac hello.c
  1. use autoconf to generate configure
$ autoconfig
$ ls
aclocal.m4 autom4te.cache configure configure.ac hello.c
  1. edit makefile.am
$ vim makefile.am
$ cat makefile.am
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
helloworld_SOURCE=hello.c
  1. ./configure
  2. ./make

Copyright © 2021 Gsharp. All Rights Reserved.

posted @ 2021-08-25 23:31  司空亦墨  阅读(106)  评论(0编辑  收藏  举报