Use scons to easily build programs

Source: Internet
Author: User
Tags glob python list

Original article: http://www.ibm.com/developerworks/cn/linux/l-cn-scons/index.html

Reference: http://www.flatws.cn/article/program/python/2011-06-02/28680.html

Preface

Since the launch of the make tool in the Bell lab in the 1970s S, Stuart Feldman has been a Unix-like tool.ProgramMember's favorite. By checking the file modification time, the make tool can know the other files on which the target file is to be compiled. In a complex project, if only a few files have been modified, the make tool knows which files need to be re-compiled to ensure that the target program is properly compiled. The advantage of doing so is that in compilation, not only can save a lot of Repeated input, but also can ensure that the program can be properly linked and shorten the Compilation Time. However, it is not easy to compile building rules for the make tool. Its Complex Configuration Rules are daunting for experienced developers. Many alternatives to the Make tool were born. scons is one of them. Scons is a program written in Python similar to the Make tool. Compared with the make tool, the scons configuration file is simpler and clearer. In addition, it has many advantages.

Back to Top

Scons Introduction

Scons is an openSource code, The next generation of program building tools written in Python. Its initial name was sccons, developed based on the cons software written in Perl, which won the SC build competition held by software carpentry in August 2000. Now sccons has been renamed as scons, so that it is no longer associated with software carpentry. Of course, there is another purpose, that is, to facilitate input.

As the next generation of software building tools, scons is designed to make it easier, more reliable, and faster for developers to build software. Compared with traditional make tools, scons has the following advantages:

    • Use a Python script as the configuration file
    • For C, C ++, and FORTRAN, built-in support for reliable automatic dependency analysis. You do not need to execute "make depends" and "make clean" as the make tool does to obtain all dependencies.
    • Built-in support for C, C ++, D, Java, Fortran, YACC, Lex, QT, swig, And Tex/latex. Users can also expand according to their own needs to obtain the desiredProgramming Language.
    • Supports parallel build of the make-J style. Compared with make-J, scons can run n jobs at the same time without worrying aboutCode.
    • Use Autoconf to search for header files, function libraries, functions, and type definitions.
    • Excellent platforms. Scons can run on Linux, Aix, BSD, HP/UX, Irix, Solaris, windows, Mac OS x, and OS/2.

Back to Top

Install scons

Scons supports a variety of operating system platforms and creates easy-to-install files for each system. Therefore, the installation methods on each system platform are different, on the scons official website, you can check the specific installation methods for each platform. If scons does not have an installation package for your system, you can download the source code of scons and install it directly. First, download the latest scons source code package (the latest version of scons is 2.0.1) from the scons website ). Next, extract the downloaded source code. There are different methods depending on the format of the downloaded source code package. on the Windows platform, you can decompress the package using WinZip or other similar tools. On Linux, run the tar command to decompress the tar package, for example:

$ Tar-zxf scons-2.0.1.tar.gz

 

Switch to the decompressed directory for installation, as shown in figure

$ CD scons-2.0.1 $ sudo Python setup. py install

 

If there is no error in command execution, scons will be installed on the system. For Linux, scons will be installed in the/usr/loca/bin directory by default, while on Windows, scons will be installed in c: \ python25 \ scripts.

Back to Top

Use scons

After scons is installed, we can use scons to build our programs or projects. As in many programming books, here we also use a simple helloscons example to illustrate how to use scons. The example helloscons contains two files:

$ Ls helloscons. c sconstruct

 

Helloscons. c is the source file of the program, and sconstruct is the configuration file of scons, similar to the MAKEFILE file when the make tool is used. Therefore, to compile your project, you need to manually create a sconstruct file (note: the file name is case sensitive ). However, you do not need to specify it during compilation. To compile this example, switch to the helloscons directory and run the scons command as follows:

$ CD helloscons/$ 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.

 

To view the result after running the scons command:

$ Ls helloscons. c helloscons. O sconstruct

 

After the build, the binary file helloscons and some target files generated during the compilation process ended with. O are obtained. Run helloscons and you will get:

$./Helloscons hello, scons!

 

Now let's look back at the helloscons example. helloscons. c is the only source code file in this example. What it does is to output a simple "Hello, scons" line on the console. Its source code is as follows:

Listing 1. helloscons. c

# Include <stdio. h> # include <stdlib. h> int main (INT argc, char * argv []) {printf ("Hello, scons! \ N "); Return 0 ;}

 

The content of the configuration file sconstruct, which serves as the project construction rule, is as follows:

Listing 2. sconstruct File

Program ('helloscons. C ')

 

