Simple learning of make and makefile

  
 Engaged in two years Windows
Client development, what multi-threading, multi-process development, have touched a lot, and finally feel the shortcomings, every day tired of business logic and Windows
API Later, I found out that this is not what I want. In the future of Windows
, how long this road can go, it is worth pondering; I found my favorite ——Linux. I finally got down and decided to engage in Linux. I have touched it many times, but stopped at a simple command. Fortunately, I did not do it. I installed the Ubuntu system directly. All the offices were transferred to the Ubuntu system, including me. Write this article. Look at Markdown's grammar during the day and try Markdown to write an article. This Linux road, go on.
Basic
There are so many things in Linux that you can't eat a fat man in one bite; learning experience and work experience tell me that learning this thing is a delicate, slowly coming; the foundation is solid, I don't want to fly The day when I can't fly. Starting with the most basic commands, I will learn a few times a day, instead of reading it in one breath, and learning by using it; then, I will go to the Linux C language practice. I am already proficient in the C language, just on the Linux platform. Unfamiliar, the basics are based on the C language development familiar with the Linux platform, and then there is this article —— "make and makefile learning".
Program compilation and linking
To summarize make and makefile, you need to understand the following process:
  • Precompilation: Also called preprocessing, some text replacement work, such as #define The definition of the content, in the code to replace;
  • Compile: the pre-processed code for lexical analysis, parsing, intermediate code … …; If it is under Windows, the intermediate code is .obj File; under Linux system, the intermediate code is the .o file;
  • Assembler: The compiled assembly code will get 0 and 1 machine language through the assembler;
  • Link: link various The static link library and the dynamic link library get the executable file.






    make and makefile can do
    a project, so many source files, a bunch of cpp and h files, how to compile? Compile a large project, if Rebuild may take hours or even a dozen hours; then we may have to ask.
  • How can I compile the entire project with one click like VS?
  • How to modify which file, compile the modified file instead of recompiling the entire project?

    Okay, make and makefiles can do this. The makefile defines a set of rules to specify which files need to be compiled first, which files need to be compiled, which files need to be recompiled, or even more complex functional operations, because the makefile is like a shell script, which can also be executed. Operating system
    command. The benefit of makefile is ——“automatic compilation  once written, only one make command is needed, and the whole project is fully automatically compiled, which greatly improves the efficiency of software development. Make is a command tool, a command tool to explain the instructions in the makefile. In general, most IDEs have this command, such as: Delphi make, Visual C++ nmake, Linux GNU make. It can be seen that the makefile has become a compilation method in engineering. When the make command is executed, a makefile is needed to tell the make command what to do to compile and link the program. Now, you should understand. Make is a command to parse the makefile; makefile is a file that tells the make command how to compile the entire project and generate executable files. It's that simple, let's continue.
    We have to solve the problem
    How come the make thing? Remember when you started the C language, did you write the Hello World program?
    #include <stdio.h>int main(){ printf("Hello World\ "); return 0;} When you enter the gcc HelloWorld.c command in the terminal, an a is generated. Out file, then you can magically use ./a.out to execute the file and print out Hello World. This is a thing that excites beginners. Of course, I have passed this age of excitement. Aha, the problem is coming, now a single HelloWorld.c file, if there are multiple code files, and there are reference relationships between multiple code files, at this time, how to compile and generate an executable file? Yes, the problem is coming. For example, there are some source files:
    add.hadd.csub.hsub.cmul.hmul.cdivi.hdivi.cmain.c These code files are defined as follows:
    //add.h#ifndef _ADD_H_#define _ADD_H_int add(int a, int b);#endif//add.c#include "add.h"int add(int a, int b){ return a + b;}//sub.h#ifndef _SUB_H_# Define _SUB_H_int sub(int a, int b);#endif//sub.c#include "sub.h"int sub(int a, int b{ return a - b;}//mul.h#ifndef _MUL_H_# Define _MUL_H_int mul(int a, int b);#endif//mul.c#include "mul.h"int mul(int a, int b){ return a * b;}//divi.h#ifndef _DIVI_H_ #define _DIVI_H_int divi(int a, int b);#endif//divi.c#include "divi.h"int divi(int a, int b){ if (b == 0) { return 0; } return a /b;}//main.c#include <stdio.h>#include "add.h"#include "sub.h"#include "mul.h"#include "divi.h" Int main(){ int a = 10; int b = 2; printf("%d + %d = %d\ ", a, b, add(a, b)); printf("%d - %d = %d\ ", a, b, sub(a, b)); printf("%d * %d = %d\ ", a, b, mul(a, b)); printf(" %d /%d = %d\ ", a, b, divi(a, b)); return 0;} You also saw that in main.c you want to reference these files, then how to compile now, generate a Executable file? The question is coming, how to fix it?
    The most stupid way to solve the problem
    The most stupid solution is to compile all the files to generate the corresponding target .o file. As follows:
    $gcc -c sub.c -o sub.o$ gcc -c add.c -o add.o$ gcc -c sub.c -o sub.o$ gcc -c mul.c -o Mul.o$ gcc -c divi.c -o divi.o$ gcc -c main.c -o main.o Then use the following command to link the generated single object file to generate an executable file.
    $gcc -o main add.o sub.o mul.o divi.o main.o
  • Copyright © Windows knowledge All Rights Reserved