Linux c Programming: make compilation 1, linux programming make Compilation

Source: Internet
Author: User

Linux c Programming: make compilation 1, linux programming make Compilation

There are countless source files in a project, which are classified into several directories according to different functions. makefile defines a series of rules to compile those files first and then compile those files, recompile those files. The biggest benefit of makefile is automated compilation. Once written, only one make command is required, and the entire process is automatically compiled. Greatly improve the development efficiency. Let's take a look at a simple example:
If a project contains one head file calc. h and two C files main. c, calc. c.
The content of main. c is as follows:
# Include "stdio. h"
# Include "calc. h"
Int main ()
{
Int n, k;
Int c;
N = 3;
K = 4;
C = calculate (n, k );
Printf ("the value is % d \ n", c );
}
The content of calc. c is as follows:
# Include "calc. h"

Int calculate (int n, int k)
{
Return n * k;
}
The content of calc. h is as follows:
# Ifdef CALC_H
# Define CALC_H
Int calculate (int n, int k );
# Endif
To compile the project file and generate the execution file main, compile the file as follows:
Root @ zhf-linux:/home/zhf/c_prj/make_function # gcc main. c calc. c-o main
However, if I modify main. c. You need to recompile all the source files, even if the other files do not change. Re-compile. A large software project consists of thousands of source files, which takes a long time to compile. It is unreasonable for a source file to be modified, resulting in all re-compilation. We can optimize it as follows:
Gcc-c main. c
Gcc-c calc. c
Gcc main. o calc. o-o main
If the main. c is modified after compilation, recompilation takes two steps:
Gcc-c main. c
Gcc main. o calc. o-o main
This is easier than the previous one, but there is still a problem. Both calc. c and main. c contain calc. h. If I make changes to calc. h. All files containing calc. h must be modified. In addition, you have to search for those that contain calc. h. It is still very troublesome. For example, a macro definition is added to calc. h. This variable is also useful in man. c and calc. c. Then, once calc. h modifies the macro-defined variable value. Both calc. c and main. c must be re-compiled.
# Define max_value 40

So what kind of compilation method do we need to save the most trouble:
1. if this project has not been compiled, all our C files must be compiled and linked.
2. If several C files of this project are modified, We will compile only the modified C files and connect them to the target program.
3. If the header file of this project is modified, We need to compile the C file that references the header files and link the target program.
The makefile file can achieve the above purpose. Create a Makefile under the file path of the project. The content is as follows:
Main: main. o calc. o
Gcc-o main. o calc. o
Main. o: main. c calc. h
Gcc-c main. c
Calc. o: calc. c calc. h
Gcc-c calc. c
Clean:
Rm *. o
Rm main
Run the make command
Root @ zhf-linux:/home/zhf/c_prj/make_function # make
Gcc-c main. c
Gcc-c calc. c
Gcc-o main. o calc. o

Let's take a look at the Makefile rules:
1. The target of the first rule is main. To obtain the main file, you must first obtain the main. o calc. o files. Therefore, make will further search for the rules with the two conditions as the target.
2. Target three main. o and calc. o of the second rule and the third rule. Main. o depends on main. c and calc. h. To obtain main. o, gcc-c main. Calc. o must be executed based on calc. c and calc. h. To obtain calc. o, gcc-c calc. c must be executed.
3. The last clean operation clears the temporary files generated during execution. When you run the make command, the command under "clean" is not executed and must be executed separately in the "make clean" mode. After execution, all *. o and main are deleted.
Root @ zhf-linux:/home/zhf/c_prj/make_function # ls-al
Total 40
Drwxr-xr-x 2 root 4096 Nov 10.
Drwxr-xr-x 3 root 4096 Nov 8 ..
-Rw-r -- 1 root 118 Nov 10 calc. c
-Rw-r -- 1 root 94 Nov 10 08:54 calc. h
-Rw-r -- 1 root 1056 Nov 10 calc. o
-Rwxr-xr-x 1 root 7396 Nov 10 main
-Rw-r -- 1 root 182 Nov 10 08:56 main. c
-Rw-r -- 1 root 1196 Nov 10 main. o
-Rw-r -- 1 root 142 Nov 10 Makefile
Execute make clean
Root @ zhf-linux:/home/zhf/c_prj/make_function # make clean
Rm *. o
Rm main
Root @ zhf-linux:/home/zhf/c_prj/make_function # ls-al
Total 24
Drwxr-xr-x 2 root 4096 Nov 10.
Drwxr-xr-x 3 root 4096 Nov 8 ..
-Rw-r -- 1 root 118 Nov 10 calc. c
-Rw-r -- 1 root 94 Nov 10 08:54 calc. h
-Rw-r -- 1 root 182 Nov 10 08:56 main. c
-Rw-r -- 1 root 142 Nov 10 Makefile

Next let's modify the content in calc. h, # define max_value 50
Check the compilation content. Since both calc. c and main. c contain calc. h, both calc. c and main. c are compiled.
Root @ zhf-linux:/home/zhf/c_prj/make_function # make
Gcc-c main. c
Gcc-c calc. c
Gcc-o main. o calc. o

If you only modify the content in calc. c. Modify calc. c as follows:
Int calculate (int n, int k)
{
Printf ("the value is % d", max_value );
Return n * k + n * k;
}
We can see that only calc. c is compiled. Main. c is not compiled
Root @ zhf-linux:/home/zhf/c_prj/make_function # make
Gcc-c calc. c
Gcc-o main. o calc. o

If no file is modified, the system prompts "main is up to date ".
Root @ zhf-linux:/home/zhf/c_prj/make_function # make
Make: 'main' is up to date.

So how does make work:
1. make searches for files named makefile or Makefile in the current directory.
2. If yes, it will find the first target file in the file. In the above example, it will find the file main.
3. If the main file does not exist, or the modification time of the. o file after the main file is later than that of the main file, the system will execute the command defined later to generate the main file.
4. If the. o file on which main is dependent exists, make will find the dependency of the. o file in the current file. If yes, generate the. o file based on the Rule.
5. When the C file and the H file exist, make will generate the. o file. Then use the. o file to generate the make final task, that is, the execution file main.
That is to say, main will find the file dependency layer by layer until a target file is compiled. If the dependent files cannot be found during the search process, the system will directly exit or report an error.

Continue to see the previous makefile file. In the first rule .. O files are repeated twice. If you need to add a new. o file to the project, add it in two places. If makefile is complex. In this case, you may forget a place to join. The compilation fails. Therefore, variables can be used in makefile to facilitate maintenance of makefile. It can be understood as a macro definition in C language.
Main: main. o calc. o
Gcc-o main. o calc. o
The file is modified as follows:
Objects = main. o calc. o
Main: $ (objects)
Gcc-o main $ (objects)

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.