Go Linux in Configure/makefile

Source: Internet
Author: User
Tags automake

This article teaches you how to use autoconf, automake, etc. to make a software published in Source code form (. tar.gz) and to use custom parameters when executing configure.


I. Overview and BASIC knowledge

Under Linux you get a package published in source code (typically. tar.gz or. tar.bz2 format), and we can compile the installation with./confiugure, make, made install, which is running. Configure can also be used to add different parameters according to their own needs (available./configure--help to view the parameter table).

Let's talk about execution. What will be generated after/configure? The system will generate config.h and multiple makefile based on the user's actual situation. Where makefile is the template used to run make, and Config.h records the user's custom parameters in the form of a macro (Marco), the compiler can precompile the source code (pre-compile) based on Config.h to generate a personalized execution file.


Second, our "software"

Now we can design a own "software", in order to be more practical, will use a number of source programs, first set up a directory TT, used to put our things, and then build a src directory under the TT, in general, the source code is placed in the SRC (as if it has become an unwritten rule: P). The overall structure is as follows:
<tt>
|-configure.in
|-makefile.am
|-acconfig.h
|-<src>
|-tt.c
|-qq.c
|-qq.h
|-makefile.am

Description
1. Configure.in This is the most important document and the entire installation process is dominated by it.
2. makefile.am Automake will generate makefile.in based on it, and then by./configure Turn makefile.in into the final makefile, and generally there should be a makefile.am in the top-level directory and in each subdirectory
3. Acconfig.h Autoheader will generate config.h.in based on it, and then by./configure turn config.h.in into final config.h
4. tt.c qq.c Qq.h This is our source program.

※ Source code content:

Tt.c

#include <stdio.h>#include<qq.h>#ifdef have_config_h#include<config.h>#endifintMainvoid){   intA = at; printf ("Hello, I am Teacher (%d), pls tell me your names!/n", a); #ifdef POPO printf ("My name is popo!/n"); #endifQQ (); return 0;}

Qq.c

#include <stdio.h><qq.h><config.h>#endifvoid qq (void  ) {   printf ("My name is qq/n");   #ifdef POPO   printf ("qq:hey POPO, long time no see./n");    #endif }

Qq.h

#ifndef __qq__ #define __qq__void QQ (void); #endif

※ Operation Process:

1. First the teacher to call the roll.
2. If the Popo comes, he will quote his name.
3. Then turn to QQ report, if Popo have come, QQ will say hello to Popo.

Obviously, popo whether or not to attend, depends entirely on Popo whether the macro has been defined, we can achieve different effects if we decide to define it before compiling.

If config.h exists, compile-time makefile will pass the macro have_config_h to the compiler, so if there is no have_config_h defined, our program should not put config.h include in.

Third, the production process

Follow the steps in the following order of execution:

The first step is to write configure.in

There are two ways to generate configure.in, one is to write from scratch, and the other is to use AutoScan, which will generate Configure.scan after AutoScan, which contains some template content, as long as the name is changed to. In.

There are two types of commands used in configure.in, one starting with AC, indicating that it is provided by autoconf, and the other is the beginning of AM, which is provided by Automake.

In configure.in we can complete a lot of detection actions, such as checking the compilation of the required programs, headers, libraries and so on, in short, the function is very powerful, but we only detect the compiler and header files, detailed usage see GNU Manuals Online

A comment line (the green part of the code) that is the "DNL"-led behavior.

Configure.in

