1. Background
in Linux under the written C or C + +, when the file is small, we can be compiled into a single executable file to complete, but there are many files, especially a file because the call to a file function and need to include the compilation, then the individual compilation is very laborious, It's time to have Project management tools----->makefile.
With a large number of IDES (integrated development environments) under Windows, Project management can be accomplished through the IDE, thus reducing project management headaches in development.
2. compiling and linking of programs
In general, whether C or C + +, the first to compile the source file into an intermediate code file, under Windows is the. obj file, under Unix is the. o file, the Object file, this action is called compilation (compile), in general, Each source file should correspond to an intermediate target file (o file or obj file). And then the large number of object file synthesis execution file, this action is called link.
At compile time, the compiler needs the correct syntax, and the declaration of functions and variables is correct. For the latter, usually you need to tell the compiler where the header file is located (the header file should only be declared, and the definition should be in a C + + file), as long as all the syntax is correct, the compiler can compile the intermediate target file.
Links are primarily link functions and global variables, so we can use these intermediate target files (o files or obj files) to link to our application. Linker and regardless of the function of the source file, just the function of the intermediate target files (object file), most of the time, due to too many source files, compile the resulting intermediate target file too many, and in the link need to clearly indicate the intermediate target file name, which is very inconvenient for compiling, so we have to give Intermediate target file is a package, under Windows This package is called "library file", that is,. lib file, under Unix, is archive file, that is,. a files.
Let's take a look at how to quickly write your own easy-to-use project management tools--->makefile
3. Makefile
Simply put, makefile defines a 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 shell scripts can be executed in makefile. Makefile brings the advantage is-"automated compilation", once written, only need a make command, the entire project is automatically compiled, greatly improving the efficiency of software development.
Makefile Rules:
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/82/F2/wKioL1dmugfiZu_NAACkVTVRfK8679.png-wh_500x0-wm_3 -wmp_4-s_1465503602.png "title=" 1_1 makefile Basic principles. png "alt=" wkiol1dmugfizu_naackvtvrfk8679.png-wh_50 "/>
Makefile Working principle:
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/82/F2/wKioL1dmujnQaPFsAACAj8CEvZ4987.png-wh_500x0-wm_3 -wmp_4-s_3955356709.png "title=" 1_3 makefile Working principle -2.png "alt=" Wkiol1dmujnqapfsaacaj8cevz4987.png-wh_50 "/>
variables and functions commonly used in Makefile: [Remember]
-
Common functions:
Common variables
[email protected] The full name of the target [frequently used]
$^ all dependent files, separated by spaces, do not contain duplicate dependent files [frequently used]
$< name of the first dependent file [frequently used]
$* the name of the target file that does not contain an extension
$+ all dependent files, separated by spaces, and in order of occurrence, may contain duplicate dependent files
introducing so much, let's look directly at the code below.
1. compile all the. c files in the current directory [can also specify the build target file]
SRC = $ (wildcard./*.c) #获取当前目录下所有的. c File obj = $ (patsubst./%.C,./%, $ (SRC)) #将所有. c c file is removed, and the parameters specified at compile time-lpthread the required library for the specified thread,- STD=GNU99 Specify C99 standard compilation #-g add debug information for GDB debugging use-wall more tips CC = gcc-g-wall-std=gnu99-lpthreadall:$ (OBJ) #生成所有目标%:%.c$ (cc) $< -O [email protected] #伪目录, making the Make command does not generate subsequent targets, which are commonly used in cleanup commands. phony:clean# cleanup all target files ' @ ' does not output to screen clean: @rm-F $ (OBJ)
2. Specify the compilation target file
src = $ (wildcard./*.c) #获取当前目录下所有的. c Files obj = $ (patsubst./%.C,./%.O, $ (src)) #将所有. c files are assigned to. o# variable assignments, parameters specified at compile time-lpthread the specified thread Need library, #-std=gnu99 specify C99 standard compile-G to add debug information for GDB debugging use-wall more tips cc = gcc-g-wall-std=gnu99-lpthread# Specify target genetic server Clientall:serve R client# Specify genetic server target dependencies and commands SERVER:WARP.O server.o$ (CC) $^-o [email protected] #指定生成client目标所依赖项和命令client: WARP.O client.o$ (cc) $^-o [email protected]%.o:%.c$ (cc)-C $<-o [email protected] #伪目录, making the Make command does not generate subsequent targets, and is commonly used in cleanup commands. Phony:cleanclean: @rm-F $ (obj) #清理所有. o file ' @ ' does not output to the screen
Makefile Dependency principle
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/82/F3/wKiom1dmv4LjsCrkAADtHGagyF4530.png-wh_500x0-wm_3 -wmp_4-s_3462728802.png "title=" 1_2 makefile Working principle -1.png "alt=" Wkiom1dmv4ljscrkaadthgagyf4530.png-wh_50 "/>
This article is from the "Sea" blog, be sure to keep this source http://lisea.blog.51cto.com/5491873/1790902
Linux Network Programming-----> Project Management Tools----->makefile