Makefile writing and Automake finishing of Linux

Source: Internet
Author: User
Tags configuration settings automake

Makefile Writing Rules


In a makefile, it usually contains the following content:
1 The target object, usually the target file or executable file, that needs to be created by the Make tool
2 files that are dependent on the target body to be created (Dependency_file)
3 command to run when creating each target body, this line must start with a tab (Tab key)
Format:
Target:dependency_files
Command/* The line must start with the TAB key */

For example, there are two files for hello.c and hello.h, the target body being created is hello.o, the command executed is the GCC compiler directive: gcc–c hello.c, then the corresponding makefile can be written as:
#The simplest example
HELLO.O:HELLO.C hello.h
Gcc–c Hello.c–o hello.o

Perform:
[email protected] makefile]# make HELLO.O
Gcc–c Hello.c–o hello.o
[[email protected] makefile]# ls
HELLO.C hello.h hello.o Makefile


Makefile variable


user-defined variables:
Makefile variables are defined in two ways: (using the Format: $ (VAR))
Recursive expansion method: Can be very good to complete the user instruction, but the variable cannot append content, may cause infinite loop: definition Format: Var=var
Simple: The value of the variable is expanded at the definition and is expanded only once, and does not contain any references to other variables: definition format: Var:=var


Variable Name:
Variable name is case sensitive
It is recommended to use lowercase letters as variable names inside makefile, and to reserve uppercase letters as variable names for controlling implied rule parameters or user overloading of command option parameters.
Do not include any strings of ":", "#", "=", and trailing spaces, and the names of variables that contain letters, numbers, and underscores should be avoided as much as possible, as they may be given special meaning in the future


common pre-defined variables:

The name of the AR library file maintenance program, the default value is AR
The name of the as assembler, with the default value as
The name of the cc C compiler, the default value is CC
CPP c Pre-compiler name, default value is $ (CC) –E
CXX C + + compiler name, default value is g++
The name of the FC Fortran compiler, the default value is F77
The name of the RM file removal program, the default value is Rm–f
Arflags library file maintenance program options, no default values
Asflags options for assembler, no default values
CFLAGS C Compiler Options, no default values
Cppflags C precompiled option, no default value
Cxxflags options for the C + + compiler, no default values
Fflags Fortran compiler option, no default value


Common automatic variables:

[email protected] The full name of the target file
$* the name of the target file that does not contain an extension
$< the name of the first dependent file
$% if the target is an archive member, the variable represents the target's archive member name

$+ all dependent files, separated by spaces, and in order of occurrence, may contain duplicate dependent files
$? All timestamps are dependent files that are later than the target file and are separated by a space
$^ all non-repeating dependent files, separated by spaces

Environment variables can also be used in makefile. Using environment variables is relatively straightforward, and make automatically reads the environment variables currently defined by the system at startup, and creates a variable with the same name and value. User-defined variables override environment variables with the same name.


Makefile Rules

Implicit rules:
Simply list the target file, make automatically searches the implicit rules directory to determine how to generate the target file, it can only find a different suffix name file of the same file name, such as "KANG.O" file must be generated by "kang.c" file

Common implicit rule directories in makefile
C compile:. C changed to. o $ (CC) –c $ (cppflags) $ (CFLAGS)
C + + compilation:. CC OR. C changed to. o $ (CXX)-C $ (cppflags) $ (cxxflags)
Pascal compilation:. P changed to. o $ (PC)-C $ (pflags)
FORTRAN compilation:. R changes to-O $ (FC)-C $ (fflags)


Pattern rule:To define the same processing rules of multiple files, the implicit rules can only be used to make the default variables to operate, and the schema rules can also introduce user-defined variables, for multiple files to establish the same rules, the utility model rules when the relevant files must be marked with "%"

For example:
DAVID:KANG.O YUL.O
GCC KANG.O bar.o-o Myprog
KANG.O:KANG.C kang.h Head.h
Gcc–wall–o-g–c Kang.c-o KANG.O
YUL.O:BAR.C head.h
Gcc-wall–o-g–c Yul.c-o YUL.O

After modification:(recursive expansion mode)
OBJS = KANG.O YUL.O
CC = gcc
CFLAGS =-wall-o-G
David: $ (OBJS)
$ (CC) $ (OBJS)-O David
KANG.O:KANG.C kang.h
$ (CC) $ (CFLAGS)-C Kang.c-o KANG.O
YUL.O:YUL.C yul.h
$ (CC) $ (CFLAGS)-C Yul.c-o YUL.O

