Recipe 1.7. Building a Simple "Hello, World" Application Using Boost.BuildProblemYou want to use Boost.Build to build a simple "Hello, World" program, such as the one in Example 1-4. Solution
Create a text file named
Jamroot
in the directory where you wish the executable and any
For example, to build an executable hello or hello.exe from the file hello.cpp in Example 1-1, create a file named Jamroot with the following content in the directory containing hello.cpp , as shown in Example 1-8. Example 1-8. Jamfile for project hello# jamfile for project hello exe hello : hello.cpp ; install dist : hello : <location>. ; Next, change to the directory containing hello.cpp and Jamroot and enter the following command: > bjam hello This command builds the executable hello or hello.exe in a subdirectory of the current directory. Finally, enter the command: > bjam dist This command copies the executable to the directory specified by the location property, which in this case is the current directory.
Discussion
The file
Jamroot
is an example of a
Jamfile
. While a small collection of C++ source files might be managed using a single Jamfile, a large
This hierarchical design is quite powerful: for example, it makes it easy to apply a requirement , such as thread support, to a project and all its descendants.
Each project is a collection of
targets
. Targets are declared by invoking
rules
, such as the
exe
rule and the
install
rule. Most targets
The exe rule is used to declare an executable target. An invocation of this rule has the form shown in Example 1-9. Example 1-9. Invocation of the exe ruleexe target-name : sources : requirements : default-build : usage-requirements ;
Here,
target-name
specifies the name of the executable,
sources
specifies a list of source files and libraries;
requirements
specifies properties that will apply to the target regardless of any additional properties
Properties are specified in the form < feature > value . For example, to declare an executable that will always be built with thread support, you could write:
exe hello
: hello.cpp
: <threading>multi
;
Several common features, and their possible values, are listed in Table 1-15. Table 1-15. Common Boost.Build features
When an executable targetor a target corresponding to a static or dynamic libraryis built, the file corresponding to the target is created in a
For simplicity I asked you to create the Jamfile from Example 1-8 in the same directory as the source file
hello.cpp
. In a real world project, however, you will often want to keep your source and binary files in separate directories. In Example 1-8 you can place the Jamfile
The
install
rule instructs Boost.Build to copy the one or more filesspecified as file
Example 1-10. Invocation of the install ruleinstall target-name : files : requirements : default-build : usage-requirements ;
Here,
target-name
is the name of the target being declared and
files
is a list of one or more files or targets to be
The location where the files are to be copied can be specified either as the target name or as the value of the location property of a target requirement. For example, in Example 1-8 you could have written the install target like so: install . : hello ; You could then install the executable as follows: > bjam . The method used in Example 1-8 is preferable, however, since it's easier to remember a named target than a file pathname.
Finally, let's look
> bjam xxx To build the target xxx with the toolset yyy , enter the command: > bjam xxx toolset= yyy To build the target xxx with version vvv of toolset yyy , enter the command: > bjam xxx toolset= yyy - vvv To build specify a standard library zzz from the command line, use the syntax: > bjam xxx stdlib= zzz You can build several targets at once by entering several target names on the command line, and build all targets in the given project by specifying no target. Consequently, you could have built and installed the executable from Example 1-9 by simply entering: > bjam To remove all the files created during the build process, including the executable, enter: > bjam --clean
See AlsoRecipe 1.2 and Recipe 1.15 |