In general, for small projects or toy programs, you can manually write Makefile. But for large projects, manually writing maintenance Makefile becomes a tedious and laborious task.
This article describes the Autotools toolset that automatically generates Makefile files that conform to the Linux specification.
If the reader does not have the Autotools toolset installed, the installation commands are as follows
$ sudo apt-get install Automake
After the installation is complete, the following tools are available,
Aclocal
AutoScan
Autoconf
Autoheader
Automake
General large projects, code organization structure is divided into two, one is all files in one directory of the flat structure, the other is organized by the hierarchy of multi-folder form. Let's look at the first kind.
flat structure of the project using the Autotools toolset
This test code is as follows,
Entry Code INT_ARITHMETIC.C
#include <stdio.h>#include<stdlib.h>#include<unistd.h>#include"sum.h"#include"Sub.h"#include"mul.h"#include"div.h"intMain () {printf ("======== < Integer arithmethic > ========\n"); intx, y; printf ("Enter the integer:"); scanf ("%d%d", &x, &y); intSM =sum (x, y); printf ("sum is:%d\n", SM); intSB =Sub (x, y); printf ("Sub is:%d\n", SB); intml =mul (x, y); printf ("Mul is:%d\n", ML); intDV =divide (x, y); printf ("Div is:%d\n", DV); return 0;}
Auxiliary code, header file,
Sum.h
#ifndef Sum_h_ #define sum_h_int sum (intint y); #endif
Sub.h
#ifndef Sub_h_ #define Sub_h_int sub (intint y); #endif
Mul.h
#ifndef Mul_h_ #define Mul_h_int MUL (intint y); #endif
Div.h
#ifndef Div_h_ #define Div_h_int divide (intint y); #endif
Auxiliary code, implementing the file,
Sum.c
" sum.h " int sum (intint y) { return x + y;}
Sub.c
" Sub.h " int Sub (intint y) { return x- y;}
Mul.c
" mul.h " int mul (intint y) { return x * y;}
Div.c
" div.h " <stdio.h>int divide (intint y) { if0 ) printf ("\nwarning:integer division may have accuracy loss.\n" ); return x/ y;}
1) Under the project folder, run the autoscan command, scan the project directory, generate the configure.scan file, the contents are as follows,
# -*-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 ([int_arithmetic.c ]) ac_config_headers ([config.h]) # Checks for programs. ac_prog_cc# Checks for libraries.# Checks for header files. Ac_check_headers ([stdlib.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics.# Checks for library Functions. Ac_output
Rename configure.scan to configure. AC, and modify its contents as,
# -*-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 (int_ Arithmetic, 0.1, [email protected])am_init_automake (int_arithmetic, 0.1)Ac_config_srcdir ([Int_ ARITHMETIC.C]) ac_config_headers ([config.h]) # Checks for programs. ac_prog_cc# Checks for libraries.# Checks for header files. Ac_check_headers ([stdlib.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics.# Checks for library Functions. ac_config_files ([Makefile]) Ac_output
The meaning of the macro definition in the above configure.ac is as follows
ac_prereq: Declaring the autoconf version number
ac_init: Declare the software name, version number and bug report address
Am_init_automake:automake required information, parameter is software name and version number
the source file name of the Ac_config_srcdir:autoscan detection, used to determine the validity of the directory
Ac_config_headers:autoscan defines the header file to be generated, and subsequent autoheader to use
AC_PROG_CC: Specifies the compiler, the default is GCC
Ac_check_headers:autoscan detected header file
Ac_config_files: Specifies that the build Makefile, if it is a multi-directory structure, can be specified to generate multiple Makefile, separated by spaces, for example,ac_config_files ([Makefile src/makefile])
Ac_output:autoscan Output
2) Run aclocal, generate ACLOCAL.M4 file according to Configure.ac, this file mainly handles various macro definitions
3) Run the autoconf, expand the macro in the CONFIGURE.AC, generate the Configure script, which may be used during aclocal.m4
4) Execute autoheader, generate the config.h.in file, which typically copies the user-attached symbol definition from the "acconfig.h" file. There is no additional symbol definition in this example, so you do not need to create a "acconfig.h" file
5) Create the makefile.am file, the Automake tool will convert makefile.am to makefile.in files according to the parameters in the configure.in, and eventually generate makefile.in by Makefile
automake_options=foreignbin_programs=int_arithmeticint_arithmetic_sources=int_arithmetic.c SUM.C SUB.C mul.c div.c
The explanations of the labels in the above makefile.am,
Automake_options
Automake provides 3 software levels: Foreign, GNU, GnitS, the default level is GNU, in this case, using the foreign level, it detects only the necessary files.
Bin_programs : The name of the executable file to generate, separated by a space if you want to generate more than one executable file.
int_arithmetic_sources: All source files that the executable file relies on.
6) Manually add the necessary files News,readme,authors,changelog
7) Execute automake--add-missing , which generates the Makefile.in file. Use the option "--add-missing" to have Automake automatically add some required script files.
8) Execute ./configure Build Makefile
====>>> This Makefile generation is complete.
If you want to continue with the installation,
9) $ make
$ sudo make install to install the executable file in the/usr/local/bin/directory, you can use it later
) $ sudo make uninstall to remove the installed executable from the/usr/local/bin directory
If you want to publish your software,
$ make dist can be packaged to generate xxx-version.tar.gz files
If you want to clean up the intermediate file,
Make clean
Make Distclean
Multi-folder hierarchy projects using the Autotools toolset
====>>> Thanks to the original author for sharing http://blog.csdn.net/initphp/article/details/43705765
Finish.
Automake-Using the Autotools toolset