After rewriting:(automatic variable)
OBJS = KANG.O YUL.O
CC = gcc
CFLAGS =-wall-o-G
David: $ (OBJS)
$ (CC) $^-o [email protected]
KANG.O:KANG.C kang.h
$ (CC) $ (CFLAGS)-C $<-o [email protected]
YUL.O:YUL.C yul.h
$ (CC) $ (CFLAGS)-C $<-o [email protected]

After modification:(implicit rules)
OBJS = KANG.O YUL.O
CC = gcc
CFLAGS =-wall-o-G
David: $ (OBJS)
$ (CC) $^-o [email protected]

After rewriting:(Pattern rule)
OBJS = KANG.O YUL.O
CC = gcc
CFLAGS =-wall-o-G
David: $ (OBJS)
$ (CC) $^-o [email protected]
%.O:%.c
$ (CC) $ (CFLAGS)-C $<-o [email protected]


use of MAKE commands
Simply type the target name at the end of the make command to establish the specified target, and if you run make directly, establish the first target in the makefile

command-line options for make

-C dir reads the makefile in the specified directory
-F file reads the file files in the current directory as makefile
-I ignores all command execution errors
-I dir specifies the directory where the makefile is contained
-N Prints only the command to be executed, but does not execute these commands
-p display make variable database and implied rules
-S does not display commands when executing commands
-W If make changes the directory during execution, the current directory name is printed


Autotools
Generate makefile and have the Make function
Which command to view aclocal AutoScan autoconf Autoheader automake whether installed

Vim hello.c#include <stdio.h>int sum (int); int main () {    printf ("The sum of 1-50 is%d\n", SUM (50));} int sum (int n) {    int i,sum;    for (i = 1,sum = 0; I <= N; ++i) {        sum + = i;    }    return sum;}

Process:
1. AutoScan
Checks the source file in the given directory and its subtree, and checks in the current directory and its subtree if no directory is given. It searches the source file for general portability issues and creates a file "Configure.scan", which is the "configure.in" prototype to be used for the next autoconf

[email protected] makefile]# LL
Total Dosage 4
-rw-r--r--1 root root 207 April 10:15 hello.c
[Email protected] automake]# AutoScan
[email protected] automake]# LL
Total Dosage 8
-rw-r--r--1 root root 0 April 10:18 autoscan.log
-rw-r--r--1 root root 467 April 10:18 Configure.scan
-rw-r--r--1 root root 207 April 10:15 hello.c


2. Modify Configure.scan
Configure.in is the autoconf script configuration file, its prototype file "Configure.scan"

#-*-Autoconf-*-# Process This file with Autoconf to produce a configure script. Ac_prereq (2.63) #AC_INIT (full-package-name,version,bug-report-address) ac_init (hello,1.0) am_init_automake (Hello, 1.0) Ac_config_srcdir ([hello.c]) Ac_config_header ([config.h]) # Checks for programs. ac_prog_cc# Checks for libraries.# Checks for headers files.# Checks for typedefs, structures, and compiler characteristics . # Checks for library functions. Ac_config_files ([makefile]) Ac_output

Script File Explanation:
1 lines that begin with the # number are comments.
2 Ac_prereq Macros declare the autoconf version required for this file, as in this example, version 2.59.
3 Ac_init macro used to define the name and version of the software and other information, in this case omitted the bug-report-address, generally the author's e-mail.
4 Am_init_automake is a necessary macro automake, so that Automake automatically generated makefile.in, as before, the package is the name of the software suite to generate, version is the release number, can also be written without parameters directly AM_ Init_automake.
5 Ac_config_srcdir macros are used to check the existence of the specified source files and to determine the validity of the source directory. Here the source file is the current directory of the HELLO.C.
6 Ac_config_header macros are used to generate config.h files for autoheader use.
7 Ac_config_files macros are used to generate the appropriate makefile file.
8 in the middle of the comments can be added to the user test program, test function library, test header file and other macro definitions.

Overwrite "Configure.scan" file and rename to "configure.in"

3. Autoheader
It then uses the Autoheader command, which is responsible for generating the config.h.in file. The tool will typically copy the user-attached symbol definition from the "acconfig.h" file, since there is no symbolic definition attached, so there is no need to create a "acconfig.h" file


4 aclocal
Use aclocal to generate a "aclocal.m4" file that mainly handles local macro definitions


5. Automake
This step is an important step in creating makefile, Automake the script profile to use is makefile.am, and the user needs to create the appropriate files themselves. After that, the Automake tool is converted into makefile.in.

automake_options=foreignbin_programs= hellohello_sources= hello.c