DNL initializes the autoconf, and the parameter is the file where the entry function resides Ac_init (src/tt.c) DNL initializes the automake with the software name and version number Am_init_automake (TT,0.1.0Dnl tells Automake that the configuration file we used, typically Config.ham_config_header (config.h) DNL, is the part that implements the custom parameter, see the following description ac_arg_enable (Popo, [ --enable-popo Popo ispresent],,enable_popo=No)ifTest"$enable _popo"=Yes, then echo."PoPo is here!"ac_define (POPO)ElseEcho"PoPo isn ' t here!"fidnl detection compiler AC_PROG_CCDNL detection of standard C header file Ac_header_stdcdnl output file, in general, top-level directories and subdirectories should have Makefile output ac_output (Makefile SRC/makefile)

There are two types of custom parameters for./configure, one is switch (--enable-xxx or--disable-xxx), the other is open, which is followed by a string of character (--with-xxx=yyyy) parameters.
The above code is the switch type, the first parameter is the parameter name, the second is the description ("./configure--help" After the display of the content), the last parameter is the default value. In general, the default value and user hint should be mutually exclusive, that is, the default value is no, you should prompt the user to modify with enable, and vice versa.
As can be seen from the above code, if $enable_popo is yes, use Ac_define to define Popo macro, otherwise it will not be defined, the macro we use here must be declared in Acconfig.h.

The second step of running aclocal running aclocal in the TT directory will generate ACLOCAL.M4.

The third step is to write acconfig.h

Macros (macro) used in configure.in should be declared in this file and generally declared with #undef.

Acconfig.h

#undef POPO


Fourth Step run Autoheader

When you run Autoheader, config.h.in is generated based on the acconfig.h of Configure.in, acconfig.h, and system presets.

Fifth step to write makefile.am

In general, there should be a makefile.am in the top-level directory and in each subdirectory.


Makefile

Automake_options = Foreign
Subdirs = src

The first line is to tell Automake not to detect the existence of authors, Readme and other files in the directory.
The second line is to tell Automake to handle the SRC subdirectory.


Src/makefile

Automake_options = Foreign
Bin_programs = TT
tt_sources = tt.c qq.c qq.h

The first line functions as before.
The second line is the name of the target execution file.
The third line is the name of all the source and header files required to generate the TT file.

Sixth step Run Automake

You can then execute the Automake and enter it at the command line
Automake-a and
Automake-a Src/makefile
Use "automake-a" or "Automake--add-missing", will automatically add install.sh, Mkinstalldirs and other files, otherwise it will be wrong, remember!

Seventh Step Run autoconf

Finally, the autoconf can be executed and the final configure! will be generated when completed.


Iv. Compiling & Testing

Compile with default values:
[Email protected] tt]#./configure
Checking for ...
PoPo isn ' t here!
Checking for ...

[[email protected] tt]# make
......

[Email protected] tt]# SRC/TT
Hello, I am Teacher (23°c), pls tell me your names!
My name is QQ

By default, we do not define macro PoPo, so./configure output "PoPo is not here!", run time only QQ to report.

And look at this:
[Email protected] tt]#/configure--help
......
--enable and--with Options recognized:
--enable-popo Popo is present

[Email protected] tt]#/configure--enable-popo
Checking for ...
PoPo is here!
Checking for ...

[[email protected] tt]# make
......

[Email protected] tt]# SRC/TT
Hello, I am Teacher (23°c), pls tell me your names!
My name is popo!
My name is QQ
Qq:hey PoPo, long time no see.

You can see the./configure output "PoPo is here!", the execution results are completely different!

In addition, we can install with make install, the preset is installed to/usr/local/bin, of course, these can be modified.


V. Generate a Release package tarball

Well, so far, our small software has been tested, can be released, under the TT has a lot of files, some of our own writing, but also some of the temporary files generated at compile time, in the end what needs to be packaged in the release package? Of course you can pick one file yourself, but the makefile generated with Automake provides several very handy features for us.

We can use make dist or makes distcheck to generate the appropriate tarball, which will also help us test whether the release package works properly, so it is recommended to use make Distcheck.

Did you see that? Release package tt-0.1.0.tar.gz has been put under the TT, there is no notice, here with the software name and version number is the Am_init_automake of the configure.in in the two parameters! Now you can try to unzip it and install it.

Go Linux in Configure/makefile

Related Article

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.