Introduction and use of Scons automatic large-scale system building tools
Author: Yu Chao email: yuchao86@gmail.com
1. Overview
Scons is an automated building tool written in Python. From the perspective of building, it is the same as GNU make. The key to Make is to build the dependency between files and then compile the project based on the dependency. The dependency needs to be built manually or manually. This is not difficult for large systems. Scons can use programs to analyze the dependencies between files and compile and generate the desired files. This is similar to automake, but automake analyzes dependencies using programs to help generate makefiles.
Similar to automake/autoconf, Scons is the next generation Software Automatic Build tool. It is cross-platform and can help us build software more conveniently, reliably, and quickly. When using scons, you only need to write an SConstruct file (which is actually a python file). Based on this file, scons can automatically complete the dependency relation derivation and compilation links.
2. Initial Scons experience
First, install the scons software and run the command: sudo apt-get install scons.
Create the/home/yuchao/dev/scons directory and write the helloscons. c file and SConstruct file respectively. The content of the two files is:
(1) Compile the executable file and use the Program function directly.
[Yuchao @ yuchao-Latitude-E5410 scons] $ pwd
/Home/yuchao/dev/scons
[Yuchao @ yuchao-Latitude-E5410 scons] $ ls
Helloscons. c SConstruct
[Yuchao @ yuchao-Latitude-E5410 scons] $ cat helloscons. c
/**
* This file is a like Makefile build project
*
* Filename: helloscons. c
* Email: yuchao86@gmail.com
*
*/
# Include <stdio. h>
Int main (int argc, char * argv [])
{
Printf ("Hell Scons, I'm YuChao \ n ");
Return 0;
}
[Yuchao @ yuchao-Latitude-E5410 scons] $ cat SConstruct
# Makefile file
Program ('helloscons. C ');
[Yuchao @ yuchao-Latitude-E5410 scons] $ scons
Scons: Reading SConscript files...
Scons: done reading SConscript files.
Scons: Building targets...
Gcc-o helloscons. o-c helloscons. c
Gcc-o helloscons. o
Scons: done building targets.
[Yuchao @ yuchao-Latitude-E5410 scons] $ ls
Helloscons. c helloscons. o SConstruct
[Yuchao @ yuchao-Latitude-E5410 scons] $./helloscons
Hell Scons, I'm Yao AO
(2) Compile the static library
Use the Library or staticLibrary function:
Library ("helloscons", ['helloscons. c'])
[Yuchao @ yuchao-Latitude-E5410 scons] $ ls
Helloscons. c SConstruct
[Yuchao @ yuchao-Latitude-E5410 scons] $ cat SConstruct
# Makefile file
# Program ('helloscons. C ');
Library ("helloscons", ['helloscons. c'])
[Yuchao @ yuchao-Latitude-E5410 scons] $ scons
Scons: Reading SConscript files...
Scons: done reading SConscript files.
Scons: Building targets...
Gcc-o helloscons. o-c helloscons. c
Ar rc libhelloscons. a helloscons. o
Ranlib libhelloscons.
Scons: done building targets.
[Yuchao @ yuchao-Latitude-E5410 scons] $ ls
Helloscons. c helloscons. o libhelloscons. a SConstruct
[Yuchao @ yuchao-Latitude-E5410 scons] $
(3) Compile the dynamic library
Use SharedLibrary functions:
SharedLibrary ('helloscons', ['helloscons. c'])
[Yuchao @ yuchao-Latitude-E5410 scons] $ ls
Helloscons. c SConstruct
[Yuchao @ yuchao-Latitude-E5410 scons] $ cat SConstruct
# Makefile file
# Program ('helloscons. C ');
# Library ("helloscons", ['helloscons. c'])
SharedLibrary ('helloscons', ['helloscons. c'])
[Yuchao @ yuchao-Latitude-E5410 scons] $ scons
Scons: Reading SConscript files...
Scons: done reading SConscript files.
Scons: Building targets...
Gcc-o helloscons. OS-c-fPIC helloscons. c
Gcc-o libhelloscons. so-shared helloscons. OS
Scons: done building targets.
[Yuchao @ yuchao-Latitude-E5410 scons] $ ls
Helloscons. c helloscons. OS libhelloscons. so SConstruct
[Yuchao @ yuchao-Latitude-E5410 scons] $
4. Scons compile multiple programs
(1) list all source files to be compiled in the form of python arrays. For example, to compile file1.c, file2.c, and file3.c to generate the executable file Hello, you can write SConstruct as follows:
Program ('hello', ['file1. C', 'file2. C', 'file3. c'])
(2) Use a regular expression (using the Glob function ):
Program ('hello', Glob ('*. C '));
[Yuchao @ yuchao-Latitude-E5410 scons] $ ls
Helloscons. c helloSina. h SConstruct weibo. h
[Yuchao @ yuchao-Latitude-E5410 scons] $ cat weibo. h
/**
* This file is a like Makefile build project
*
* Filename: weibo. h
* Email: yuchao86@gmail.com
*
*/
# Ifndef _ WEIBO_H _
# Define _ WEIBO_H _
Int weibo ()
{
Printf ("Hell weibo, I'm YuChao \ n ");
Return 0;
}
# Endif/* _ WEIBO_H _*/
[Yuchao @ yuchao-Latitude-E5410 scons] $ cat helloSina. h
/**
* This file is a like Makefile build project
*
* Filename: helloSina. h
* Email: yuchao86@gmail.com
*
*/
# Ifndef _ HELLOSINA_H _
# Define _ HELLOSINA_H _
Int sina ()
{
Printf ("Hello SINA, I'm Yao AO \ n ");
Return 0;
}
# Endif/* _ HELLOSINA_H _*/
[Yuchao @ yuchao-Latitude-E5410 scons] $ cat helloscons. c
/**
* This file is a like Makefile build project
*
* Filename: helloscons. c
* Email: yuchao86@gmail.com
*
*/
# Include <stdio. h>
# Include "helloSina. h"
# Include "weibo. h"
Int main (int argc, char * argv [])
{
Printf ("Hell Scons, I'm YuChao \ n ");
Sina ();
Weibo ();
Return 0;
}
[Yuchao @ yuchao-Latitude-E5410 scons] $ cat sconstruct
# MAKEFILE file
Program ('hello', glob ('*. C '));
# Library ("helloscons", ['helloscons. c'])
[Yuchao @ yuchao-Latitude-E5410 scons] $ scons
Scons: Reading sconscript files...
Scons: done reading sconscript files.
Scons: building targets...
Gcc-O helloscons. O-C helloscons. c
Gcc-O hello helloscons. o
Scons: done building targets.
[Yuchao @ yuchao-Latitude-E5410 scons] $ ls
Hello helloscons. c helloscons. O hellosina. h sconstruct Weibo. h
[Yuchao @ yuchao-Latitude-E5410 scons] $./Hello
Hell scons, I'm Yao AO
Hello Sina, I'm Yao AO
Hell Weibo, I'm Yao AO
[Yuchao @ yuchao-Latitude-E5410 scons] $
(3) Link Library
Library ('foo', ['f1. C', 'f2. C', 'f3. c'])
Program ('prog. C', LIBS = ['foo', 'bar'], LIBPATH = '.', CCFLAGS = 'dhello ')
5. Summary
As a new and efficient automatic Software Component tool, Scons greatly simplifies software development and maintenance.
6. References
Scons home: http://www.scons.org/