Getting started with cainiao: Linux Makefile Overview

Source: Internet
Author: User
Article Title: getting started with cainiao: Linux Makefile overview. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
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.
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.