Make,makefile and program compilation link process

Source: Internet
Author: User

First, the Linux program to run the process
1, create three files in one directory: main.c hello.c hello.h write them as follows:

2, if you want this program to execute, you must perform the compilation link on the three files above, as shown in the following figure:


Through the above process. We can summarize the process and command of the GCC compiler to preprocess, compile, assemble, and link the target files into executable files:
(1) Preprocessing (macro substitution, deletion of annotations and extra whitespace characters, conditional compilation, file inclusion):

Where option-e is viewed, this option is to allow GCC to stop compiling after preprocessing ends
Process.
Option-O refers to the destination file, the. i file is the C original program that will complete the preprocessing process.
(2) compile (GCC check code normative, syntax error, build assembly):

Where option-S is viewed, the option is compiled and not compiled, and the. s file is generated.
(3) compilation (Generating machine-identifiable code, which will compile the generated. s file into. o binary target code):

(4) Link (generate executable or library file):
After a successful compilation, it goes into the link phase, which involves an important concept: the Library view the applet above will find that the implementation of the printf function is not defined, and the "stdio.h" included in the precompiled phase is only its declaration, not the implementation of the definition function. So where does printf really come true.
In fact, the system to the implementation of these functions are the name of the library file libc.so.6, in the absence of special designation, GCC will go to the system default path "/user/lib" under the search, and link to the Lib.so.6 library function, so that the printf function can be implemented, which is the role of the link.
function library is divided into two kinds: static library and dynamic library. A static library refers to adding the library file's code to the executable file when compiling the link, so the resulting file is larger, but the library file is no longer used during execution. The suffix name generally is. A; dynamic libraries instead of adding library files to the executable in the process of compiling the links, the runtime linked files are loaded into the library at the time of the program's execution, which saves the overhead of the system, usually with a suffix name of. So. The libc.so.6 mentioned above is the dynamic library, so you can get GCC to use dynamic libraries by default at compile time. When the link process is complete, GCC can generate the executable file main.

(5) Implementation:
"./executable file name" You can execute this program, the output of the following figure:

Two, make and makefile
1, understanding of make and Makefile
As mentioned above, to execute a program, the first step is to compile the intermediate file (. o file) (at compile time the compiler checks only program syntax, functions, variables are declared, if the function is not declared, the compiler gives a warning, but still generates an. o file.) When linked, the linker looks for the implementation of the function in all the. o files, if not found, that would be a link error code, similar to this: Link2001 error, and then through the chain to deliver executable files, so that in the small program seems to be nothing, but in large projects this is very difficult to upset the process, you have to remember all the intermediate documents of the text Name, you can easily use them in the link, but if there is a small bug in the program, then the above process to start over. This is so inefficient that there are makefile and make.
A number of source files in a project, which by type, function, modules are placed in several directories, makefile defined a series of rules to specify which files to compile first, which to compile, which need to recompile, and even more complex functional operations. Makefile is like a shell script, which can also execute operating system commands. The benefit of makefile is "Automated compilation"Once written, only need a make command, the whole project completely automatic compilation, greatly improve the efficiency of software development."
the difference between make and makefile:
Make: is a command tool.
Makefile: is a file that holds the compilation method.
2, writing the makefile format and rules
When the make command executes, a makefile file is needed to tell make how to compile and link the program. Here is the writing rule for makefile:
(1) If the project has not been compiled, then all of our C files will be compiled and linked.
(2) If some C files of this project are modified, we only compile the modified C file and link the target program.
(3) If the header file of this project has been changed, we need to compile the C file referencing the header files and link the target program.


Attention:
After defining the dependencies, the subsequent line defines how to generate the operating system commands for the target file, and must begin with a TAB key. Remember that make does not matter how the command works, but he executes the commands that are defined.
Clean is not a file, it is an action noun, and make clear uses it to erase all target files for recompilation.


How the 3,make works.
(1) Make will find 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 above example, he will find "Hello" this file, and the file as the final target file.
(3) If the hello file does not exist, or if the subsequent. o file on which Hello is dependent is newer than the hello file, then he will perform the command defined later in the afterlife.
into hello this file, which is recompile.
(4) If the. o file that the Hello relies on also exists, make will find the dependency on the current file for the target. o file, and if so, generate an. o file based on that rule. (which
Sort of like a stack process)
(5) Of course, your. C and. h files exist, so make generates an. o file,
Then use the. o file to declare the final task of make, which is the execution file hello.
Make will look up the dependencies of the file one layer at a time until it finally compiles the first target file, and if there is an error in the process (the dependent file cannot be found, etc.), make makes a direct exit and complains. For a defined command error, or if the compilation is unsuccessful, make simply ignore it. Make just the file dependency, that is, if after I find the dependency, the file after the colon is still out, then sorry, make does not work.
Well, that's all, and the rest of the stuff about make and makefile will continue to be perfected, and there are a lot of inadequacies in the hope that little friends can help.

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.