Steps for Automatically Generating Linux Makefile

Source: Internet
Author: User

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:

 
 
  1. #include <stdio.h>   
  2. int main(int argc, char** argv)   
  3. {   
  4. printf("Hello, Auto Makefile!\n");   
  5. return 0;   
  6. }  

The status is as follows:

 
 
  1. [root@localhost main]# pwd  
  2. /root/project/main  
  3. [root@localhost main]# ls  
  4. main.c  
  5. [root@localhost main]#  

Step 2 of Linux Makefile:

 
 
  1. Run autoscan to automatically create two files:
  2. The status of autoscan. log configure. scan is as follows:
  3. [Root @ localhost main] # autoscan
  4. [Root @ localhost main] # ls
  5. Autoscan. log configure. scan main. c
  6. [Root @ localhost main] #

Step 3: Change configure. scan to configure. in to view configure. in:

 
 
  1. # -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.  
  3.  
  4. AC_PREREQ(2.61)  
  5. AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)  
  6. AC_CONFIG_SRCDIR([main.c])  
  7. AC_CONFIG_HEADER([config.h])  
  8.  
  9. # Checks for programs.  
  10. AC_PROG_CC  
  11.  
  12. # Checks for libraries.  
  13. # Checks for header files.  
  14. # Checks for typedefs, structures, and compiler characteristics.  
  15. # Checks for library functions.  
  16. AC_OUTPUT  

