There are countless source files in a project, which are placed in several directories by type, function, module, makefile define series of rules to specify, which files need to be compiled first, which files need to be compiled, which files need to be recompiled, and even more complex functional operations, Because makefile is like a shell script, it can also be a command of the operating system.
Programmers in a Linux environment should not be considered a qualified professional programmer if they do not use gun make to build and manage their own projects. Using the gun's Make tool in a Linux (Unix) environment makes it easier to build a project of your own, and the entire project's compilation requires only one command to compile, connect, and ultimately execute. But it will take us some time to complete one or more of the writing called makefile files.
The makefile file to be completed describes the compilation, connection, and other rules for the entire project. These include: which source files of the project need to be compiled, which library files need to be created, how to create these library files, and how to generate the executable file we want. While it may seem complicated, the benefit of writing makefile for engineering is the ability to use a single line of commands to automate compilation, once the correct makefile is provided. Compiling the entire project the only thing you need to do is to enter the make command at the shell prompt, which compiles completely automatically, greatly improving efficiency.
Makefile structure
#表示注释
Variable definition
Var=test defining variable var, forcing Zhi Wei test
Var+=app Add the app value after the value defined before Var
Var?=testapp if the previous VAR is not defined, define and use TestApp, otherwise use the previous value
The first goal is the overall goal
Dependencies can be files (directories) or for other destinations
The action can be a Linux command, which line of the action must be the first character to be padded with the TAB key.
Target:depend1 depend2 Depend3 ...
[TAB] Action1
[TAB] Action2
Target
[TAB] Action1
[TAB] Action2
Use of Makefile
Make find makefile or makefile file to perform the overall goal
Make clean executes the clean target in the makefile file
Make-c directory into the directory folder to perform the overall goal
Make clean-c directory into the directory folder to execute the clean target
Make-f Comm_makefile specifying a makefile file with the-F option
Make Var=value a parameter VAR to makefile with a value of
Case:
Create Vendor1 and Vendor2 two folders respectively, and put two functions in the two folders
[Email protected] library]$ mkdir Vendor1
[Email protected] library]$ mkdir Vendor2
[Email protected] library]$ CD Vendor1
[Email protected] vendor1]$ vim CRYPTO.C
#include <stdio.h>void crypto (void) { printf ("Start Crypt ... "); return ;}
[Email protected] vendor1]$ vim crypto.h
#ifndef _crypto_h_ #define _crypto_h_void CRYPTO (void); #endif
[Email protected] vendor1]$ Vim makefile
Lib_name=vendor1all:shared static -F *. oshared :-shared-fpic-o Lib${lib_name}.so *. Cstatic: -c *. -rcs lib${lib_name}.a *. Oclean: -F *.a * .so-f *. Oinstall: CP lib${lib_name}. * .. /libs
[Email protected] vendor1]$ CD. /vendor2
[Email protected] vendor2]$ vim FUNC.C
#include <stdio.h>void func (void) { printf ("Start func ... " ); return ;}
[Email protected] vendor2]$ vim func.h
#ifndef _func_h_ #define _func_h_void FUNC (void); #endif
[Email protected] vendor2]$ Vim makefile
Lib_name=vendor2all:shared static -F *. oshared :-shared-fpic-o Lib${lib_name}.so *. Cstatic: -c *. -rcs lib${lib_name}.a *. Oclean: -F *.a * .so-f *. Oinstall: CP lib${lib_name}. * .. /libs
[Email protected] vendor2]$ CD.
[Email protected] library]$ vim MAIN.C
" crypto.h " "func.h"int main (void) { crypto (); Func (); return 0 ;}
[Email protected] library]$ Vim makefile
app_name?=appall:lib_vendor1 lib_vendor2 -static Main.c-ivendor1-ivendor2-o ${app_name }-lvendor1-lvendor2-lvendor1-lvendor2lib_vendor1: -C vendor1lib_vendor2: -A Vendor2
[[email protected] library]$ ls
MAIN.C Makefile Vendor1 Vendor2
[[email protected] library]$ make
make-c Vendor1
make[1]: Entering directory '/home/xiaohexiansheng/cc/library/vendor1 '
gcc-shared-fpic-o libvendor1.so *.c
gcc-c *.c
Ar-rcs libvendor1.a *.o
rm-f *.O
make[1]: Leaving directory '/home/xiaohexiansheng/cc/library/vendor1 '
make-c Vendor2
make[1]: Entering directory '/home/xiaohexiansheng/cc/library/vendor2 '
gcc-shared-fpic-o libvendor2.so *.c
gcc-c *.c
Ar-rcs libvendor2.a *.o
rm-f *.O
make[1]: Leaving directory '/home/xiaohexiansheng/cc/library/vendor2 '
gcc main.c-ivendor1-ivendor2-o app-lvendor1-lvendor2-lvendor1-lvendor2
[[email protected] library]$ ls
app main.c makefile Vendor1 Vendor2
Production of Makefile