Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/
1. Entry
1) makefile: Helps compile multiple files.
2) basic compiling example: Take the following four files as an example.
//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_Hvoid 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_Hvoid 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);}
The makefile for compilation and construction is
main:main.o mytool1.o mytool2.ogcc -o main main.o mytool1.o mytool2.omain.o:main.c mytool1.h mytool2.hgcc -c main.cmytool1.o:mytool1.c mytool1.hgcc -c mytool1.cmytool2.o:mytool2.c mytool2.hgcc -c mytool2.c
The essence of makefile is to describe the dependencies and compilation methods between the source code involved in the construction. The general format is as follows:
Target: Components
Tab rule
The first line indicates the dependency. The second line is the rule.
Main: Main. O mytool1.o mytool2.o is the so-called dependency relationship. In fact, it is a method to solve the problem. We recommend that you draw a dependency graph on the paper, for example:
Main
--- Main. O -- (main. C, mytool1.h, mytool2.h)
|
Mytool2.o -- (mytool2.c, mytool2.h)
|
Mytool1.o -- (mytool1.c, mytool1.h)
Gcc-O main. O mytool1.o mytool2.o is the so-called rule.
3) simplified
$ @ -- Target file, $ ^ -- all dependent files, $ <-- the first dependency file. The substitution rules can be abbreviated as follows:
main:main.o mytool1.o mytool2.ogcc -o $@ $^main.o:main.c mytool1.h mytool2.hgcc -c $<mytool1.o:mytool1.cmytool1.hgcc -c $<mytool2.o:mytool2.cmytool2.hgcc -c $<
If there are regular naming rules, you can abbreviated them as follows:
main:main.o mytool1.o mytool2.ogcc -o $@ $^..c.o:gcc -c $<