Automatically generate makefile

Source: Internet
Author: User
Tags automake

Use autotools to create makefile simple instance Parsing

Creating makefile manually is a laborious and error-prone task for a large project. Autotools tools can generate makefile easily by entering a simple target file, dependency file, and file directory. Currently, software development on Linux generally uses autotools to create makefiles.

Autotools mainly include aclocal, autoscan, Autoconf, autoheader, and automake. Autotools is mainly used to generate the final makefile using the script files of each tool. The following describes the specific process with examples.

Step 1: Run autoscan at the highest level of the source code directory tree. Then manually modify the configure. Scan file and change it to configure. AC/configure. In.

Step 2: Run aclocal, which generates the aclocal. M4 File Based on Configure. ac.

Step 3: Run Autoconf, which generates the configuration script file configure Based on Configure. AC and aclocal. M4.

Step 4: manually compile the makefile. Am File

Step 5: Run automake -- add-missing to generate makefile. In Based on makefile. am.

Step 6: Run configure, which generates the MAKEFILE file based on the content of makefile. In.

After obtaining the MAKEFILE file, we can use the make program to manage our projects.

Detailed process:

Step 1: autoscan

Use autoscan to check the source file in the given directory and Its subdirectory tree. If no directory is provided, check the current directory and Its subdirectory tree. Two files are generated: Configure. Scan and autoscan. log.

Configure. Scan is the prototype file of Configure. In. Configure. In is the script configuration file of Autoconf. Therefore, the next step is to modify Configure. Scan and convert it to configure. In.

Step 2 Configure. In

First, change Configure. Scan to configure. In.

The Configure. Scan file contains the following content:

#-*-Autoconf -*-

# Process this file with Autoconf to produce a configure script.

Ac_prereq (2.57)

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

Note:

1. Comments starting.

2. ac_prereq macro specifies the Autoconf version required in this file.

3. The ac_init macro is used to define the software name, version, and other information. The bug-Report-address here can be omitted.

4. The ac_config_srcdir macro is used to detect the existence of the specified source code file and determine the validity of the source code directory. This parameter does not need to be modified.

5. The ac_config_header macro is used to generate the config. h file for autoheader usage.

Add a macro am_init_automake (package, Version) as follows:

#-*-Autoconf -*-

# Process this file with Autoconf to produce a configure script.

Ac_prereq (2.57)

Ac_init (main, 1.0)

Am_init_automake (main, 1.0)

Ac_prog_cc

Ac_output (makefile)

Step 3: Execute aclocal and Autoconf to generate the configure file

The aclocal command generates aclocal. M4. When aclocal. M4 and configure. In are available, you can execute Autoconf to generate the configure executable file.

Step 4: automake

This is an important step. The script configuration file required by automake is makefile. am, which needs to be created by yourself.

Automake_options = foreign

Bin_programs = Main

Main_sources = Main. c fun. c

Automake_options is the option for setting automake. Automake provides three software levels: Foreign, GNU, and gnits, which allow users to choose from. The default level is GNU. Currently, foreign is only used to detect necessary files.

Bin_programs defines the name of the execution file to be generated. If multiple executable files are generated, each file name is separated by a space.

File_sources defines the dependency file of the execution program. Similarly, for multiple execution files, you need to define the corresponding file_sources.

The next step is to use automake to generate the configure. In file for it. Here you can use the option -- adding-missing to allow automake to automatically add necessary script files.

Step 5 run configure

[Root @ localhost root] $ automake -- add-Missing

Run makefile with make command

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.