Write makefile with me
Chen Hao
Overview
--
What is makefile? Maybe many winodws programmers do not know this, because the windows ide has done this job for you, But I think makefile should be understood as a good and professional programmer. It seems that there are so many HTML editors, but if you want to be a professional, you still need to understand the meaning of HTML tags. Especially for software compilation in UNIX, you can't help writing makefile by yourself. If you want to write makefile, it shows whether a person can complete large-scale engineering.
Because makefile is related to the compilation rules of the entire project. The source files in a project are not counted. They are stored in several directories by type, function, and module. makefile defines a series of rules to specify which files need to be compiled first, which files need post-compilation, which need to be re-compiled, or even perform more complex functional operations, because makefile is like a shell script and can also execute operating system commands.
The benefit of makefile is "automatic compilation". Once written, only one make command is required, and the entire project is fully automatically compiled, which greatly improves the efficiency of software development. Make is a command tool that explains commands in makefile. Generally, most ides use this command, such as make in Delphi and nmake in Visual C ++, GNU make in Linux. It can be seen that makefile is a compilation method in Engineering.
There are few articles about How to Write makefile. This is why I want to write this article. Of course, the make statements of different manufacturers are different and have different syntaxes, but they are all written in "file dependency". Here, I only talk about GNU make, my environment is RedHat Linux 8.0, and the make version is 3.80. This make is the most widely used and used. It also complies with the IEEE 1003.2-1992 standard (posix.2 ).
In this document, we will use the C/C ++ source code as our basis, so it will inevitably involve some knowledge about C/C ++ compilation and relevant content, you can also view the relevant compiler documentation. The default compiler here is GCC and CC in UNIX.
Compilation and link of Programs
----------
Here, I would like to talk about some of the program compilation Specifications and methods. Generally, whether it is C, C ++, or pas, you must first compile the source file into an intermediate code file, in Windows, that is. OBJ file, which is in UNIX format. o file, that is, object file. This action is called compile ). Then merge a large number of object files into the execution file. This action is called Link ).
During compilation, the compiler requires correct syntax and correct declaration of functions and variables. For the latter, you usually need to tell the compiler where the header file is located (the header file should only be declared, and the definition should be placed in the C/C ++ file), as long as all the syntax is correct, the compiler can compile the intermediate target file. In general, each source file should correspond to an intermediate target file (o file or OBJ file ).
The links are mainly linked functions and global variables. Therefore, we can use these intermediate target files (O files or OBJ files) to link our applications. The linker does not care about the source file where the function is located, but only about the intermediate target file of the function. In most cases, due to too many source files, too many intermediate target files are generated during compilation, during the link, you need to clearly specify the intermediate target file name, which is inconvenient for compilation. Therefore, we need to pack the intermediate target file, in Windows, this package is called "library file", that is. lib file. In UNIX, it is an archive file, that is. file.
To sum up, the source file will first generate the intermediate target file, and then the intermediate target file will generate the execution file. During compilation, the compiler only checks program syntax, and whether functions and variables are declared. If the function is not declared, the compiler will give a warning, but the object file can be generated. When linking a program, the linker will find the function implementation in all object files. If it cannot be found, it will report the Link error code (linker error). In VC, this error is generally caused by a link 2001 error, which means that the linker cannot find the function implementation. You need to specify the object file of the function.
Well, let's get down to the truth. GNU make has a lot of content, so we can start with it.
Makefile Introduction
-------
When executing the make command, you need a MAKEFILE file to tell the make command how to compile and link the program.
First, we use an example to describe the writing rules of makefile. In order to give everyone a sense. This example is from the GNU make user manual. In this example, our project contains 8 C files and 3 header files, we need to write a makefile to tell the make command how to compile and link these files. Our rules are:
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 link them to the target program.
3) if the header file of this project has been changed, we need to compile the C files that reference these header files and link them to the target program.
As long as our makefile is well written, we can use only one make command to complete all this, the make command automatically and intelligently determines which files need to be re-compiled based on the current file modification, so as to compile the required files and link the target program.
I. makefile rules
Before talking about this makefile, let's take a rough look at the makefile rules.
Target...: prerequisites...
Command
...
...
Target is a target file, which can be an object file or an execution file. It can also be a label. For the label feature, it will be described in the subsequent "pseudo-target" chapter.
Prerequisites is the file or target required to generate the target.
Command is the command to be executed by make. (Any shell command)
This is the dependency of a file. That is to say, one or more target files depend on the files in prerequisites, and their generation rules are defined in command. To put it bluntly, if more than one prerequisites file is newer than the target file, the command defined by command will be executed. This is the makefile rule. That is, the core content in makefile.
In the end, makefile is like this, as if my document should be over. Haha. This is the main line and core of makefile, but it is not enough to write a makefile. I will give you some experience in the future. There are plenty of contents. :)
Ii. Example
As mentioned above, if a project has three header files and eight C files, we want to complete the three rules described above, our makefile should look like the following.
Edit: Main. o kbd. O command. O display. O/
Insert. O search. O files. O utils. o
CC-O edit main. o kbd. O command. O display. O/
Insert. O search. O files. O utils. o
Main. O: Main. c defs. h
CC-C main. c
KBD. O: KBD. c defs. H command. h
CC-c kbd. c
Command. O: Command. c defs. H command. h
CC-C command. c
Display. O: Display. c defs. h buffer. h
CC-C display. c
Insert. O: insert. c defs. h buffer. h
CC-C insert. c
Search. O: search. c defs. h buffer. h
CC-C search. c
Files. O: files. c defs. h buffer. H command. h
CC-C files. c
Utils. O: utils. c defs. h
CC-C utils. c
Clean:
Rm edit main. o kbd. O command. O display. O/
Insert. O search. O files. O utils. o
A backslash (/) indicates a line break. This makes it easier to read makefile. We can save this content in a file named "makefile" or "makefile", and then directly enter the command "make" in this directory to generate the execution file edit. If you want to delete the execution file and all the intermediate target files, simply execute "make clean.
In this makefile, the target file contains the execution file edit and intermediate target file (*. o), the dependent files (Prerequisites) are the ones after the colon. c file and. h file. Each. o file has a set of dependent files, and these. O files are dependent files of the execution file edit. The dependency essentially describes the files generated by the target file, in other words, the files updated by the target file.
After the dependency is defined, the subsequent line defines how to generate the operating system commands for the target file, which must start with a tab key. Remember, make does not care about how the command works. It only executes the defined command. Make will compare the modification date of the targets file and the prerequisites file. If the date of the prerequisites file is newer than the date of the targets file, or the target does not exist, then, make will execute subsequent defined commands.
It should be noted that clean is not a file, it is just an action name, a bit like lable in C language, and there is nothing after the colon, so, make will not automatically find the dependency of the file, and will not automatically execute the subsequent commands. To execute the subsequent command, you must clearly name the lable after the make command. This method is very useful. We can define unnecessary compilation or compilation-independent commands in a makefile, such as program packaging, program backup, and so on.
Next Page->
(All Rights Reserved. Please indicate the author and source when reprinting)