Makefile and configure

Source: Internet
Author: User
Tags builtin

Preface

If you have installed software from the source code, you must be familiar with./configure & make install. But what happened behind the scenes in every step of the process? This article will show you how to find out. An in-depth understanding of this process will help you develop your own tricks on the basis of LFS. However, it should be noted that the makefile and make explanations in this article are quite short-sighted and superficial, but it is sufficient to understand the installation process.

Overview

The process is explained in one sentence:

According to makefile in the source package. the in file indicates that the configure script checks the current system environment and configuration options, and generates makefile files in the current directory (there are other files that do not need to be concerned about in this article ), then the make program will compile the source code as a binary file according to the makefile instructions in the current directory, and finally move these binary files (that is, install) to the specified location (still follow the instructions in the MAKEFILE file ).

Makefile is the core behind the scenes. To thoroughly understand the installation process, you must first have a full understanding of the MAKEFILE file. This article will first describe makefile and make, and then describe the configure script. In addition, the two parts provide reference materials that can be used in practice as detailed as possible.

Makefile and make

The relationship between makefile and make is summarized in one sentence:

Makefile contains all the rules and objectives, while make is a tool for interpreting makefile rules to accomplish the goals.

Make syntax

First, let's look at the command line Syntax of make:

Make [Options] [targets] [Var = value]...

[Options] is a command line option. You can use the make -- Help Command to view all of them. [Var = value] is an environment variable specified on the command line. You are familiar with both of them, I will explain it in detail later. What is [targets? The literal meaning is "goal", that is, the task to be completed by the make command. Based on experience, this [targets] can probably use "ckeck", "Install", and so on (that is, common test and installation commands ). But what exactly is it? What does the make command without any "target" mean? Why does "make
Perl utilities "is such a weird command? To answer these questions, you must first understand the "Rules" in the MAKEFILE file ".

Makefile rules

The makefile rule contains the dependency between files and the commands required to update the target of this rule.

A simple makefile rule is written as follows:

Target: prerequisites

Command

Target

The target of the rule. That is, the "target" that can be used by make ". Some targets can have no dependencies but only actions (command lines), such as "clean". Generally, only a series of commands are defined to delete intermediate files. Similarly, some goals can have no action but only dependencies. For example, "all" is generally used only as "the ultimate goal ".

Prerequisites

Rule dependency. Generally, a target depends on one or more files.

Command

The command line of the rule. A rule can have zero or multiple command lines.

OK! Now you understand what [targets] is. It turns out that they are from the target of a rule in the MAKEFILE file ). In addition, the goal of the first rule in the MAKEFILE file is called "ultimate goal", that is, the target (usually "all") When you omit the [targets] parameter ").

When you view an actual Makefile file, you will find that some rules are complex, but they all comply with the basic format of the rules. In addition, Makefile usually contains many other things except Rules, but this article only cares about the variables.

Makefile variable

Variables in Makefile are more like Macros in C, representing a text string (variable value) and can be used in any part of the rule. The definition of a variable is simple: VAR = VALUE; the reference of a variable is also simple: $ (VAR) or $ {VAR }. The expansion process of variable reference is a strict text replacement process, that is, the string of the variable value is precisely expanded in the place where the variable is referenced. For example, if the value is VAR = c, "$ (VAR)-$ (VAR) VAR. $ (VAR) "is expanded to" c-c VAR. c ".

Although the system environment variables can be directly used in Makefile, you can also "hide" the system environment variables by defining the variables with the same name in Makefile. On the other hand, we can use the-e parameter to force the environment variables in the system to overwrite the variables with the same name in Makefile when calling make. In addition, using the environment variable specified in VAR = VALUE format on the command line that calls make can also overwrite the same name variable in Makefile.

Makefile instance

Let's take a look at a simple and practical Makefile file:

CC = gcc

CPPFLAGS =

CFLAGS =-O2-pipe

LDFLAGS =-s

PREFIX =/usr

All: prog1 prog2

Prog1: prog1.o

$ (CC) $ (LDFLAGS)-o prog1 prog1.o

Prog1.o: prog1.c

$ (CC)-c $ (CFLAGS) prog1.c

Prog2: prog2.o

$ (CC) $ (cflags) $ (ldflags)-O prog2 prog2.o

Prog2.o: prog2.c

$ (CC)-C $ (cppflags) $ (cflags) prog2.c

Clean:

