makefile study notes - makefile overview

20180411 makefile study notes - makefile overview



The makefile is mainly written when the software is compiled under Unix, and is generally not used under Window (what the makefile in Unix does is equivalent to what the IDE in Window does).
Whether or not to write makefiles shows from one side whether a person has the ability to complete large-scale projects.










The main thing that makefile does: makefile is related to the compilation rules of the entire project. There are countless source files of a project, which are placed in several directories according to type, function, and module. The makefile defines a series of rules to specify which files need to be compiled first, which files need to be compiled later, and which files need to be compiled. Recompile, or even perform more complex functional operations, because the makefile is like a shell script, which can also execute the commands of the operating system.




The advantage of makefile is --- "automatic compilation", once it is written, only one make command is needed, and the whole project is completely automatically compiled, which greatly improves the efficiency of software development. make is a command tool that interprets the makefile Command tool for instructions. Generally speaking, most IDEs have this command, such as: Delphid make, nmake of Visual C++, and make of GNU under Linux. It can be seen that makefile has become a compilation method in engineering.




The make of different manufacturers is different and has different syntax. Only GNU make is described here. The adaptation environment is RedHat Linux 8.0, and the version of make is 3.80. This make is the most widely used. This make is also the most compliant with the IEEE1003.2-1992 standard (POSIX.2).


In this article, the C/C++ source code will be used as the basis, and the default compilers used are GCC and CC under UNIX.




The general specification of program compilation is: whether it is C, C++ or pas, the source file should first be compiled into an intermediate code file, which is .obj file under Windows, and .o file under UNIX, that is, Object File. This action is called compiling, and then a large number of Object Files are synthesized into executable files. This action is called linking.






When compiling, what the compiler needs is correct syntax and correct declaration of functions and variables. For the latter, usually you need to tell the compiler where the header file is (the header file should just be the declaration, and the definition should be in the C/C++ file), and as long as all the syntax is correct, the compiler can compile the intermediate target document. In general, each source file should correspond to an intermediate object file (O file or OBJ file).


When linking, it is mainly linking functions and global variables, so it is perfect to use these intermediate object files (O files or OBJ files) to link our application. The linker does not care about the source file where the function is located, but only the intermediate object file of the function (Object File). The name of the intermediate object file, which is very inconvenient for compilation, so it is perfect to package the intermediate object file. Under Windows, this package is called "Library File" (Library File), which is the .lib file. Under UNIX, it is an Archive file. That is, the .a file.






Note: When linking the program, the linker will look for the implementation of the function in all Object Files. If it can't find it, it will report the linker error code (Linker Error). Under VC, this kind of error is generally: Link 2001 error , which means that the linker could not find the implementation of the function, you need to specify the Object File of the function.












Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324608790&siteId=291194637