Scons User Guide Chapter 2: simple compilation

Source: Internet
Author: User

1. Compile a simple C/C ++ Program


This is a famous "Hello, world!" written in C language! "Program: int main () {printf (" Hello, world! \ N ") ;}use scons to compile it. Enter the following command in a file named sconstruct: Program ('Hello. C ') This short configuration file gives scons two pieces of information: What You Want To compile (an executable program), the input file you compile (hello. c ). Program is a compiler method (builder_method). A python call Tells scons that you want to compile an executable program.
Run the scons command to compile the program. On Linux or Unix systems, you will see the following output: % sconsscons: Reading sconscript files... scons: done reading sconscript files. scons: building targets... CC-O hello. o-C hello. CCC-O hello. oscons: done building targets.
On a Windows system with Microsoft Visual C ++ compiler, you will see the following output: C: \> sconsscons: Reading sconscript files... scons: done reading sconscript files. scons: building targets... CL/fohello. OBJ/C hello. c/nologolink/nologo/out: hello.exe hello. objembedmanifestexecheck (target, source, ENV) scons: done building targets.
First, you only need to specify the source file. scons will correctly infer the name of the target file and executable file. Second, the same sconstruct file generates the correct output file in both Linux and Windows: hello.oand hello.exe on the posixsystem, and hello.objand hello.exe on the Windows system. This is a simple example. It is explained that scons makes it easy to compile scripts.
2. Compile the Target Program
The program compilation method is one of the many compilation methods provided by scons. The other is the object compilation method, which tells scons to compile a target file from the specified source file: Object ('hello. C ')
Now run the scons command to compile. In the POSIX system, it only compiles hello. O target file: % sconsscons: Reading sconscript files... scons: done reading sconscript files. scons: building targets... CC-O hello. o-C hello. cscons: done building targets.
Compile hello in windows. OBJ target file: C :\> sconsscons: Reading sconscript files... scons: done reading sconscript files. scons: building targets... CL/fohello. OBJ/C hello. c/nologoscons: done building targets.
3. Simple Java Compilation
Scons also makes it easy to compile Java. Unlike the two compilation methods of program and object, you need to specify a directory for the Java compilation method. This directory is used to store the compiled class file and one for storing. directory of the Java source file: Java ('classe', 'src ')
If the src directory contains only one hello. java file, the output of running scons command will be as follows (in POSIX system): % sconsscons: Reading sconscript files... scons: done reading sconscript files. scons: building targets... javac-D classes-sourcepath SRC src/hello. define cons: done building targets.
4. Clear after compilation
After using scons, you do not need to add a special command or target name after compilation. When you call scons, scons deletes the appropriate files generated by compilation by using the-C or -- clean option. % Scons-C
5. sconstruct File
If you have used the make compilation system, you can infer that the sconstruct file is equivalent to the makefile in The make system. Scons reads the sconstruct file to control program compilation.
5.1. The sconstruct file is a Python scriptThe sconstruct file is actually a Python script. You can use Python annotations in your sconstruct file: # arrange to build the "hello" program. program ('Hello. c') # "hello. C "is the source file.
5.2. scons functions are order-independent.The important thing is that the sconstruct file does not work exactly as a normal Python script, and it works more like a makefile, that is, the sequence in which the scons function is called in the sconstruct file does not affect the sequence in which scons you actually want to compile programs and target files. In other words, when you call the program method, you do not tell scons to compile the program immediately while calling the method, but tell scons that you want to compile the program. For example, A program consists of a hello. the C file is compiled, which is determined by scons to compile the program when necessary. Scons prints the output status message to show when it reads the sconstruct file and when it actually compiles the target file, then we can distinguish whether to call a program-like compilation method or compile the program in reality.
Let's take a look at the following example: Print "calling program ('hello. C ')"

Program ('hello. C') print "calling program ('Goodbye. C')" Program ('Goodbye. C') print "finished calling program ()"
Execute scons. We can see that the output of the print statement is between the messages reading the sconstruct file, indicating that the python statement is executed: % sconsscons: Reading sconscript files... calling Program ('Hello. C ') calling program ('Goodbye. C ') finished calling program () scons: done reading sconscript files... scons: building targets... CC-O goodbye. o-C goodbye. CCC-O goodbye. OCC-O hello. o-C hello. CCC-O hello. oscons: done building targets.
6. Make scons output more concise
You have seen that some messages will be printed during scons compilation. The messages are centered around the commands actually used to compile the program: C: \ sconsscons: Reading sconscript files... scons: done reading sconscript files. scons: building targets... CL/fohello. OBJ/C hello. c/nologolink/nologo/out: hello.exe hello. objembedmanifestexecheck (target, source, ENV)

Scons: done building targets. These messages reflect the sequence in which scons work.
One drawback is that these messages make the output look messy. When scons is called, The-Q option can be used to shield messages irrelevant to the actual compiled program commands: C: \> scons-qcl/fohello. OBJ/C hello. c/nologolink/nologo/out: hello.exe hello. objembedmanifestexecheck (target, source, ENV)

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.