autoconf and automake to create Makefile

autoconf , autoscan, automake, autoheader and aclocal are tools we will use. You can check if you already installed them by command:

>which autoxxxx

Step 1, mkdir "auto-makefile-test" and enter this folder

Step 2, we write a c code process.c

#include <stdio.h>

void main(int argc, char** argv)
{
        int pid;
        printf("main\n");
        pid=fork();
        if(pid == 0){
                printf("pid is 0\n");
        }
        else
                printf("pid is %d\n",pid);
}

Step 2, Create following text files, you can write any words in these files.

README, INSTALL, AUTHORS, NEWS, ChangeLog.

Step 3, Create Makefile.am and write following code in it:

bin_PROGRAMS=process

process_SOURCES=process.c

Step 4, Run autoscan

>autoscan

chunxie@chunxie-VirtualBox:~/auto-makefile-test$ ls
AUTHORS  ChangeLog  INSTALL  Makefile.am  NEWS  process.c  README
chunxie@chunxie-VirtualBox:~/auto-makefile-test$ 
chunxie@chunxie-VirtualBox:~/auto-makefile-test$ autoscan
chunxie@chunxie-VirtualBox:~/auto-makefile-test$ 
chunxie@chunxie-VirtualBox:~/auto-makefile-test$ ls
AUTHORS       ChangeLog       INSTALL      NEWS       README
autoscan.log  configure.scan  Makefile.am  process.c

Here we can see autoscan create autoscan.log and configure.scan two files. Let's take a look of configure.scan

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

AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([process.c])
AC_CONFIG_HEADERS([config.h])

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

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

Step 5, Move configure.scan to configure.ac

chunxie@chunxie-VirtualBox:~/auto-makefile-test$ mv configure.scan configure.ac
chunxie@chunxie-VirtualBox:~/auto-makefile-test$ ls
AUTHORS       ChangeLog     INSTALL      NEWS       README
autoscan.log  configure.ac  Makefile.am  process.c

Step 6, Add automake to configure.ac as following:

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

AC_PREREQ([2.69])
#AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_INIT(process,1.0)
AM_INIT_AUTOMAKE(process,1.0)
AC_CONFIG_SRCDIR([process.c])
AC_CONFIG_HEADERS([config.h])

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

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

Step 7, Run aclocal and autoconf

chunxie@chunxie-VirtualBox:~/auto-makefile-test$ aclocal
main::scan_file() called too early to check prototype at /usr/local/bin/aclocal line 643.
chunxie@chunxie-VirtualBox:~/auto-makefile-test$ autoconf
chunxie@chunxie-VirtualBox:~/auto-makefile-test$ ls
aclocal.m4  autom4te.cache  ChangeLog  configure.ac  Makefile.am  process.c
AUTHORS     autoscan.log    configure  INSTALL       NEWS         README

aclocal will create aclocal.m4 and autom4te.cache

autoconf will create configure

Step 8, Run autoheader

chunxie@chunxie-VirtualBox:~/auto-makefile-test$ autoheader
chunxie@chunxie-VirtualBox:~/auto-makefile-test$ ls
aclocal.m4      autoscan.log  configure     Makefile.am  README
AUTHORS         ChangeLog     configure.ac  NEWS
autom4te.cache  config.h.in   INSTALL       process.c

autoheader will create config.h.in

Step 9, Run automake

chunxie@chunxie-VirtualBox:~/auto-makefile-test$ automake
configure.ac:7: required file `./install-sh' not found
configure.ac:7:   `automake --add-missing' can install `install-sh'
configure.ac:7: required file `./missing' not found
configure.ac:7:   `automake --add-missing' can install `missing'
Makefile.am: required file `./COPYING' not found
Makefile.am:   `automake --add-missing' can install `COPYING'
Makefile.am: required file `./depcomp' not found
Makefile.am:   `automake --add-missing' can install `depcomp'
chunxie@chunxie-VirtualBox:~/auto-makefile-test$ automake --add-missing
configure.ac:7: installing `./install-sh'
configure.ac:7: installing `./missing'
Makefile.am: installing `./COPYING' using GNU General Public License v3 file
Makefile.am:     Consider adding the COPYING file to the version control system
Makefile.am:     for your code, to avoid questions about which license your project uses.
Makefile.am: installing `./depcomp'
chunxie@chunxie-VirtualBox:~/auto-makefile-test$ ls
aclocal.m4      autoscan.log  configure     depcomp     Makefile.am  NEWS
AUTHORS         ChangeLog     configure.ac  INSTALL     Makefile.in  process.c
autom4te.cache  config.h.in   COPYING       install-sh  missing      README

We can see automake will create depcomp, COPYING, missing, install-sh and Makefile.

And first time it needs --add-missing to create all 4 files.

Now we can see the Makefile

Step 10, Run configure

chunxie@chunxie-VirtualBox:~/auto-makefile-test$ ./configure 
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for pid_t... yes
checking vfork.h usability... no
checking vfork.h presence... no
checking for vfork.h... no
checking for fork... yes
checking for vfork... yes
checking for working fork... yes
checking for working vfork... (cached) yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands

It checks the system.

Step 11, Run make

chunxie@chunxie-VirtualBox:~/auto-makefile-test$ make
make  all-am
make[1]: Entering directory `/home/chunxie/auto-makefile-test'
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT process.o -MD -MP -MF .deps/process.Tpo -c -o process.o process.c
mv -f .deps/process.Tpo .deps/process.Po
gcc  -g -O2   -o process process.o  
make[1]: Leaving directory `/home/chunxie/auto-makefile-test'
chunxie@chunxie-VirtualBox:~/auto-makefile-test$ ls
aclocal.m4      config.h       configure.ac  Makefile     process
AUTHORS         config.h.in    COPYING       Makefile.am  process.c
autom4te.cache  config.log     depcomp       Makefile.in  process.o
autoscan.log    config.status  INSTALL       missing      README
ChangeLog       configure      install-sh    NEWS         stamp-h1

Here we can see the code is built. And we can run it.

chunxie@chunxie-VirtualBox:~/auto-makefile-test$ ./process
main
pid is 655
chunxie@chunxie-VirtualBox:~/auto-makefile-test$ pid is 0

And we can also run make install to install this binary to system. But I don't want to try it.

The End

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值