Use scons, scons
1. Overview
Scons is an automated building tool written in Python. It has obvious advantages over GNU make:
A. Portability: scons can be run wherever python can run.
B. Scalability: In theory, scons only provides python classes. scons users can do all the things that python can do on the basis of this class. For example, if you want to switch a large project that has used Makefile to scons, You can retain the original Makefile and use python to parse the compilation options and source/target files in Makefile and pass them as parameters to scons, complete Compilation.
C. Intelligence: Scons inherits the autoconf/automake function to automatically parse the include path and typedef of the System. "All dependencies are viewed globally"
2. scons File
Possible files in scons:
SConstruct, Sconstruct, sconstruct, SConscript
Scons will search for configuration files in the following sequence in the current directory: SConstruct, Sconstruct, and sconstruct, and read the relevant configuration from the first file to be read.
In the configuration file SConstruct, you can use the SConscript () function to set the attached configuration file. By convention, these ancillary configuration files are named "SConscript", and you can also use any other name.
3. scons command line parameters
1 scons: Execute the SConstruct Script 2 scons-c clean3 scons-Q to display only the compilation information, remove unnecessary print information 4 scons-Q -- implicit-cache hello save dependency 5 -- implicit-deps-changed force update dependency 6 -- implicit-deps-unchanged force use original dependency link, even if it has changed
4. Method 4.1 Program provided by SConstruct: generate executable files
1 Program ('Hello. c ') // compile the hello.cexecutable file, and the system automatically generates (hello.exe on Windows; hello on POSIX) 2 Program ('hello', 'Hello. c') // specifies the outputfile name (hello.exe on Windows; hello on POSIX) 3 Program (['Hello. c', 'file1. c', 'file2. c ']) // compile multiple files. The Output file name is named 4 Program (source = "hello. c ", target =" hello ") 5 Program (target =" hello ", source =" hello. c ") 6 Program ('hello', Split ('hello. c file1.c file2.c ') // compile multiple files 7 8 Program (Glob ("*. c ") 9 src = [" hello. c "," foo. c "]; Program (src)
4.2 Object: generate the target file
1 Object ('hello. C') // compile the target hello. c file, which is automatically generated according to the system (hello. obj on Windows; hello. o on POSIX)
4.3 Library: generate static/dynamic Library files
1 Library ('foo', ['f1. c', 'f2. c', 'f3. c ']) // compile library2 SharedLibrary ('foo', ['f1. c', 'f2. c', 'f3. c ']) // compile shared library3 StaticLibrary ('bar', ['f4. c', 'f5. c', 'f6. c ']) // compile the static library
Library usage:
1 Program ('prog. C', LIBS = ['foo', 'bar'], LIBPATH = '.') // connects to the database without a suffix or prefix.
4.4 SourceSignatures: Determine whether the source file is modified
1 SourceSignatures ('md5') // determines whether the content is changed. The default method is 2 SourceSignatures ('timestamp') // Based on the modification time
4.5 TargetSignatures: determines whether the target file has changed
1 TargetSignatures ('built') // Based on the Compilation result 2 TargetSignatures ('content') // Based on the file content, if only a sentence comment is added, it will not be re-compiled
4.6 Ignore: Ignore dependency
1 Ignore (hello, 'Hello. H') // Ignore a dependency
4.7 Depends: Clear dependency
1 Depends (hello, 'other _ file') // specify the dependency
4.8 SConscript: scons configuration file
The directory structure of the source file is as follows:
Src:
| SConstruct
| Test. cpp
| MA (directory ):
| SConscript
| Func. cpp
Test. cpp is the main file, which calls the function defined in func. cpp.
The content of SConstruct is as follows:
1 env = Environment()2 flags = env.ParseFlags(['-pthread -I/usr/include/stlport ',' -L .'])3 env.MergeFlags(class_flags)4 subobj = SConscript(['mA/SConscript'])5 obj = subobj + env.Object(Glob("*.cpp"))6 env.Program("test",list(obj),LIBS = ['libstlport.a'])
MA/SConscrip:
1 obj = Object(Glob("*.cpp"))2 Return("obj")
The above project can be compiled without an accident, but it will be Aborted during running. Because test. cpp, mA/func. cpp uses the class containing the string type, but test. cpp considers the size of the string variable to be 24 bytes, mA/func. cpp considers the size of the string variable to be 4 bytes (libstlport. a hacker ).
The solution is to output environment variables. Modify SConstruct and mA/SConscript as follows:
SConstruct:
1 env = Environment()2 flags = env.ParseFlags(['-pthread -I/usr/include/stlport ',' -L .'])3 env.MergeFlags(class_flags)4 Export('env')5 subobj = SConscript(['mA/SConscript'],exports = 'env')6 obj = subobj + env.Object(Glob("*.cpp"))7 env.Program("test",list(obj),LIBS = ['libstlport.a'])
MA/SConscript:
1 Import('env')2 obj = env.Object(Glob("*.cpp"))3 Return("obj")