Linux C Programming: Make Compile a

Source: Internet
Author: User

A project in a number of source files, according to different functions categorized in several directories, makefile defined a series of rules to develop those files need to be compiled first, those files after compilation, those files recompile. The biggest benefit of makefile is automated compilation. Once written, only one make command is required and the entire process is compiled automatically. Greatly improve the efficiency of development. Let's look at a simple example:
If a project has 1 header files calc.h and 2 C files main.c,calc.c
The contents of MAIN.C are 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 contents of CALC.C are as follows:
#include "calc.h"

int calculate (int n,int k)
{
return n*k;
}
The contents of calc.h are as follows:
#ifdef CALC_H
#define Calc_h
int calculate (int n,int k);
#endif
In order to complete the compilation of the project file case, and generate the execution file main, compile the file as follows
[Email protected]:/home/zhf/zhf/c_prj/make_function# gcc main.c calc.c-o Main
But if I make a change to MAIN.C. You need to recompile all the source files, even if there are no changes to the other files. and then recompile. A large software project consists of thousands of source files, which take a long time to compile. A source file modification causes all recompilation to be unreasonable. We can optimize this by:
Gcc-c MAIN.C
Gcc-c CALC.C
GCC MAIN.O calc.o-o Main
If the MAIN.C has been modified after compilation, it takes two steps to recompile:
Gcc-c MAIN.C
GCC MAIN.O calc.o-o Main
This is more convenient than before, but there are still problems, in CALC.C and Main.c contain calc.h. If I make a change to calc.h. All files that contain calc.h have to be changed. And they have to go around looking for the ones that contain calc.h. Still a lot of trouble. For example, a macro definition was added to the calc.h. And this variable is useful in both MAN.C and CALC.C. So once calc.h modifies the value of the macro definition variable. Both CALC.C and MAIN.C must be recompiled.
#define MAX_VALUE 40

So what kind of compiling method do we need to be able to save the most trouble:
1 if this project has not been compiled, then all of our C files are compiled and linked
2 If a few C files of this project are modified, then we only compile the modified C file and connect the target program
3 If the header file for this project has been modified, then we need to compile the C file referencing the header files and link the target program.
To achieve the above purposes is the makefile file. Create a new makefile file under the project's file path. The contents are as follows:
MAIN:MAIN.O CALC.O
Gcc-o Main 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
Execute make command
[Email protected]:/home/zhf/zhf/c_prj/make_function# make
Gcc-c MAIN.C
Gcc-c CALC.C
Gcc-o Main MAIN.O CALC.O

Look at the rules of Makefile:
1 The goal of the first rule is main. And in order to get the main, must first get MAIN.O CALC.O these 2 files. So make will further look for the rules that target these 2 conditions.
2 The objectives of the second rule and the third set of rules are MAIN.O and CALC.O. MAIN.O relies on main.c and calc.h. Gcc-c Main is executed in order to get MAIN.O. CALC.O relies on CALC.C and calc.h. In order to get CALC.O must perform gcc-c calc.c
3 The final clean operation clears the temporary files that are generated during the execution. When executed with the Make command, the command under clean does not execute and is executed separately as made clean. After execution, all *.o and main are deleted.
[Email protected]:/home/zhf/zhf/c_prj/make_function# ls-al
Total 40
Drwxr-xr-x 2 root root 4096 Nov 10 09:15.
Drwxr-xr-x 3 root root 4096 Nov 8 10:35.
-rw-r--r--1 root root 118 Nov 08:55 calc.c
-rw-r--r--1 root root 94 Nov 08:54 calc.h
-rw-r--r--1 root root 1056 Nov 09:15 CALC.O
-rwxr-xr-x 1 root root 7396 Nov 09:15 main
-rw-r--r--1 root root 182 Nov 08:56 main.c
-rw-r--r--1 root root 1196 Nov 09:15 main.o
-rw-r--r--1 root root 142 Nov 09:15 Makefile
Perform make clean
[Email protected]:/home/zhf/zhf/c_prj/make_function# make clean
RM *.O
RM Main
[Email protected]:/home/zhf/zhf/c_prj/make_function# ls-al
Total 24
Drwxr-xr-x 2 root root 4096 Nov 10 09:29.
Drwxr-xr-x 3 root root 4096 Nov 8 10:35.
-rw-r--r--1 root root 118 Nov 08:55 calc.c
-rw-r--r--1 root root 94 Nov 08:54 calc.h
-rw-r--r--1 root root 182 Nov 08:56 main.c
-rw-r--r--1 root root 142 Nov 09:15 Makefile

Let's modify the content in Calc.h, #define MAX_VALUE 50
Look at the compiled content. Since both CALC.C and main.c contain calc.h, CALC.C and MAIN.C are compiled
[Email protected]:/home/zhf/zhf/c_prj/make_function# make
Gcc-c MAIN.C
Gcc-c CALC.C
Gcc-o Main MAIN.O CALC.O

If you only modify the contents of the CALC.C. CALC.C modified as follows
int calculate (int n,int k)
{
printf ("The value is%d", max_value);
return n*k+n*k;
}
You can see that only CALC.C has been compiled. MAIN.C not compiled
[Email protected]:/home/zhf/zhf/c_prj/make_function# make
Gcc-c CALC.C
Gcc-o Main MAIN.O CALC.O

When there is no file modification: The main is
[Email protected]:/home/zhf/zhf/c_prj/make_function# make
Make: ' main ' was up to date.

So how does make work:
1 make will look for a file named Makefile or makefile in the current directory
2 if found, it will find the first target file in the file, in the example above, it will find the main file
3 If main does not exist, or if the subsequent. o file that main depends on is later modified than main, then the command defined later will be executed to generate the main file
4 if the. o file that main depends on exists, then make will look for the dependency of the. o file in the current file and, if found, generate the. o file based on that rule.
5 when C files and H files are present, make generates an. o file. And then use the. o file to generate the finalization of make, which is the Execute file main
In other words, main will look for a layer of dependencies on the file until a target file is compiled. If the files that are dependent on the lookup process are not found then they will exit or error.

Keep looking at the previous makefile file. At the time of the first rule, the. o file was repeated two times. If the project needs to add a new. o file, then it needs to be added in 2 places. If makefile is complicated. Then it is possible to forget a place to join. Causes the compilation to fail. So for makefile easy maintenance, variables can be used in makefile. Can be understood as a macro definition in the C language
MAIN:MAIN.O CALC.O
Gcc-o Main MAIN.O CALC.O
The file is modified as follows:
OBJECTS=MAIN.O CALC.O
Main:$ (objects)
Gcc-o main $ (objects)

Linux C Programming: Make Compile a

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.