Interpret the above documents:

 
 
  1. #-*-Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.
  3. # AC_PREREQ:
  4. # Make sure that a new Autoconf version is used. If you want to create an Autoconf version for configure
  5. # This is earlier than version, so an error message is printed in the standard error output and no configure is created.
  6. AC_PREREQ (2.61)
  7. #
  8. # 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
  9. #
  10. AC_INIT (FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
  11. #
  12. # Used to detect the existence of the specified source code file to determine the validity of the source code directory
  13. #
  14. AC_CONFIG_SRCDIR ([main. c])
  15. #
  16. # It is used to generate the config. h file for autoheader to use
  17. #
  18. AC_CONFIG_HEADER ([config. h])
  19. # Checks for programs.
  20. AC_PROG_CC
  21. # Checks for libraries.
  22. # Checks for header files.
  23. # Checks for typedefs, structures, and compiler characteristics.
  24. # Checks for library functions.
  25. # Create an output file. Call this macro once at the end of 'configure. in.
  26. #
  27. AC_OUTPUT
  28.  

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:

 
 
  1. #                                               -*- Autoconf -*-  
  2. # Process this file with autoconf to produce a configure script.  
  3.  
  4. AC_PREREQ(2.61)  
  5. AC_INIT(main, 1.0, pgpxc@163.com)  
  6. AC_CONFIG_SRCDIR([main.c])  
  7. AC_CONFIG_HEADER([config.h])  
  8. AM_INIT_AUTOMAKE(main,1.0)  
  9.  
  10. # Checks for programs.  
  11. AC_PROG_CC  
  12. # Checks for libraries.  
  13. # Checks for header files.  
  14. # Checks for typedefs, structures, and compiler characteristics.  
  15. # Checks for library functions.  
  16. 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:

 
 
  1. [root@localhost main]# aclocal  
  2. [root@localhost main]# ls  
  3. aclocal.m4  autom4te.cache  autoscan.log  configure.in  configure.in~  main.c  
  4. [root@localhost main]#  

Step 5: Run autoconf to generate configure. The current status is:

 
 
  1. [root@localhost main]# autoconf  
  2. [root@localhost main]# ls  
  3. aclocal.m4      autoscan.log  configure.in   main.c  
  4. autom4te.cache  configure     configure.in~  
  5. [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:

 
 
  1. [root@localhost main]# autoheader  
  2. [root@localhost main]# ls  
  3. aclocal.m4      autoscan.log  configure     configure.in~  
  4. autom4te.cache  config.h.in   configure.in  main.c  
  5. [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:

 
 
  1. AUTOMAKE_OPTIONS=foreign 
  2. bin_PROGRAMS=main 
  3. 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:

 
 
  1. [root@localhost main]# automake --add-missing  
  2. configure.in:8: installing `./missing'  
  3. configure.in:8: installing `./install-sh'  
  4. Makefile.am: installing `./depcomp'  
  5. [root@localhost main]# ls  
  6. aclocal.m4      config.h.in   configure.in~  main.c        Makefile.in  
  7. autom4te.cache  configure     depcomp        Makefile.am   missing  
  8. autoscan.log    configure.in  install-sh     Makefile.am~  
  9. [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:

 
 
  1. [root@localhost main]# ./configure   
  2. checking for a BSD-compatible install... /usr/bin/install -c  
  3. checking whether build environment is sane... yes  
  4. checking for a thread-safe mkdir -p... /bin/mkdir -p  
  5. checking for gawk... gawk  
  6. checking whether make sets $(MAKE)... yes  
  7. checking for gcc... gcc  
  8. checking for C compiler default output file name... a.out  
  9. checking whether the C compiler works... yes  
  10. checking whether we are cross compiling... no  
  11. checking for suffix of executables...   
  12. checking for suffix of object files... o  
  13. checking whether we are using the GNU C compiler... yes  
  14. checking whether gcc accepts -g... yes  
  15. checking for gcc option to accept ISO C89... none needed  
  16. checking for style of include used by make... GNU  
  17. checking dependency style of gcc... gcc3  
  18. configure: creating ./config.status  
  19. config.status: creating Makefile  
  20. config.status: creating config.h  
  21. config.status: executing depfiles commands  
  22. [root@localhost main]# ls  
  23. aclocal.m4      config.h.in    configure.in   main.c        Makefile.in  
  24. autom4te.cache  config.log     configure.in~  Makefile      missing  
  25. autoscan.log    config.status  depcomp        Makefile.am   stamp-h1  
  26. config.h        configure      install-sh     Makefile.am~  
  27. [root@localhost main]#  

Run make in Step 9 of Linux Makefile,

Test the configuration file Makefile. The status is as follows:

 
 
  1. [root@localhost main]# make  
  2. cd . && /bin/sh /root/project/main/missing --run aclocal-1.10   
  3. cd . && /bin/sh /root/project/main/missing --run automake-1.10 --foreign   
  4. cd . && /bin/sh /root/project/main/missing --run autoconf  
  5. /bin/sh ./config.status --recheck  
  6. running CONFIG_SHELL=/bin/sh /bin/sh ./configure   --no-create --no-recursion  
  7. checking for a BSD-compatible install... /usr/bin/install -c  
  8. checking whether build environment is sane... yes  
  9. checking for a thread-safe mkdir -p... /bin/mkdir -p  
  10. checking for gawk... gawk  
  11. checking whether make sets $(MAKE)... yes  
  12. checking for gcc... gcc  
  13. checking for C compiler default output file name... a.out  
  14. checking whether the C compiler works... yes  
  15. checking whether we are cross compiling... no  
  16. checking for suffix of executables...   
  17. checking for suffix of object files... o  
  18. checking whether we are using the GNU C compiler... yes  
  19. checking whether gcc accepts -g... yes  
  20. checking for gcc option to accept ISO C89... none needed  
  21. checking for style of include used by make... GNU  
  22. checking dependency style of gcc... gcc3  
  23. configure: creating ./config.status  
  24. /bin/sh ./config.status  
  25. config.status: creating Makefile  
  26. config.status: creating config.h  
  27. config.status: config.h is unchanged  
  28. config.status: executing depfiles commands  
  29. cd . && /bin/sh /root/project/main/missing --run autoheader  
  30. rm -f stamp-h1  
  31. touch config.h.in  
  32. make  all-am  
  33. make[1]: Entering directory `/root/project/main'  
  34. gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c  
  35. mv -f .deps/main.Tpo .deps/main.Po  
  36. gcc  -g -O2   -o main main.o    
  37. cd . && /bin/sh ./config.status config.h  
  38. config.status: creating config.h  
  39. config.status: config.h is unchanged  
  40. make[1]: Leaving directory `/root/project/main'  
  41. [root@localhost main]# ls  
  42. aclocal.m4      autoscan.log  config.h.in  config.status  configure.in   depcomp     main    main.o    Makefile.am   Makefile.in  stamp-h1  
  43. autom4te.cache  config.h      config.log   configure      configure.in~  install-sh  main.c  Makefile  Makefile.am~  missing  
  44. [root@localhost main]#   
  45.  

In step 10 of Linux Makefile, run the generated file main:

 
 
  1. [root@localhost main]# ./main  
  2. Hello, Auto Makefile!  
  3. [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!

  1. Linux Makefile compilation environment starts with helloworld
  2. The truth about the Linux Makefile system is in line with free software practices.
  3. Linux Makefile
  4. Environment in which Linux Makefile is automatically compiled and linked
  5. Introduction to the Linux Makefile Environment

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.