Linux Makefile Auto-generated

Source: Internet
Author: User
Tags manual writing perl script automake

I. Introduction of Linux Makefile

Linux makefile is used to automatically compile and link, a project has a lot of files, each file changes will lead to the project re-link, but not all the files need to be recompiled, Linux Makefile records the information of the file, When make determines which files need to be recompiled at the time of the link.

The purpose of Linux makefile is to let the compiler know what other files are needed to compile a file. When those dependent files are changed, the compiler automatically discovers that the final makefile is obsolete and compiles the appropriate modules.

The basic structure of Linux makefile is not very complex, but when a program developer begins to write Linux makefile, it often suspects that their writing is customary and that their makefile are often associated with their own development environment. Linux makefile may have to be modified after the system environment variable or path has changed. This creates a lot of problems with manual writing makefile, Automake just helps us to solve these problems.

With Automake, program developers only need to write some simple files containing predefined macros, generated configure by autoconf based on a macro file, by Automake generates a makefile.in from another macro file, and then uses configure to generate a compliant Linux Makefile based on makefile.in. Below we will describe in detail the Automake generation method of Linux makefile.

II. environment used by Linux makefile

The program mentioned in this article is based on the Linux distribution: Fedora Core Release 1, which contains the autoconf,automake we want to use.

Third, the Linux makefile starts from the HelloWorld

We start with the example program HelloWorld we use most often. The following procedure is simply to say: Create a new three file:

    1. Helloworld.c
    2. Configure.in
    3. makefile.am

Then execute: aclocal; Autoconf Automake--add-missing;./configure; make;/helloworld can see that makefile is generated and can be HELLOWORLD.C compiled. Quite simply, a few commands can make a makefile, how do you feel.

Now let's start with the detailed procedure:

1, build a directory in your working directory to build a HelloWorld directory, we use it to store HelloWorld programs and related files, such as under the/home/my/build:

 
   
  

2, HELLOWORLD.C then use your own favorite editor to write a hellowrold.c file, such as command: VI helloworld.c. Use the following code as the content of the HELLOWORLD.C.

 
   
  

Save exit when finished. Now in the HelloWorld directory there should be a helloworld.c you wrote yourself.

3. Generate configure we use the AutoScan command to help us generate a configure.in template file based on the source code in the directory. Command:

 
   
  

After execution, a file is generated in the Hellowrold directory: Configure.scan, we can take it as a blueprint for configure.in. Now rename Configure.scan to Configure.in, and edit it to modify it to remove extraneous statements as follows:

Configure.in content Start

 
   
  

Configure.in End of content

Then execute the commands aclocal and autoconf, respectively, will produce ACLOCAL.M4 and configure two files:

 
   
  

As you can see, configure.in content is a macro definition that, after autoconf processing, becomes a shell script that examines system features, environment variables, and the parameters required by the software. Autoconf is a tool used to generate automatic configuration software source code scripts (configure). The Configure script can run independently of the autoconf and does not require user intervention during the run.

To generate the Configure file, you must tell autoconf how to find the macro you are using. The way is to use the Aclocal program to generate your ACLOCAL.M4. Aclocal automatically generates ACLOCAL.M4 files based on the contents of the Configure.in file. Aclocal is a Perl script that is defined as: "Aclocal-create aclocal.m4 by scanning configure.ac".

Autoconf creates configure from configure.in, a template file that enumerates the various parameters required to compile the software. Autoconf needs the GNU M4 macro processor to handle ACLOCAL.M4, generating configure scripts.

M4 is a macro processor. Copies the input to the output while expanding the macro. A macro can be either inline or user-defined. In addition to the ability to expand macros, M4 also has built-in functions for referencing files, executing commands, integer arithmetic, text manipulation, looping, and so on. M4 can be either the front end of the compiler or a single macro processor.

4, new makefile.am file, command: $ vi makefile.am content as follows:

 
   
  

Automake will generate makefile.in based on the Linux makefile.am you write. Macros and targets defined in makefile.am instruct Automake to generate the specified code. For example, macro Bin_programs will cause the compilation and connection targets to be generated.

5. Run Automake command

 
   
  

Automake will produce some files based on the makefile.am file, including the most important Linux makefile.in.

6. Execute configure to generate Linux Makefile
$./configure
Checking for a bsd-compatible install .../usr/bin/install-c
Checking whether build environment is sane ... yes
Checking for gawk ... gawk
Checking whether make sets $ (make) ... yes
Checking for gcc ... gcc
Checking for C compiler default output ... a.out
Checking whether the C compiler works ... yes
Checking whether we is 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 ANSI C ... none needed
Checking for style's include used by make ... Gnu
Checking dependency style of gcc ... gcc3
Configure:creating./config.status
Config.status:creating Makefile
config.status:executing depfiles Commands
$ ls-l Makefile
-rw-rw-r--1 Yutao yutao 15035 Oct 10:40 Makefile
As you can see, at this point the makefile has been produced.

