The magical DD of makefile under Linux

Source: Internet
Author: User

One: Cause

(0) First we assume that the reader is already familiar with the common operation of Vim under Linux ( e.g., u undo or : Undo Ctrl-r Redo or: redo);

(1) Under the Linux make command, the application and the makefile file, when we need to a C + + or Java and other projects in a project to compile all the source files, every compilation, do not want to repeat the operation of the previous command, Do not want to make the unchanged source files again unnecessary compilation operations-the best choice makefile (PS: Although at this point, you can choose to write a shell, but the shell needs too much code, a lot of detection and judgment to write their own)

Second: detailed

(1) First, a brief introduction to Linux gcc common commands

1) Simple compilation (one step)
hello.c
#include <stdio.h>
int main (void)
{
printf ("Hello world!\n");
return 0;
}

For this program, the one Step compiler command is:
GCC Hello.c-o Hello

2) In essence, the above compilation process is divided into four stages, namely preprocessing (also known as precompilation, preprocessing), compiling (compilation), assembly (Assembly) and connection (linking) (as follows)

2.1 Preprocessing

GCC-E hello.c-o hello.i or GCC-E hello.c

You can output the code in the Hello.I file that contains the hello.c after preprocessing. Open the Hello.I file and take a look, and you'll see. The subsequent instruction is to output the preprocessed code directly in the command-line window.
The-e option of GCC allows the compiler to stop after preprocessing and output the preprocessing results. In this case, the preprocessing result is to insert the contents of the stdio.h file into the hello.c.

2.2 Compiling to Assembly code (compilation)

After preprocessing, you can compile the generated hello.i file directly and generate the assembly code:

Gcc-s Hello.i-o Hello.s

The-s option of GCC, which indicates that the assembly code is stopped and the-O output assembly code file is generated during program compilation.

2.3 Assembly (Assembly)

For the assembly code file generated in the previous section, the Hello.s,gas assembler is responsible for compiling it as a target file, as follows:

Gcc-c Hello.s-o hello.o

2.4 Connection (linking)

The GCC connector is provided by gas and is responsible for connecting the program's target file with all the additional target files needed to eventually generate the executable file. Additional target files include a static connection library and a dynamic connection library.
For the hello.o generated in the previous section, connect it to the C standard input and output library, and the resulting program Hello
GCC Hello.o-o Hello
In the command line window, execute the./hello, let it say HelloWorld!

3) Compilation of multiple program Files

Typically, the entire program is composed of multiple source files, which in turn form a number of compilation units, which can be well managed using GCC. Suppose you have a program consisting of two source files of hello1.c and hello2.c, in order to compile them and eventually generate executable Hello, you can use this command:
GCC hello1.c hello2.c-o Hello
If more than one file is processed at the same time, GCC will still follow the process of preprocessing, compiling, and linking. If you delve into it, the above command is roughly equivalent to executing the following three commands in turn:
Gcc-c Hello1.c-o HELLO1.O
Gcc-c Hello2.c-o HELLO2.O
GCC hello1.o hello2.o-o Hello


(2) Writing of Makefile

1) Suppose I have A.C,B.C, main.c three source files, and eventually compile them into an executable file called Main

If I want to erase my compiled files, I can do the RM command, and of course we can also erase the features written into the makefile inside.
Makefile provides a pseudo-target feature that can provide additional functionality for your makefile.

The following is the code in the makefile file that provides the clean feature:

MAIN:A.O B.O MAIN.O
@gcc A.O b.o Main.o-o Main
@echo OK
@gcc-C MAIN.C


B.o:b.c
@gcc-C B.C
A.o:a.c
@gcc-C A.C


# with ". Phony {target name} ' defines a pseudo-target
# Execute the pseudo-target with "make {target name}"
. Phony:clean

Clean:
@rm-F Main *.O
@echo Clean

2) explanation

Makefile, #代表注释, there must be at least one blank line after each command, and the specific command is indicated by the TAB key. Phony:clean used to define pseudo-targets

Do clean to enter the clean function

In addition, Makefile provides the system's default automation variables
$^: represents all dependent files
[email protected]: represents the target
$<: Represents the first dependent file
So we can use
GCC $^-o [email protected]
Instead of just makefile.
GCC a.o b.o main.o-o Main

The magical DD of makefile under Linux

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.