LINUX C language programming foundation (Makefile)

xiaoxiao2021-03-06  14

Suppose we have a program below, the source code is as follows:

/ * main.c * /

#include "mytool1.h"

#include "mytool2.h"

INT main (int Argc, char ** argv)

{

MyTool1_print ("Hello");

MyTool2_print ("Hello");

}

/ * MyTool1.h * /

#ifndef _mytool_1_h

#define_mytool_1_h

Void mytool1_print (char * print_str);

#ENDIF

/ * MyTool1.c * /

#include "mytool1.h"

Void mytool1_print (char * print_str)

{

Printf ("this is mytool1 print% s", print_str);

}

/ * mytool2.h * /

#ifndef _mytool_2_h

#define _mytool_2_h

Void mytool2_print (char * print_str);

#ENDIF

/ * MyTool2.c * /

#include "mytool2.h"

Void mytool2_print (char * print_str)

{

Printf ("this is mytool2 print% s", print_str);

}

Of course, because this program is very short, we can compile this:

gcc -c main.c

gcc -c mytool1.c

gcc -c mytool2.c

GCC -O main main.o mytool1.o mytool2.o

In this way, we can also generate the main program, and it is not very troublesome. But if we think that if one day we have modified a file (such as myTool1.c), then we need to re-enter the command? Maybe you will say, this is easy to solve, I wrote a shell script, Let it help me do it. Yes, it is possible to play for this program. But when we think about things, we have hundreds of source programs when we have hundreds of sources. To this end, smart programmers came up with a very good Tools to do this, this is Make. As long as we do the following Make, we can solve the above problem. Before we execute make, we must write a very important file first. --Makefile. For the program above, a possible Makefile file is: # This is the Makefile file above the program:

Main: main.o mytool1.o mytool2.o

GCC -O main main.o mytool1.o mytool2.o

Main.o: main.c mytool1.h mytool2.h

gcc -c main.c

Mytool1.o: mytool1.c mytool1.h

gcc -c mytool1.c

Mytool2.o: mytool2.c mytool2.h

gcc -c mytool2.c

转载请注明原文地址:https://www.9cbs.com/read-50789.html

New Post(0)