Before giving you a detailed introduction to the Linux Makefile, first let everyone know about the Linux Makefile, and then give a comprehensive introduction to the Linux Makefile, hoping to be useful to everyone. Because the graduation design and development platform is Linux, writing Linux Makefile is essential for Linux. To be lazy, I want to use autotools to automatically generate Makefile, after reading a large amount of materials, I made a small experiment based on my understanding. The process was recorded in great detail!
My platform is: HP 6510B Notebook Fedora 8 32-bit Autotools tool versions are fully self-contained in Fedora 8 and have not been upgraded yet! To compile a simple source file main. c, you need to automatically generate a makefile. The steps are as follows:
Linux Makefile Step 1
Create a file main. c In the/root/project/main directory. Its content is as follows:
- #include <stdio.h>
- int main(int argc, char** argv)
- {
- printf("Hello, Auto Makefile!\n");
- return 0;
- }
The status is as follows:
- [root@localhost main]# pwd
- /root/project/main
- [root@localhost main]# ls
- main.c
- [root@localhost main]#
Step 2 of Linux Makefile:
- Run autoscan to automatically create two files:
- The status of autoscan. log configure. scan is as follows:
- [Root @ localhost main] # autoscan
- [Root @ localhost main] # ls
- Autoscan. log configure. scan main. c
- [Root @ localhost main] #
Step 3: Change configure. scan to configure. in to view configure. in:
- # -*- Autoconf -*-
- # Process this file with autoconf to produce a configure script.
-
- AC_PREREQ(2.61)
- AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
- AC_CONFIG_SRCDIR([main.c])
- AC_CONFIG_HEADER([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_OUTPUT
Interpret the above documents:
- #-*-Autoconf -*-
- # Process this file with autoconf to produce a configure script.
- # AC_PREREQ:
- # Make sure that a new Autoconf version is used. If you want to create an Autoconf version for configure
- # This is earlier than version, so an error message is printed in the standard error output and no configure is created.
- AC_PREREQ (2.61)
- #
- # Initialize and define the basic information of the software, including the full name of the Set package, the version number, and the email address required to report the BUG
- #
- AC_INIT (FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
- #
- # Used to detect the existence of the specified source code file to determine the validity of the source code directory
- #
- AC_CONFIG_SRCDIR ([main. c])
- #
- # It is used to generate the config. h file for autoheader to use
- #
- AC_CONFIG_HEADER ([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.
- # Create an output file. Call this macro once at the end of 'configure. in.
- #
- AC_OUTPUT
-
Modify action:
1. Modify the parameters in AC_INIT: AC_INIT (main, 1.0, pgpxc@163.com)
2. Add the macro AM_INIT_AUTOMAKE, which is an essential macro for automake. Similarly, the PACKAGE is the name of the software suite to be generated, and the VERSION is the VERSION number.
3. Add the output file Linux Makefile after AC_OUTPUT
Modified results:
- # -*- Autoconf -*-
- # Process this file with autoconf to produce a configure script.
-
- AC_PREREQ(2.61)
- AC_INIT(main, 1.0, pgpxc@163.com)
- AC_CONFIG_SRCDIR([main.c])
- AC_CONFIG_HEADER([config.h])
- AM_INIT_AUTOMAKE(main,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])
Step 4: Run aclocal,
Generate an "aclocal. m4" file and a buffer folder autom4te. cache. This file mainly processes local macro definitions. The status is:
- [root@localhost main]# aclocal
- [root@localhost main]# ls
- aclocal.m4 autom4te.cache autoscan.log configure.in configure.in~ main.c
- [root@localhost main]#
Step 5: Run autoconf to generate configure. The current status is:
- [root@localhost main]# autoconf
- [root@localhost main]# ls
- aclocal.m4 autoscan.log configure.in main.c
- autom4te.cache configure configure.in~
- [root@localhost main]#
Step 6: Run autoheader to generate the config. h. in file.
This tool usually copies the user-attached symbol definitions from the "acconfig. h" file, so there is no additional symbol definition here, so you do not need to create the "acconfig. h" file. The status is:
- [root@localhost main]# autoheader
- [root@localhost main]# ls
- aclocal.m4 autoscan.log configure configure.in~
- autom4te.cache config.h.in configure.in main.c
- [root@localhost main]#
Step 7: automake will be run below, but preparations should be made before this!
First, create a Linux Makefile. am. This step is an important step to create a Linux Makefile. The script configuration file used by automake is Linux Makefile. am. You need to create the corresponding file by yourself. Then, convert the automake tool to Linux Makefile. in.
The content of this Linux Makefile. am is as follows:
- AUTOMAKE_OPTIONS=foreign
- bin_PROGRAMS=main
- mainmain_SOURCES=main.c
The following describes the corresponding items of the script file. AUTOMAKE_OPTIONS indicates the option to set automake. As GNU has already introduced in chapter 1st), it has strict regulations on software released by itself, such as license declaration file COPYING. Otherwise, an error will be reported during automake execution.
Automake provides three software levels: foreign, gnu, and gnits, allowing users to choose to use them. The default level is gnu. In this example, the foreign level is used to detect only required files. Defines the name of the execution file to be generated. If multiple execution files are to be generated, each file name is separated by a space.
Main_SOURCES defines the original file required by the execution program "main. If the "main" program is generated by multiple original files, all the original files used by the program must be listed and separated by spaces. For example, if the target body "main" requires "main. c "," sunq. c "," main. h ", The main_SOURCES = main. c sunq. c main. h. Note that if you want to define multiple execution files, you need to define the corresponding file_SOURCES for each execution program.
Then, use automake to generate the "configure. in" file. Here, use the "-adding-missing" option to allow automake to automatically add some necessary script files.
The status after running is:
- [root@localhost main]# automake --add-missing
- configure.in:8: installing `./missing'
- configure.in:8: installing `./install-sh'
- Makefile.am: installing `./depcomp'
- [root@localhost main]# ls
- aclocal.m4 config.h.in configure.in~ main.c Makefile.in
- autom4te.cache configure depcomp Makefile.am missing
- autoscan.log configure.in install-sh Makefile.am~
- [root@localhost main]#
Run configure in Step 8 of Linux Makefile,
In this step, the Makefile. in is converted into the final Makefile by running the automatic configuration setting file configure. The running result is as follows:
- [root@localhost main]# ./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... gawk
- checking whether make sets $(MAKE)... yes
- checking for gcc... gcc
- checking for C compiler default output file name... a.out
- checking whether the C compiler works... yes
- checking whether we are cross compiling... no
- checking for suffix of executables...
- 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
- configure: creating ./config.status
- config.status: creating Makefile
- config.status: creating config.h
- config.status: executing depfiles commands
- [root@localhost main]# ls
- aclocal.m4 config.h.in configure.in main.c Makefile.in
- autom4te.cache config.log configure.in~ Makefile missing
- autoscan.log config.status depcomp Makefile.am stamp-h1
- config.h configure install-sh Makefile.am~
- [root@localhost main]#
Run make in Step 9 of Linux Makefile,
Test the configuration file Makefile. The status is as follows:
- [root@localhost main]# make
- cd . && /bin/sh /root/project/main/missing --run aclocal-1.10
- cd . && /bin/sh /root/project/main/missing --run automake-1.10 --foreign
- cd . && /bin/sh /root/project/main/missing --run autoconf
- /bin/sh ./config.status --recheck
- running CONFIG_SHELL=/bin/sh /bin/sh ./configure --no-create --no-recursion
- 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... gawk
- checking whether make sets $(MAKE)... yes
- checking for gcc... gcc
- checking for C compiler default output file name... a.out
- checking whether the C compiler works... yes
- checking whether we are cross compiling... no
- checking for suffix of executables...
- 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
- configure: creating ./config.status
- /bin/sh ./config.status
- config.status: creating Makefile
- config.status: creating config.h
- config.status: config.h is unchanged
- config.status: executing depfiles commands
- cd . && /bin/sh /root/project/main/missing --run autoheader
- rm -f stamp-h1
- touch config.h.in
- make all-am
- make[1]: Entering directory `/root/project/main'
- gcc -DHAVE_CONFIG_H -I. -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
- mv -f .deps/main.Tpo .deps/main.Po
- gcc -g -O2 -o main main.o
- cd . && /bin/sh ./config.status config.h
- config.status: creating config.h
- config.status: config.h is unchanged
- make[1]: Leaving directory `/root/project/main'
- [root@localhost main]# ls
- aclocal.m4 autoscan.log config.h.in config.status configure.in depcomp main main.o Makefile.am Makefile.in stamp-h1
- autom4te.cache config.h config.log configure configure.in~ install-sh main.c Makefile Makefile.am~ missing
- [root@localhost main]#
-
In step 10 of Linux Makefile, run the generated file main:
- [root@localhost main]# ./main
- Hello, Auto Makefile!
- [root@localhost main]#
So far, all the steps have been completed. To understand and complete this experiment, I have spent two days !! This is the most fun to see the final results !! Like yourself!
- Linux Makefile compilation environment starts with helloworld
- The truth about the Linux Makefile system is in line with free software practices.
- Linux Makefile
- Environment in which Linux Makefile is automatically compiled and linked
- Introduction to the Linux Makefile Environment