Rm-f *. {o, a} prog {1, 2}

Install: prog1 prog2

If (test! -D $ (prefix)/bin); then mkdir-p $ (prefix)/bin; FI

CP-F prog1 $(prefix)/bin/prog1

CP-F prog2 $(prefix)/bin/prog2

Check Test: prog1 prog2

Prog1 <sample1.ref> sample1.rz

Prog1 <sample2.ref> sample3.rz

CMP sample1. OK sample1.rz

CMP sample2. OK sample2.rz

It can be seen that make is equivalent to make all and make prog1 prog2. The commonly used make check and make install also find the ownership. We also see how various variables in makefile affect compilation. For this specific makefile, you can even omit the make command in the three-step installation and directly use make install for installation.

Similarly, to use custom compilation parameters to compile prog2, you can use the make prog2 CFLAGS = "-O3-march = athlon64" or CFLAGS = "-O3-march = athlon64" & make-e prog2 command to achieve this purpose.

Makefile conventions

The following are some common target names in Makefile and their meanings:

All

Compile the entire software package without recreating any documents. This is generally the default ultimate goal. This goal generally uses the "-g" option for compilation and connection of all source programs so that the final executable program contains debugging information. You can use the strip program to remove these debugging symbols.

Clean

Clear the files generated during the make process in the current directory. It cannot delete the configuration files of the software package or those files created during build.

Distclean

Similar to "clean", but add and delete the configuration files in the current directory and the files generated during the build process.

Info

Generate necessary Info documents.

Check or test

Complete all self-check functions. Before performing the check, ensure that all programs have been created (but can be not installed ). To perform the test, you must run the test command without installing the program.

Install

Compile the program and copy the final executable program and library files to the specified directory. This installation generally does not perform strip operations on executable programs.

Install-strip

Similar to "install", however, strip operations are performed on executable files copied to the installation directory.

Uninstall

Delete all files installed by "install.

Installcheck

Run the installation check. Before performing the installation check, make sure that all programs have been created and installed.

Installdirs

Create the installation directory and Its subdirectories. It cannot change the compiling directory of the software, but only creates the installation directory of the program.

The following are some common variable names in Makefile and their meanings:

These conventional variables are divided into three types. The first type represents the name of the executable program. For example, CC represents the executable program of the compiler. The second type represents the parameters used by the Program (multiple parameters are separated by spaces ), for example, CFLAGS indicates the parameters used by the compiler for execution (a strange way is to directly include parameters in CC). The third class indicates the installation directory, such as prefix, which has a simple meaning, only their default values are listed below.

The AR function library package program allows you to create a static library. a document. The default value is "ar ".

AS assembler. The default value is "".

Cc c Compilation Program. The default value is "cc ".

Cxx c ++ compiles programs. The default value is "g ++ ".

Cpp c/C ++ pre-processor. The default value is "$ (CC)-E ".

FC Fortran compiler. The default value is "f77 ".

PC Pascal Compiler. The default value is "pc ".

YACC Yacc grammar analyzer. The default value is "yacc ".

Command line parameters of the package program in the ARFLAGS function library. The default value is "rv ".

Command line parameters of the ASFLAGS assembler.

Command line parameters of the cflags c compiler program.

Command line parameters of the cxxflags c ++ compiler program.

Command line parameters of the cppflags c/C ++ Preprocessor.

Command line parameters of the FFLAGS Fortran compiler.

Command line parameters of the PFLAGS Pascal Compiler.

Command line parameters of the YFLAGS Yacc grammar analyzer.

The command line parameters of the LDFLAGS linker.

Prefix/usr/local

Exec_prefix $ (prefix)

Bindir $ (exec_prefix)/bin

Sbindir $ (exec_prefix)/sbin

Libexecdir $ (exec_prefix)/libexec

Datadir $ (prefix)/share

Sysconfdir $ (prefix)/etc

Sharedstatedir $ (prefix)/com

Localstatedir $ (prefix)/var

Libdir $ (exec_prefix)/lib

Infodir $ (prefix)/info

Includedir $ (prefix)/include

Oldincludedir $ (prefix)/include

Mandir $ (prefix)/man

The Directory of the source file to be compiled by srcdir. No default value exists.

Make Option

Finally, let's talk about the command line options of make (based on Make-3.81 ):

-B, -- always-make

Instead of determining whether to recreate certain target files based on the dependency of the rules.

-C dir, -- directory = DIR

