Use autotools to automatically generate makefile

Source: Internet
Author: User
Tags automake

The following is a reference from http:// OS .51cto.com/art/201003/185539.htmand combined with experimental data.

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 system is: Ubuntu 10.04.4 LTS-bit autotools tool Version 2.65. To compile a simple source file hello. C, you need to automatically generate a makefile. The steps are as follows:

 

Linux makefile Step 1

Create a testautomake folder under the Directory and enter the testautomake directory to create a file hello. C with the following content:

 

# Include <stdio. h>

Int main ()

{

Printf ("Hello, auto makefile! \ N ");

Return 0;

}

The status is as follows:

 

Cai @ Ubuntu :~ /Testautomake $ pwd

/Home/CAI/testautomake

Cai @ Ubuntu :~ /Testautomake $ ls

Hello. c

 

Step 2 of Linux makefile:

 

Run autoscan to automatically create two files:

The status of autoscan. Log Configure. Scan is as follows:

Cai @ Ubuntu :~ /Testautomake $ autoscan

Cai @ Ubuntu :~ /Testautomake $ ls

Autoscan. Log Configure. Scan hello. c

Step 3: Change Configure. Scan to configure. In.

MV Configure. Scan Configure. In

View Configure. In:

 

#-*-Autoconf -*-

# Process this file with Autoconf toproduce A configure script.

 

Ac_prereq ([1, 2.65])

Ac_init ([full-package-name], [Version], [bug-Report-address])

Ac_config_srcdir ([Hello. C])

Ac_config_headers ([config. H])

 

# Checks for programs.

Ac_prog_cc

 

# Checks for libraries.

 

# Checks for header files.

 

# Checks for typedefs, structures, andcompiler characteristics.

 

# Checks for library functions.

 

Ac_output

Interpret the above documents:

 

#-*-Autoconf -*-

# Process this file with Autoconf toproduce 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.65)

#

# 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 ([Hello. 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, andcompiler 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 (hello, 1.0, cai@126.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 linuxmakefile after ac_output

 

Modified results:

 

#-*-Autoconf -*-

# Process this file with Autoconf toproduce A configure script.

 

Ac_prereq ([1, 2.65])

Ac_init (hello, 1.0, cai@126.com)

Ac_config_srcdir ([Hello. C])

Ac_config_headers ([config. H])

Am_init_automake (hello, 1.0)

 

# Checks for programs.

Ac_prog_cc

 

# Checks for libraries.

 

# Checks for header files.

 

# Checks for typedefs, structures, andcompiler 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:

 

Cai @ Ubuntu :~ /Testautomake $ aclocal

Cai @ Ubuntu :~ /Testautomake $ ls

Aclocal. M4 autom4te. cache autoscan. Log Configure. In hello. c

Step 5: Run Autoconf to generate Configure. The current status is:

 

Cai @ Ubuntu :~ /Testautomake $ Autoconf

Cai @ Ubuntu :~ /Testautomake $ ls

Aclocal. M4 autom4te. cache autoscan. Log configure Configure. In hello. c

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:

 

Cai @ Ubuntu :~ /Testautomake $ ls

Aclocal. M4 autoscan. Log configure hello. c

Autom4te. cache config. H. In Configure. In

Step 7: automake will be run soon, but preparations should be made before that!

 

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 = Hello

Hello_sources = Main. c

The following describes the corresponding items of the script file. Automake_options indicates the option to set automake. GNU (introduced in chapter 1st) has strict regulations on software released by itself, such as license declaration file copying. Otherwise, an error is 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.

 

Hello_sources defines the original file required by the "hello" executable program. If the "hello" 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 "Main" requires "Hello. c", "m1.c", and "m1.h" dependency files, hello_sources = Hello. c m1.c m1.h is defined. Note that if you want to define multiple execution files, you must define the corresponding file_sources for each execution program. The prefix file value is the value of the bin_programs macro.

 

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:

 

Cai @ Ubuntu :~ /Testautomake $ automake -- add-Missing

Configure. In: 8: Installing './install-Sh'

Configure. In: 8: Installing './missing'

Makefile. AM: Installing './depcomp'

Cai @ Ubuntu :~ /Testautomake $ ls

Aclocal. M4 autoscan. Log configure depcomp install-SH makefile. In

Autom4te. cache config. H. In Configure. In hello. c makefile. am missing

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:

Cai @ Ubuntu :~ /Testautomake $./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 whether the C compiler works... yes

Checking for C compiler default Output Filename... 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 ccompiler... yes

Checking whether GCC accepts-G... yes

Checking for GCC option to accept isoc89... None needed

Checking for style of include used bymake... 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

Cai @ Ubuntu :~ /Testautomake $ ls

Aclocal. M4 config. h config. Status depcomp makefile missing

Autom4te. cache config. H. In configure hello. c makefile. Am stamp-h1

Autoscan. Log config. Log Configure. In install-SH makefile. In

Run make in Step 9 of Linux makefile,

 

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

 

Cai @ Ubuntu :~ /Testautomake $ make

Make all-AM

Make [1]: Entering directory '/home/CAI/testautomake'

Gcc-dhave_config_h-I.-G-O2-MT hello. O-MD-MP-MF. deps/Hello. TPO-c-o hello. O hello. c

MV-F. deps/Hello. TPO. deps/Hello. Po

Gcc-g-O2-O hello. o

Make [1]: Leaving directory '/home/CAI/testautomake'

Cai @ Ubuntu :~ /Testautomake $ ls

Aclocal. M4 config. H. In Configure. In hello. O makefile. In

Autom4te. cache config. Log depcomp install-SH missing

Autoscan. Log config. Status Hello makefile stamp-h1

Config. h configure hello. c makefile. AM

 

In step 10 of Linux makefile, the generated file Hello:

Cai @ Ubuntu :~ /Testautomake $./Hello

Hello, auto makefile!

At this point, all the steps have been completed, and the final results can be seen. This is the most fun !! Like it!

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.