Explain:
1 automake_optionsOption to set the Automake. GNU has strict specifications for the software it publishes, such as the need to attach a license statement file copying, etc., otherwise the Automake will error when executed. Automake offers 3 software levels: foreign, GNU, and GnitS, which allows the user to choose the default level of GNU. In this example, the foreign level is used to detect only the required files.
2 Bin_programsDefines the execution file name to be generated. If you want to produce multiple execution files, each file name is separated by a space.
3 hello_sourcesDefine "Hello" as the original file required for the execution of the program. If the "Hello" program is generated by more than one original file, all the original files it uses must be listed and separated by a space. For example: if the target body "hello" needs "hello.c", "DAVID.C", "hello.h" three dependent files, then define HELLO_SOURCES=HELLO.C david.c hello.h. It is important to note that if you are defining multiple execution files, you must define the corresponding file_sources for each executor

You can then use the Automake command to generate a "configure.in" file, where the option "-a" (or "-adding-missing") allows Automake to automatically add some required script files

[Email protected] automake]# vim makefile.am
[[email protected] automake]# automake–a (or Automake--add-missing)
Configure.in:installing './install-sh '
Configure.in:installing './missing '
Makefile.am:installing ' Depcomp '
[[email protected] automake]# ls
ACLOCAL.M4 autoscan.log configure.in hello.c makefile.am missing
Autom4te.cache Configure Depcomp Install-sh makefile.in config.h.in

The makefile.in file is generated after the Automake


6 autoconf
Run autoconf to generate the "Configure" executable file


7 Running Configure
Run the automatic configuration settings file configure, turn makefile.in into the final makefile

When running configure, the system information is collected and can be configured by the user in the Configure command.
There are two types of custom parameters for./configure, one is switch (--enable-xxx or--disable-xxx), the other is open, which is followed by a string of character (--with-xxx=yyyy) parameters.
The "Config.log" file in the same directory can be easily debugged.

Approximate flowchart:


Follow-up work:

The makefile generated by the Autotools
1 make to generate executable files
2 make install to the system directory
3 Make clean makes clears the previously compiled executable and target files (object file, *.O)
4 Make dist to package programs and related documents as a zipped document for publishing
5 making Debian or RMP packages (follow-up)

Example steps:
[[email protected] automake]# AutoScan [[email protected] automake]# lsautoscan.log configure.scan hello.c[ [email protected] automake]# vim configure.scan [[email protected] automake]# mv Configure.scan configure.in [[email protected] automake]# lsautoscan.log configure.in hello.c[[email protected] automake]# Autoheader [ [email protected] automake]# lsautom4te.cache autoscan.log config.h.in configure.in hello.c[[email  Protected] automake]# aclocal[[email protected] automake]# lsaclocal.m4 autom4te.cache autoscan.log config.h.in C Onfigure.in hello.c[[email protected] automake]# vim makefile.am[[email protected] automake]# automake- Aconfigure.in:6: Installing './install-sh ' Configure.in:6: Installing './missing ' makefile.am:installing './depcomp ' [  [email protected] automake]# lsaclocal.m4 autom4te.cache autoscan.log config.h.in configure.in depcomp hello.c Install-sh makefile.am makefile.in MISSING[[EMAIL&NBSp;protected] automake]# autoconf[[email protected] automake]# lsaclocal.m4 autom4te.cache autoscan.log config.h . In Configure configure.in depcomp hello.c install-sh makefile.am makefile.in missing[[email protected] Autom ake]#./configure checking for a bsd-compatible install .../usr/bin/install-cchecking whether build environment is sane. . Yeschecking for a thread-safe mkdir-p .../bin/mkdir-pchecking for gawk ... gawkchecking whether make sets $ (make) ... Yesc Hecking for gcc ... gccchecking for C compiler default output file name ... a.outchecking whether the C compiler works ... ye Schecking whether we is cross compiling ... nochecking for suffix of executables ... checking for suffix of object files: . Ochecking whether we are using the GNU C compiler ... yeschecking whether GCC accepts-g ... yeschecking for GCC option to a Ccept ISO C89 ... none neededchecking for style's include used by make ... Gnuchecking dependency style of gcc ... gcc3configure:creating./config. statusconfig.status:creating makefileconfig.status:creating config.hconfig.status:executing depfiles commands[[ Email protected] automake]# lsaclocal.m4 autoscan.log config.h.in config.status configure.in hello.c Mak Efile makefile.in stamp-h1autom4te.cache config.h config.log Configure Depcomp install-sh Makefil e.am missing[[email protected] automake]# cat Makefile


Makefile writing and Automake finishing of Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.