3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Frequently Asked Questions</title>
5 <link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
8 <link rel="up" href="../bbv2.html" title="Chapter 39. Boost.Build V2 User Manual">
9 <link rel="prev" href="extender.html" title="Extender Manual">
11 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
12 <table cellpadding="2" width="100%"><tr>
13 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
14 <td align="center"><a href="../../../index.html">Home</a></td>
15 <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
16 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
17 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
18 <td align="center"><a href="../../../more/index.htm">More</a></td>
21 <div class="spirit-nav">
22 <a accesskey="p" href="extender.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../bbv2.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a>
25 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
26 <a name="bbv2.faq"></a>Frequently Asked Questions</h2></div></div></div>
28 <dt><span class="section"><a href="faq.html#id3908656">
29 How do I get the current value of feature in Jamfile?
31 <dt><span class="section"><a href="faq.html#id3908718">
32 I am getting a "Duplicate name of actual target" error. What does that
35 <dt><span class="section"><a href="faq.html#bbv2.faq.envar">
36 Accessing environment variables
38 <dt><span class="section"><a href="faq.html#id3908985">
39 How to control properties order?
41 <dt><span class="section"><a href="faq.html#id3909040">
42 How to control the library linking order on Unix?
44 <dt><span class="section"><a href="faq.html#bbv2.faq.external">
45 Can I get capture external program output using a Boost.Jam variable?
47 <dt><span class="section"><a href="faq.html#id3909184">
48 How to get the project root (a.k.a. Jamroot) location?
50 <dt><span class="section"><a href="faq.html#id3909210">
51 How to change compilation flags for one file?
53 <dt><span class="section"><a href="faq.html#bbv2.faq.dll-path">
54 Why are the <code class="literal">dll-path</code> and <code class="literal">hardcode-dll-paths
55 </code> properties useful?
57 <dt><span class="section"><a href="faq.html#bbv2.recipies.site-config">Targets in site-config.jam</a></span></dt>
58 <dt><span class="section"><a href="faq.html#bbv2.faq.header-only-libraries">Header-only libraries</a></span></dt>
61 <div class="titlepage"><div><div><h3 class="title">
62 <a name="id3908656"></a>
63 How do I get the current value of feature in Jamfile?
64 </h3></div></div></div>
66 This is not possible, since Jamfile does not have "current" value of any
67 feature, be it toolset, build variant or anything else. For a single
68 invocation of <code class="filename">bjam</code>, any given main target can be
69 built with several property sets. For example, user can request two build
70 variants on the command line. Or one library is built as shared when used
71 from one application, and as static when used from another. Each Jamfile
72 is read only once so generally there is no single value of a feature you
73 can access in Jamfile.
76 A feature has a specific value only when building a target, and there are
77 two ways you can use that value:
79 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
81 Use conditional requirements or indirect conditional requirements. See
82 <a class="xref" href="overview.html#bbv2.overview.targets.requirements.conditional">the section called “Requirements”</a>.
85 Define a custom generator and a custom main target type. The custom
86 generator can do arbitrary processing or properties. See the <a class="xref" href="extender.html" title="Extender Manual">the section called “Extender Manual”</a>.
91 <div class="titlepage"><div><div><h3 class="title">
92 <a name="id3908718"></a>
93 I am getting a "Duplicate name of actual target" error. What does that
95 </h3></div></div></div>
97 The most likely case is that you are trying to compile the same file
98 twice, with almost the same, but differing properties. For example:
100 <pre class="programlisting">
101 exe a : a.cpp : <include>/usr/local/include ;
107 The above snippet requires two different compilations of
108 <code class="computeroutput">a.cpp</code>, which differ only in their <code class="literal">include</code>
109 property. Since the <code class="literal">include</code> feature is declared as
110 <code class="literal">free</code> Boost.Build does not create a separate build
111 directory for each of its values and those two builds would both produce
112 object files generated in the same build directory. Ignoring this and
113 compiling the file only once would be dangerous as different includes
114 could potentially cause completely different code to be compiled.
117 To solve this issue, you need to decide if the file should be compiled
120 <div class="orderedlist"><ol class="orderedlist" type="1">
121 <li class="listitem">
123 To compile the file only once, make sure that properties are the same
124 for both target requests:
126 <pre class="programlisting">
127 exe a : a.cpp : <include>/usr/local/include ;
128 exe b : a.cpp : <include>/usr/local/include ;
133 <pre class="programlisting">
134 alias a-with-include : a.cpp : <include>/usr/local/include ;
135 exe a : a-with-include ;
136 exe b : a-with-include ;
139 or if you want the <code class="literal">includes</code> property not to affect
140 how any other sources added for the built <code class="computeroutput">a</code> and
141 <code class="computeroutput">b</code> executables would be compiled:
143 <pre class="programlisting">
144 obj a-obj : a.cpp : <include>/usr/local/include ;
151 Note that in both of these cases the <code class="literal">include</code>
152 property will be applied only for building these object files and not
153 any other sources that might be added for targets <code class="computeroutput">a</code> and
154 <code class="computeroutput">b</code>.
157 <li class="listitem">
159 To compile the file twice, you can tell Boost.Build to compile it to
160 two separate object files like so:
162 <pre class="programlisting">
163 obj a_obj : a.cpp : <include>/usr/local/include ;
169 or you can make the object file targets local to the main target:
171 <pre class="programlisting">
172 exe a : [ obj a_obj : a.cpp : <include>/usr/local/include ] ;
173 exe b : [ obj a_obj : a.cpp ] ;
176 which will cause Boost.Build to actually change the generated object
177 file names a bit for you and thus avoid any conflicts.
180 Note that in both of these cases the <code class="literal">include</code>
181 property will be applied only for building these object files and not
182 any other sources that might be added for targets <code class="computeroutput">a</code> and
183 <code class="computeroutput">b</code>.
188 A good question is why Boost.Build can not use some of the above
189 approaches automatically. The problem is that such magic would only help
190 in half of the cases, while in the other half it would be silently doing
191 the wrong thing. It is simpler and safer to ask the user to clarify his
192 intention in such cases.
195 <div class="section">
196 <div class="titlepage"><div><div><h3 class="title">
197 <a name="bbv2.faq.envar"></a>
198 Accessing environment variables
199 </h3></div></div></div>
201 Many users would like to use environment variables in Jamfiles, for
202 example, to control the location of external libraries. In many cases it
203 is better to declare those external libraries in the site-config.jam file,
204 as documented in the <a class="link" href="faq.html#bbv2.recipies.site-config" title="Targets in site-config.jam">recipes
205 section</a>. However, if the users already have the environment
206 variables set up, it may not be convenient for them to set up their
207 site-config.jam files as well and using the environment variables might be
211 Boost.Jam automatically imports all environment variables into its
212 built-in .ENVIRON module so user can read them from there directly or by
213 using the helper os.environ rule. For example:
215 <pre class="programlisting">
217 local unga-unga = [ os.environ UNGA_UNGA ] ;
221 or a bit more realistic:
223 <pre class="programlisting">
225 local SOME_LIBRARY_PATH = [ os.environ SOME_LIBRARY_PATH ] ;
226 exe a : a.cpp : <include>$(SOME_LIBRARY_PATH) ;
231 <div class="section">
232 <div class="titlepage"><div><div><h3 class="title">
233 <a name="id3908985"></a>
234 How to control properties order?
235 </h3></div></div></div>
237 For internal reasons, Boost.Build sorts all the properties alphabetically.
238 This means that if you write:
240 <pre class="programlisting">
241 exe a : a.cpp : <include>b <include>a ;
244 then the command line with first mention the <code class="computeroutput">a</code> include
245 directory, and then <code class="computeroutput">b</code>, even though they are specified in the
246 opposite order. In most cases, the user does not care. But sometimes the
247 order of includes, or other properties, is important. For such cases, a
248 special syntax is provided:
250 <pre class="programlisting">
251 exe a : a.cpp : <include>a&&b ;
256 The <code class="computeroutput">&&</code> symbols separate property values and specify
257 that their order should be preserved. You are advised to use this feature
258 only when the order of properties really matters and not as a convenient
259 shortcut. Using it everywhere might negatively affect performance.
262 <div class="section">
263 <div class="titlepage"><div><div><h3 class="title">
264 <a name="id3909040"></a>
265 How to control the library linking order on Unix?
266 </h3></div></div></div>
268 On Unix-like operating systems, the order in which static libraries are
269 specified when invoking the linker is important, because by default, the
270 linker uses one pass though the libraries list. Passing the libraries in
271 the incorrect order will lead to a link error. Further, this behaviour is
272 often used to make one library override symbols from another. So,
273 sometimes it is necessary to force specific library linking order.
276 Boost.Build tries to automatically compute the right order. The primary
277 rule is that if library <code class="computeroutput">a</code> "uses" library <code class="computeroutput">b</code>, then
278 library <code class="computeroutput">a</code> will appear on the command line before library
279 <code class="computeroutput">b</code>. Library <code class="computeroutput">a</code> is considered to use <code class="computeroutput">b</code>
280 if <code class="computeroutput">b</code> is present either in the <code class="computeroutput">a</code> library's
281 sources or its usage is listed in its requirements. To explicitly specify
282 the <code class="literal">use</code> relationship one can use the
283 <code class="literal"><use></code> feature. For example, both of the following
284 lines will cause <code class="computeroutput">a</code> to appear before <code class="computeroutput">b</code> on the
287 <pre class="programlisting">
289 lib a : a.cpp : <use>b ;
294 The same approach works for searched libraries as well:
296 <pre class="programlisting">
298 lib png : : <use>z ;
299 exe viewer : viewer png z ;
304 <div class="section">
305 <div class="titlepage"><div><div><h3 class="title">
306 <a name="bbv2.faq.external"></a>
307 Can I get capture external program output using a Boost.Jam variable?
308 </h3></div></div></div>
310 The <code class="literal">SHELL</code> builtin rule may be used for this purpose:
312 <pre class="programlisting">
313 local gtk_includes = [ SHELL "gtk-config --cflags" ] ;
318 <div class="section">
319 <div class="titlepage"><div><div><h3 class="title">
320 <a name="id3909184"></a>
321 How to get the project root (a.k.a. Jamroot) location?
322 </h3></div></div></div>
324 You might want to use your project's root location in your Jamfiles. To
325 access it just declare a path constant in your Jamroot.jam file using:
327 <pre class="programlisting">
328 path-constant TOP : . ;
331 After that, the <code class="computeroutput">TOP</code> variable can be used in every Jamfile.
334 <div class="section">
335 <div class="titlepage"><div><div><h3 class="title">
336 <a name="id3909210"></a>
337 How to change compilation flags for one file?
338 </h3></div></div></div>
340 If one file must be compiled with special options, you need to explicitly
341 declare an <code class="computeroutput">obj</code> target for that file and then use that target
342 in your <code class="computeroutput">exe</code> or <code class="computeroutput">lib</code> target:
344 <pre class="programlisting">
346 obj b : b.cpp : <optimization>off ;
349 Of course you can use other properties, for example to specify specific
350 C/C++ compiler options:
352 <pre class="programlisting">
354 obj b : b.cpp : <cflags>-g ;
357 You can also use <a class="link" href="tutorial.html#bbv2.tutorial.conditions" title="Conditions and alternatives">conditional
358 properties</a> for finer control:
360 <pre class="programlisting">
362 obj b : b.cpp : <variant>release:<optimization>off ;
367 <div class="section">
368 <div class="titlepage"><div><div><h3 class="title">
369 <a name="bbv2.faq.dll-path"></a>
370 Why are the <code class="literal">dll-path</code> and <code class="literal">hardcode-dll-paths
371 </code> properties useful?
372 </h3></div></div></div>
373 <div class="note"><table border="0" summary="Note">
375 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
376 <th align="left">Note</th>
378 <tr><td align="left" valign="top"><p>
379 This entry is specific to Unix systems.
383 Before answering the questions, let us recall a few points about shared
384 libraries. Shared libraries can be used by several applications, or other
385 libraries, without physically including the library in the application
386 which can greatly decrease the total application size. It is also possible
387 to upgrade a shared library when the application is already installed.
390 However, in order for application depending on shared libraries to be
391 started the OS may need to find the shared library when the application is
392 started. The dynamic linker will search in a system-defined list of paths,
393 load the library and resolve the symbols. Which means that you should
394 either change the system-defined list, given by the <code class="envar">LD_LIBRARY_PATH
395 </code> environment variable, or install the libraries to a system
396 location. This can be inconvenient when developing, since the libraries
397 are not yet ready to be installed, and cluttering system paths may be
398 undesirable. Luckily, on Unix there is another way.
401 An executable can include a list of additional library paths, which will
402 be searched before system paths. This is excellent for development because
403 the build system knows the paths to all libraries and can include them in
404 the executables. That is done when the <code class="literal">hardcode-dll-paths
405 </code> feature has the <code class="literal">true</code> value, which is the
406 default. When the executables should be installed, the story is different.
409 Obviously, installed executable should not contain hardcoded paths to your
410 development tree. (The <code class="literal">install</code> rule explicitly disables the
411 <code class="literal">hardcode-dll-paths</code> feature for that reason.) However,
412 you can use the <code class="literal">dll-path</code> feature to add explicit paths
413 manually. For example:
415 <pre class="programlisting">
416 install installed : application : <dll-path>/usr/lib/snake
417 <location>/usr/bin ;
420 will allow the application to find libraries placed in the <code class="filename">
421 /usr/lib/snake</code> directory.
424 If you install libraries to a nonstandard location and add an explicit
425 path, you get more control over libraries which will be used. A library of
426 the same name in a system location will not be inadvertently used. If you
427 install libraries to a system location and do not add any paths, the
428 system administrator will have more control. Each library can be
429 individually upgraded, and all applications will use the new library.
432 Which approach is best depends on your situation. If the libraries are
433 relatively standalone and can be used by third party applications, they
434 should be installed in the system location. If you have lots of libraries
435 which can be used only by your application, it makes sense to install them
436 to a nonstandard directory and add an explicit path, like the example
437 above shows. Please also note that guidelines for different systems differ
438 in this respect. For example, the Debian GNU guidelines prohibit any
439 additional search paths while Solaris guidelines suggest that they should
443 <div class="section">
444 <div class="titlepage"><div><div><h3 class="title">
445 <a name="bbv2.recipies.site-config"></a>Targets in site-config.jam</h3></div></div></div>
447 It is desirable to declare standard libraries available on a given system.
448 Putting target declaration in a specific project's Jamfile is not really
449 good, since locations of the libraries can vary between different
450 development machines and then such declarations would need to be
451 duplicated in different projects. The solution is to declare the targets
452 in Boost.Build's <code class="filename">site-config.jam</code> configuration file:
454 <pre class="programlisting">
455 project site-config ;
456 lib zlib : : <name>z ;
461 Recall that both <code class="filename">site-config.jam</code> and
462 <code class="filename">user-config.jam</code> are projects, and everything you can
463 do in a Jamfile you can do in those files as well. So, you declare a
464 project id and a target. Now, one can write:
466 <pre class="programlisting">
467 exe hello : hello.cpp /site-config//zlib ;
473 <div class="section">
474 <div class="titlepage"><div><div><h3 class="title">
475 <a name="bbv2.faq.header-only-libraries"></a>Header-only libraries</h3></div></div></div>
477 In modern C++, libraries often consist of just header files, without any
478 source files to compile. To use such libraries, you need to add proper
479 includes and possibly defines to your project. But with a large number of
480 external libraries it becomes problematic to remember which libraries are
481 header only, and which ones you have to link to. However, with Boost.Build
482 a header-only library can be declared as Boost.Build target and all
483 dependents can use such library without having to remeber whether it is a
484 header-only library or not.
487 Header-only libraries may be declared using the <code class="computeroutput">alias</code> rule,
488 specifying their include path as a part of its usage requirements, for
491 <pre class="programlisting">
494 : # no build requirements
496 : <include>whatever ;
499 The includes specified in usage requirements of <code class="computeroutput">my-lib</code> are
500 automatically added to all of its dependants' build properties. The
501 dependants need not care if <code class="computeroutput">my-lib</code> is a header-only or not,
502 and it is possible to later make <code class="computeroutput">my-lib</code> into a regular
503 compiled library without having to that its dependants' declarations.
506 If you already have proper usage requirements declared for a project where
507 a header-only library is defined, you do not need to duplicate them for
508 the <code class="computeroutput">alias</code> target:
510 <pre class="programlisting">
511 project my : usage-requirements <include>whatever ;
518 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
519 <td align="left"></td>
520 <td align="right"><div class="copyright-footer">Copyright © 2006-2009 Vladimir Prus<p>Distributed under the Boost Software License, Version 1.0.
521 (See accompanying file <code class="filename">LICENSE_1_0.txt</code> or copy at
522 <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
527 <div class="spirit-nav">
528 <a accesskey="p" href="extender.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../bbv2.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a>