Switch the working directory to DIR before running the make program.

-D

Print all debugging information during make execution. These include: make thinks that the files need to be rebuilt; those files need to compare their last modification time and comparison results; re-build the command to be executed by the target; and use the implicit rules. With this option, we can see all the information about the build dependency chain and Reconstruction of the target process, which is equivalent to "-debug = ".

-- Debug = FLAGS

Print debugging information during make execution. FLAGS is used to control the debugging information level:

A

Output debugging information of all types

B

Output basic debugging information. Including the objects that have expired and whether the objects have expired successfully.

V

In addition to level B, the file name of the parsed makefile does not need to be rebuilt.

I

In addition to level B, it also includes the description of all used implicit rules.

J

Output all sub-processes that execute commands, including the command execution PID.

M

Output make's reading, updating, and executing makefile information.

-E, -- environment-overrides

Use the definition of system environment variables to overwrite the definition of variables with the same name in Makefile.

-F FILE, -- file = FILE, -- makefile = FILE

Specify the FILE as a Makefile.

-H, -- help

Print help information.

-I, -- ignore-errors

Ignore errors during rule command execution.

-I DIR, -- include-dir = DIR

Specify the search directory that contains the Makefile file. When multiple "-I" are used to specify a directory, the search directory is performed in the specified order.

-J [N], -- jobs [= N]

Specifies the number of commands executed in parallel. If the "-j" parameter is not specified, the number of executed commands will be the maximum number allowed by the system.

-K, -- keep-going

In case of a command execution error, do not terminate the make execution, that is, execute all the commands as much as possible until a fatal error occurs.

-L [N], -- load-average [= N], -- max-load [= N]

If the system LOAD exceeds LOAD (floating point number), the new task is no longer started.

-L, -- check-symlink-times

At the same time, the timestamp of the symbolic connection and the timestamp of the target file to which it points are determined by the later timestamp of the two.

-N, -- just-print, -- dry-run, -- recon

Only the command to be executed is printed, but the command is not actually executed.

-O FILE, -- old-file = FILE, -- assume-old = FILE

The FILE is not rebuilt even if the dependency on the FILE has expired.

-P, -- print-data-base

Before executing the command, print all the data (including the values of rules and variables) of the Makefile read by make and the version information of make. If you only need to print the data information, you can use the make-qp command. Run the make-p-f/dev/null command to view the preset rules and variables before the make command is executed.

-Q, -- question

"Inquiry mode ". If no command is run and no output is displayed, a query status is returned. If the returned status is 0, no target needs to be rebuilt. If the returned status is 1, the target to be rebuilt exists. If the returned status is 2, an error occurs.

-R, -- no-builtin-rules

Cancel all embedded implicit rules, but you can use the mode rules in Makefile to define the rules. At the same time, the list of implied SUFFIXES that support the suffix-based rules will be canceled. We can also use ". SUFFIXES" in Makefile to define our own suffix rules. This option does not cancel the implicit variables embedded in make.

-R, -- no-builtin-variables

Cancel the implicit variables embedded in make. However, you can define certain variables in Makefile. Note that the "-r" option is enabled for this option. Because implicit rules are based on embedded implicit variables.

-S, -- silent, -- quiet

The executed command is not displayed.

-S, -- no-keep-going, -- stop

Cancel the "-k" option. In the recursive make process, neutron make inherits the upper-layer command line options through the MAKEFLAGS variable. We can use the "-S" option in the sub-make to cancel the "-k" option passed by the upper layer, or cancel the "-k" option in the system environment variable MAKEFLAGS.

-T, -- touch

Update the timestamp of all target files to the current system time. Prevent make from rebuilding all obsolete target files.

-V, -- version

Print version information.

-W, -- print-directory

Print the working directory before make enters a directory. This option is enabled by default when the "-C" option is used.

-- No-print-directory

Cancel the "-w" option. It can be used in recursive make calls. If you cancel the "-C" parameter, "-w" is enabled by default ".

-W file, -- what-if = FILE, -- new-file = FILE, -- assume-new = FILE

Sets the time stamp of the FILE to the current time, but does not change the actual last modification time of the FILE. This option is mainly used to achieve forced reconstruction of all objects dependent on FILE files.

Configure

The main purpose of this phase is to generate Makefile files, which is the most critical strategizing phase. Basically, all personalized adjustments to the installation process are concentrated in this step.