7. Compiling code using Linux makefile
$ makeif gcc-dpackage_name= ""-dpackage_tarname= ""-dpackage_version= ""-dpackage_string= ""-DPACKAGE_BUGREPORT= ""- Dpackage= "HelloWorld"-dversion= "1.0"
-I.-I.-G-O2-MT HELLOWORLD.O-MD-MP-MF ". Deps/helloworld. Tpo "
-c-o helloworld.o ' test-f ' helloworld.c ' | | echo './' helloworld.c;
Then Mv-f ". Deps/helloworld. Tpo "". Deps/helloworld. Po ";
Else Rm-f ". Deps/helloworld. Tpo "; Exit 1;
Figcc-g-o2-o HelloWorld HELLOWORLD.O run HelloWorld
$./helloworld Hello, Linux world!

This HelloWorld is compiled, if you follow the above steps to do, it should also be easy to compile the correct HelloWorld file. You can also try using some other make commands, such as make Clean,make Install,make Dist, to see what effects they will give you. How do you feel? I can write such a professional Linux Makefile, the boss will be very impressed with you.

Iv. in a comprehensible and understandable

For each of the above-mentioned commands, let's do some more detailed introductions.

1, AutoScan

AutoScan is used to scan the source code directory for generating Configure.scan files. AutoScan can use directory names as parameters, but if you do not use parameters, then AutoScan will assume that the current directory is being used. AutoScan will scan the source files in the directory you have specified and create a Configure.scan file.

2, Configure.scan

Configure.scan contains the basic options for system configuration, which are all macro definitions. We need to rename it to configure.in.

3, Aclocal

Aclocal is a Perl scripting program. Aclocal automatically generates ACLOCAL.M4 files based on the contents of the Configure.in file. The definition of aclocal is: "Aclocal-create aclocal.m4 by scanning configure.ac".

4, autoconf

The autoconf is used to generate configure files. Configure is a script that can set up the source program to accommodate a variety of operating system platforms, and generate the appropriate Linux Makefile based on different systems, so that your source code can be compiled on different operating system platforms.

The contents of the Configure.in file are macros that, after autoconf processing, become shell scripts that examine system features, environment variables, and the parameters that the software must have. The order of macros in the configure.in file is not specified, but you must add Ac_init macros and Ac_output macros to the front and last sides of all macros.

In Configure.ini: #号表示注释, the content behind this macro is ignored. Ac_init (FILE) This macro is used to check the path where the source code resides. Am_init_automake (Package, VERSION)

This macro is required, and it describes the name of the package we are going to generate and its version number: The package is the name of the bundle and version is the revision number. When you use the Make Dist command, it will give you a helloworld-1.0.tar.gz-like software release package with the name and version number of the corresponding package.

AC_PROG_CC This macro will check the C compiler used by the system. Ac_output (FILE) This macro is the name of the makefile that we want to output. When we use automake, we actually need to use some other macros, but we can use aclocal to help us generate them automatically. After executing the aclocal we will get the Aclocal.m4 file.

After generating the configure.in and ACLOCAL.M4 two macro files, we can use the autoconf to generate the Configure file.

5, makefile.am

Linux makefile.am is used to generate makefile.in, which you need to write manually. Some of the content is defined in makefile.am: Automake_options This is the Automake option. When executing Automake, it checks the directory for the existence of various files in the standard GNU software package, such as authors, ChangeLog, news, and so on. When we set it to foreign, Automake will use the standard of the generic package to check.

Bin_programs This is the file name that specifies the executable file that we want to produce. If you want to produce more than one executable file, separate the names with a space between them. Helloworld_sources This is the source code that is required to specify the "HelloWorld" to produce.

If it uses more than one source file, separate them with a space number. For example need HELLOWORLD.H,HELLOWORLD.C then please write helloworld_sources= helloworld.h helloworld.c. If you define more than one executable file in Bin_programs, you define a relative filename_sources for each executable file.

6, Automake

We use Automake--add-missing to generate makefile.in. The definition of option--add-missing is "add missing standard files to the package", which will allow Automake to add some of the files that are necessary for a standards-only software bundle. The makefile.in file that we produced with Automake is in accordance with the GNU Makefile Convention, and then we can produce the appropriate Makefile file just by executing the Configure Shell script.

7. Linux Makefile

In makefile, which complies with the GNU Makefiel Convention, contains some basic pre-defined actions: make the source code according to the makefile, connect, generate the target file, the executable file. Make clean clears the object file (the file with the suffix ". O") and the executable file that resulted from the last make command.

Make install installs the successful executable file into the system directory, typically the/usr/local/bin directory. Make Dist generates a release package file (that is, distribution packages). This command will package the executable and related files into a tar.gz compressed file to be used as the software package for the release. It generates a file with a name similar to "package-version.tar.gz" in the current directory. Package and version are the Am_init_automake (package, version) that we defined in configure.in. Make Distcheck generates and tests the release package to determine the correctness of the release package. This will automatically unpack the package file, execute the Configure command, and execute make to confirm that the compilation does not appear to be wrong, and that the package is ready to be released.

Helloworld-1.0.tar.gz is a ready for distribution make distclean similar to do clean, but it also removes all configure generated files, including makefile.

V. Concluding remarks

With the introduction above, you should be able to easily generate your own makefile files and corresponding project files that conform to the GNU Convention. If you want to write more complex and customary makefile, you can refer to the configure.in and makefile.am files in some open code projects, such as: Embedded database SQLite, unit test Cppunit.

References: http://os.51cto.com/art/201002/184987.htm

Linux Makefile Auto-generated

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.