You may be surprised that sconstruct has only one line of content, but it is much simpler than traditional makefile. Sconstruct is written in the syntax of a Python script. You can write it like a Python script. The program is the compilation type. It indicates that you want to build an executable binary program which is generated by the helloscons. c file. The name of the generated executable program is not specified here. But don't worry, scons will remove the suffix of the source code file name and use it as the name of the executable file. Here, we can execute the cleanup task without specifying the cleanup action like makefile. In scons, the cleanup task is specified by the-C parameter, as follows:

$ Scons-C scons: Reading sconscript files... scons: done reading sconscript files. scons: cleaning targets... removed helloscons. O removed helloscons scons: done cleaning targets. $ ls helloscons. c sconstruct

 

It doesn't matter if you don't want to compile executable binary files directly. Scons supports multiple compilation types. You can choose one of them as needed. Scons supports the following compilation types:

    • Program: Compile to an executable program (which is an EXE file on Windows), which is the most common compilation type.
    • Object: only compiled into the target file. With this type, only the target file is generated after compilation. In POSIX, the target file ends with. O and ends with. OBJ on Windows.
    • Library: compile it into a library file. The default library compiled by scons refers to the static Link Library.
    • Staticlibrary: The Displayed Library is compiled into a static link library, which has the same effect as the above library.
    • Sharedlibrary: Compile the dynamic link library on the POSIX system, and compile the DLL on the Windows platform.

This simple sconstruct configuration file shows from one side how simple it is to use scons to build a program. In actual project development, the program construction rules are far more complex than the helloscons example. However, this is not a problem. You can extend sconstruct just like extending your own Python script file. if you do not want to use sconstruct to set the default executable file name for you, but select your preferred name, such as myscons, you can change the content of sconstruct:

Program ('myscons, 'helloscons. C ')

 

Myscons is the name of the executable file you want. You can replace it with any name you like, but note that this name must be placed first. Then run the scons command in the helloscons directory to obtain the executable myscons file, as shown below:

$ Scons-Q gcc-O helloscons. O-C helloscons. c gcc-O myscons helloscons. o

 

The-Q Parameter reduces the redundancy information generated by scons during compilation. If your project is composed of multiple source files and you want to specify some macro definitions for compilation and explicitly specify certain libraries, these are very simple for scons. Another example is helloscons2. Helloscons2 consists of three source files: helloscon2.c, file1.c, and file2.c. It also specifies the compilation options and the specific libraries used. Let's take a look at the sconstruct file of helloscons2:

Program ('helloscons2 ', ['helloscons2. c', 'file1. c', 'file2. C '], Libs = 'M', libpath = ['/usr/lib', '/usr/local/lib'], ccflags ='-dhelloscons ')

 

As you imagine, Such a configuration file is not complicated. the sconstruct file indicates that it will generate an executable program named helloscons2 consisting of helloscons2.c, file1.c, and file2.c. Note: multiple source files must be placed in a python list. If you have many source code files and dozens or even hundreds of files, you can use glob ('*. C ') to replace the source code list. As follows:

Program ('helloscons2 ', glob (' *. C ')

 

In the configuration file, Libs, libapth, and ccflags are the built-in Keywords of scons. Their functions are as follows:

    • Libs: indicates the library to be used during the link process. If there are multiple libraries, put them in a list. In this example, we use a library called M.
    • Libpath: The Search Path of the Linked Library. Multiple Search paths are put in one list. In this example, the library search path is/usr/lib and/usr/local/lib.
    • Ccflags: Compilation option. You can specify any compilation option. If multiple options are available, they should be placed in one list. In this example, the compile option defines a macro helloscons through the-d gcc option.

When running the scons command, we can see how these variables are used. Let's execute the scons command:

$ Scons-Q gcc-O file1.o-C-dhelloscons file1.c gcc-O file2.o-C-dhelloscons file2.c gcc-O kernel-C-kernel gcc-O helloscons2 kernel file1.o file2.o-l /usr/lib-L/usr/local/lib-LM

 

The scons command output shows how the executable program helloscons2 is generated by multiple source files, and how libs, libpath, and ccflags defined in sconstruct are used. It can be seen that scons compilation configuration files are simple even for complex projects. In addition, scons provides many functions to adapt to different needs. If you want to learn more about how to use scons, refer to scons help manual.

Back to Top

Summary

This article briefly introduces the features of scons, how to install scons, and how to use scons in projects through examples. As the next generation software building tool, scons uses the Python language as the configuration file, which is not only powerful, but also easy to use. It is very suitable for cross-platform projects. If you get bored with the complex coding rules of the make tool, try the new scons.

 

Back to Top

Download

 

description name size download method
sample code used in this article sample.zip 10 KB HTTP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.