Guidance:
3.1.makefile Compilation
In Linux, make is usually used to manage and compile all the source files of a large development project.
MAKEFILE file to tell the make command how to compile and link the program. makefile is related to the compilation rules of the entire project. There are countless source files in a project, which are stored in several directories by type, function, and module. makefile defines a series of rules to specify which files need to be compiled first, which files need post-compilation, which need to be re-compiled, or even perform more complex functional operations, because makefile is like a shell script and can also execute operating system commands. Some ides in windows, such as Vc, will automatically generate the corresponding makefile, all of which are transparent. But in Linux, you cannot write makefile by yourself. Will you write makefile, shows whether a person has the ability to complete large-scale projects.
The Make tool uses the incremental compilation method. Each time only the source files that have been modified and need to be compiled are compiled, the make tool automatically determines which source files need to be re-compiled, when a project is large and only a few source files are modified, this will save a lot of time.
For more information about how to compile makefile files, see make's man.
And info document (in Linux Command Line Mode, enter man
Make or info make ). There are many rules for writing makefile files. What is important is how to write the makefile files we need in the simplest way.
There are also a lot of introduction materials on the Internet. There is an article on the Internet that describes the preparation of makefile files:
The following describes how to compile the makefile of the project that uses the pwlib development library. However, for other projects, you only need to remove the scripts that compile the pwlib library in the common. Mak file.
3.1.1 compile makefile of the project using the pwlib Development Library
Pwlib is portable
The abbreviation of Windows library is translated into a lightweight windows class library. pwlib is written in C ++. It was designed to enable openh323 to run in windows and Unix X-Windows,
However, with step-by-step improvement, pwlib has been widely used in cross-platform programs.
View the MAKEFILE file of the example program in the/samples/hello_world/directory of pwlib. The contents of the makefile are as follows:
# Simple makefile
For the hello World Program
Prog
= Hello
Sources =
Hello. cxx
Ifndef
Pwlibdir
Pwlibdir = $ (home)/pwlib
Endif
Include
$ (Pwlibdir)/make/ptlib. Mak
# End
Makefile
In fact, the ptlib. Mak file of the pwlib library is used. The header files required for compilation are all set in the ptlib. Mak file.
In the directory where the MAKEFILE file is located, run the make all command on the command line to compile the release and DEBUG Versions of the program, which are placed in the subdirectories obj_linux_x86_r and obj_linux_x86_d of the current directory respectively.
The content in this makefile is described as follows:
L The prog variable is the name of the compiled program.
L The sources variable stores the source files to be compiled and linked for this project. When multiple source files exist, they can be separated by spaces, although the file names can carry paths, however, the path does not work in the sources variable. during actual compilation, only the file names are reserved for all content before the last "/" character in each file.
L pwlibdir is the installation directory of pwlib, you need to set this environment variable (if you want the system to automatically set this environment variable every time you restart the system, put the setting of this environment variable in the/etc/profile file ), if it is not set, "user main directory/pwlib" is automatically used as the installation directory of pwlib.
3.1.2 in-depth analysis of the ptlib. Mak File
Analyze the ptlib. Mak file with the following content;
Ifndef pwlibdir
Pwlibdir = $ (home)/pwlib
Endif
Include
$ (Pwlibdir)/make/Unix. Mak
Include
$ (Pwlibdir)/make/common. Mak
That is, ptlib. Mak contains the Unix. Mak and common. Mak files. Among them, Unix. Mak is the file defining the compilation option variables, and the compilation rules are placed in the common. Mak file.
Through the analysis of these two files, we can summarize some variables we need to use to write the MAKEFILE file, which are listed as follows:
L stdccflags: The include directory compilation options, pre-compiled macro definition options, warning options, and Optimization Options of all header files are placed in this variable, and the compilation options are separated by spaces.
L ldflags: Enter the shared library and static library search directory settings in this variable.
L ldlibs: Set the library to be used for linking to this variable.
L vpath_cxx :*. the search path of the cxx source file is put into this variable. multiple directories are separated by spaces. during compilation, the corresponding source files are searched in the directory where the makefile is located. If the source files are not found, the files are searched according to the path settings in vpath_cxx.
L vpath_c: * put the search path of the. C source file into this variable. multiple directories are separated by spaces. The file search sequence is similar to that of the vpath_cxx variable.
3.1.3 add new compilation rules
Common. only *. C and *. the source file of cxx defines compilation rules, and most programs in Windows use them. CPP is used as the suffix of the C ++ source file.
How to add the compilation rules for the source file with the. cpp suffix? This requires modifying the common. Mak file of pwlib. The specific steps are as follows:
1. Add the search directory settings for the. cpp File
Add the following statement after the vpath %. cxx $ (vpath_cxx) Statement:
Vpath %. cpp $ (vpath_cxx)
2. Add compilation rules for. cpp files
Add the following statement before the $ (objdir)/%. O: %. cxx statement:
$ (Objdir)/%. O: %. cpp
@ If [!
-D $ (objdir)]; then mkdir-p $ (objdir); FI
$ (Cplus)
$ (Stdccflags) $ (optccflags) $ (cflags) $ (stdcxxflags)-x C ++-C $ <-o
$ @
3. Add the naming rules for the. o file (target code file) of the. cpp File
Add the following statement after the src_objs :=$ (src_objs:. cxx =. o) Statement
Src_objs: =
$ (Src_objs:. cpp =. O)
4. Add naming rules for. Dep files (dependent files) of. cpp files
Add the following statement after the src_deps :=$ (src_deps:. cxx =. Dep) Statement
Src_deps: =
$ (Src_deps:. cpp =. Dep)
5. Add the following statement to generate the. Dep file for the. cpp file:
Add the following statement before $ (depdir)/%. Dep: %. cxx
$ (Depdir)/%. Dep: %. cpp
@ If [!
-D $ (depdir)]; then mkdir-p $ (depdir); FI
@ Printf
% S $ (objdir) >$ @
$ (Cplus)
$ (Stdccflags:-G =)-M $ <>>$ @
This article is transferred from
Http://larryjiazhiqiang.blog.sohu.com/67531576.html