[Scons translation scons learning] 2. simplified the compilation process

Source: Internet
Author: User
Tags glob python list

Chapter 3. less simple things to do with builds

In this chapter, you will see some simple compilation configuration examples.

3.1 specifying the name of the target (output) File
=====
When you use program to compile the project, the default output file name is the same as the source file name.
Program ('hello. C ')
If you want to use another output name, you only need to specify it on the left side of the source file name. In scons, the first appearance of the target file is required, followed by the source file.
Program ('new _ hello', 'Hello. C ')

% Scons-Q
CC-O hello. O-C hello. c
CC-O new_hello hello. o

3.2 compiling multiple source file
=====
In actual projects, more than one source file must be compiled. In scons, you need to put the source file in a python list.
Program (['prog. C', 'file1. C', 'file2. c'])

% Scons-Q
CC-O file1.o-C file1.c
CC-O file2.o-C file2.c
CC-O Prog. O-C Prog. c
CC-O prog Prog. O file1.o file2.o
In scons, the target file name is automatically exported as the first source file name, which is prog. C. Similarly, if you want to specify a different target file name, move the list to the right. The first item is the specified output file name.
Program ('program ', ['prog. C', 'file1. C', 'file2. c'])

% Scons-Q
CC-O file1.o-C file1.c
CC-O file2.o-C file2.c
CC-O Prog. O-C Prog. c
CC-O program Prog. O file1.o file2.o

3.3 making a list of files with glob
=====
Sometimes it is complicated to specify the source file list one by one. For example, a folder contains 100 source files. In this case, you can use the scons glob function to automatically match some specified function names. For example, you can compile all. c files in the folder.

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

3.4 specifying single files vs. Lists of Files
=====
We already know two ways to specify the source file list. One is.
Program ('hello', ['file1. C', 'file2. c'])
The other is.
Program ('hello', 'Hello. C ')
Of course, you can also put only one source file in the list.
Program ('hello', ['hello. c'])
Although both string and list formats work here, scons treats them as lists. In python, the list and string cannot be directly added, but can be added between lists. Therefore, when you want to add a source file to the source file list, you cannot use a string.
Program ('gram1', common_sources + 'program1. C ')
# Here an error is reported, because the list tries to add the string to the list.
The following format should be used.
Program ('gram2', common_sources + ['program2. c'])

3.5 making lists of files easier to read
=====
One disadvantage of using the list to organize source files is that it is not easy to read. Each source file must be enclosed in quotation marks. Both scons and Python provide functions to handle this situation. In scons, you can use the split function to convert a string to a list. The string is separated by spaces.
Program ('signature', split ('main. c file1.c file2.c '))
This is actually similar to the split () function in Python, and the split function is more flexible. Put a long string of source file lists in program, which is not beautiful. You can use a variable to store the source file list.
Src_files = Split ('main. c file1.c file2.c ')
Program ('program ', src_files)
The split function does not care about the number of spaces, so it can also be written as this.
Src_files = Split ("" Main. c
File1.c
File2.c """)
Program ('program ', src_files)

3.7 compiling multiple programs
=====
Multiple targets can be compiled and output in an sconstruct file. You only need to call program multiple times.
Program ('foo. C ')
Program ('bar', ['bar1. C', 'bar2. c'])

% Scons-Q
CC-O bar1.o-C bar1.c
CC-O bar2.o-C bar2.c
CC-o Bar bar1.o bar2.o
CC-O Foo. O-C Foo. c
CC-O Foo. o
It is worth noting that scons is not compiled in the order of the program function. In fact, the compilation sequence is determined by dependency.

3.8 sharing source files between multiple programs
=====
It is common to reuse code between programs. One of the methods is to use the library and then link it during program compilation. This is detailed in chapter 4.
However, a more direct method is to directly include the same source file.
Program (split ('foo. c common1.c common2.c '))
Program ('bar', split ('bar1. c bar2.c common1.c common2.c '))

% Scons-Q
CC-O bar1.o-C bar1.c
CC-O bar2.o-C bar2.c
CC-O common1.o-C common1.c
CC-O common2.o-C common2.c
CC-o Bar bar1.o bar2.o common1.o common2.o
CC-O Foo. O-C Foo. c
CC-O Foo. O common1.o common2.o
If the two programs share a lot of source files, it is too troublesome to write them one by one. In this case, use the python syntax to solve the problem.
Common = ['common1. C', 'common2. c']
Foo_files = ['foo. c'] + common
Bar_files = ['bar1. C', 'bar2. c'] + common
Program ('foo', foo_files)
Program ('bar', bar_files)

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.