What content does the configure script affect in Makefile? Basically, this can be said: all the content, including the Makefile rules and Makefile variables that are most concerned about in this article. Which factors affect the final generated Makefile file? Answer: system environment and configuration options.

The impact of configuration options is obvious. However, the concept of "system environment" is very broad and involves many aspects. However, we only care about environment variables here, specifically, the environment variables used in Makefile and the environment variables with the same name as the variables in Makefile will be used in the future.

General configure syntax

Before proceeding, let's take a look at the configure script syntax. There are two types:

Configure [OPTIONS] [VAR = VALUE]...

Configure [OPTIONS] [HOST]

Regardless of the syntax, we can use configure -- help to view all available [OPTIONS], and usually we can see at the end what environment variables this script cares about. In this article, we will merge these two syntaxes and use the following simplified Syntax:

Configure [OPTIONS]

This syntax can be recognized by all configure scripts. It can also complete all the functions of the preceding two syntaxes by setting environment variables and using specific [OPTIONS.

General configure options

Although the configure scripts of each software package vary widely, they all have some common options and basically follow the same option syntax.

Script Option

-- Help

Displays help information.

-- Version

Displays version information.

-- Cache-file = FILE

Cache test results in FILE files (disabled by default ).

-- No-create

The configure script does not output result files after it is run. It is often used for testing before official compilation.

-- Quiet, -- silent

The "checking..." message output during script operation is not displayed.

DIRECTORY Options

-- Srcdir = DIR

The directory where the source code file is located. The default directory is the directory where the configure script is located or its parent directory.

-- Prefix = PREFIX

PREFIX of the top-level installation directory of system-independent files. The default value is/usr/local or/usr/local/pkgName.

-- Exec-prefix = EPREFIX

The top-level installation directory of system-related files. The default value is generally PREFIX.

-- Bindir = DIR

The directory where executable files are stored. The default value is EPREFIX/bin.

-- Sbindir = DIR

The system administrator can execute the DIR directory. The default value is EPREFIX/sbin.

-- Libexecdir = DIR

The executable directory DIR of the program. The default value is EPREFIX/libexec.

-- Datadir = DIR

The installation directory DIR of common data files. The default value is generally PREFIX/share.

-- Sysconfdir = DIR

The data directory of a read-only single machine. The default value is usually PREFIX/etc.

-- Sharedstatedir = DIR

Writable system-independent data directory DIR. The default value is generally PREFIX/com.

-- Localstatedir = DIR

Writable single-machine data directory dir. The default value is generally PREFIX/var.

-- Libdir = DIR

The installation directory DIR of the library file. The default value is EPREFIX/lib.

-- Includedir = DIR

C header file directory DIR. The default value is generally PREFIX/include.

-- Oldincludedir = DIR

Non-gcc C header file directory DIR, the default value is usually/usr/include

-- Infodir = DIR

The installation directory DIR of the Info document. The default value is generally PREFIX/info.

-- Mandir = DIR

The installation directory dir of the Man document. The default value is generally PREFIX/man.

Architecture options

Friends who are playing cross-compilation are familiar with these options, and for common cross-compilation scenarios, HOST = BUILD! = TARGET. But don't worry about friends who don't use cross-compilation. Set all three of them to the same one.

-- Host = HOST

The output result of the config. guess script by default.

-- Build = BUILD

The machine used to establish the tool chain. The default value is HOST.

-- Target = TARGET

The machine on which the binary code generated by the toolchain is run. The default value is HOST.

Feature options

-- Enable-FEATURE

Enable FEATURE

-- Disable-FEATURE

Disable the FEATURE

-- With-PACKAGE [= DIR]

Enable the additional PACKAGE. You can also specify the directory DIR where the PACKAGE is located.

-- Without-PACKAGE

Disable additional PACKAGE

Common Environment Variables

In addition to the general options above, the following environment variables affect the final Makefile file:

CPP

C Preprocessor command

CXXCPP

C ++ pre-processor commands

CPPFLAGS

C/C ++ pre-processor command line parameters

CC

C compiler commands

CFLAGS

C compiler command line parameters

CXX

C ++ compiler commands

CXXFLAGS

C ++ compiler command line parameters

LDFLAGS

Connector command line parameters

As for how to set these environment variables, you can use them as global variables or [VAR = VALUE] on the command line. the configure [OPTIONS] syntax is partially used. This is not detailed here.

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.