• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
5<title>Using the predefs</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../predef.html" title="Chapter&#160;29.&#160;Boost.Predef 1.10">
10<link rel="prev" href="../predef.html" title="Chapter&#160;29.&#160;Boost.Predef 1.10">
11<link rel="next" href="adding_new_predefs.html" title="Adding new predefs">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="../predef.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../predef.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><a accesskey="n" href="adding_new_predefs.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="predef.using_the_predefs"></a><a class="link" href="using_the_predefs.html" title="Using the predefs">Using the predefs</a>
29</h2></div></div></div>
30<p>
31      To use the automatically defined predefs one needs to only include the single
32      top-level header:
33    </p>
34<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">predef</span><span class="special">.</span><span class="identifier">h</span><span class="special">&gt;</span>
35</pre>
36<p>
37      This defines <span class="bold"><strong>all</strong></span> the version macros known
38      to the library. For each macro it will be defined to either a <span class="emphasis"><em>zero</em></span>
39      valued expression for when the particular item is not detected, and to a <span class="emphasis"><em>positive</em></span>
40      value if it is detected. The predef macros fall onto five categories each with
41      macros of a particular prefix:
42    </p>
43<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
44<li class="listitem">
45          <code class="computeroutput"><span class="identifier">BOOST_ARCH_</span></code>for system/CPU
46          architecture one is compiling for.
47        </li>
48<li class="listitem">
49          <code class="computeroutput"><span class="identifier">BOOST_COMP_</span></code> for the compiler
50          one is using.
51        </li>
52<li class="listitem">
53          <code class="computeroutput"><span class="identifier">BOOST_LANG_</span></code> for language
54          standards one is compiling against.
55        </li>
56<li class="listitem">
57          <code class="computeroutput"><span class="identifier">BOOST_LIB_C_</span></code> and <code class="computeroutput"><span class="identifier">BOOST_LIB_STD_</span></code> for the C and C++ standard
58          library in use.
59        </li>
60<li class="listitem">
61          <code class="computeroutput"><span class="identifier">BOOST_OS_</span></code> for the operating
62          system we are compiling to.
63        </li>
64<li class="listitem">
65          <code class="computeroutput"><span class="identifier">BOOST_PLAT_</span></code> for platforms
66          on top of operating system or compilers.
67        </li>
68<li class="listitem">
69          <code class="computeroutput"><span class="identifier">BOOST_ENDIAN_</span></code> for endianness
70          of the os and architecture combination.
71        </li>
72<li class="listitem">
73          <code class="computeroutput"><span class="identifier">BOOST_HW_</span></code> for hardware
74          specific features.
75        </li>
76<li class="listitem">
77          <code class="computeroutput"><span class="identifier">BOOST_HW_SIMD</span></code> for SIMD
78          (Single Instruction Multiple Data) detection.
79        </li>
80</ul></div>
81<div class="note"><table border="0" summary="Note">
82<tr>
83<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
84<th align="left">Note</th>
85</tr>
86<tr><td align="left" valign="top"><p>
87        The detected definitions are for the configuration one is targeting during
88        the compile. In particular in a cross-compile this means the target system,
89        and not the host system.
90      </p></td></tr>
91</table></div>
92<p>
93      One uses the individual definitions to compare against specific versions by
94      comparing against the <code class="computeroutput"><span class="identifier">BOOST_VERSION_NUMBER</span></code>
95      macro. For example, to make a choice based on the version of the GCC C++ compiler
96      one would:
97    </p>
98<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">predef</span><span class="special">.</span><span class="identifier">h</span><span class="special">&gt;</span>
99<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
100
101<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
102<span class="special">{</span>
103  <span class="keyword">if</span> <span class="special">(</span><span class="identifier">BOOST_COMP_GNUC</span> <span class="special">&gt;=</span> <span class="identifier">BOOST_VERSION_NUMBER</span><span class="special">(</span><span class="number">4</span><span class="special">,</span><span class="number">0</span><span class="special">,</span><span class="number">0</span><span class="special">))</span>
104    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"GCC compiler is at least version 4.0.0"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
105  <span class="keyword">else</span>
106    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"GCC compiler is at older than version 4.0.0, or not a GCC compiler"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
107  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
108<span class="special">}</span>
109</pre>
110<p>
111      As you might notice above the <code class="computeroutput"><span class="keyword">else</span></code>
112      clause also covers the case where the particular compiler is not detected.
113      But one can make the test also test for the detection. All predef definitions
114      are defined as a zero (0) expression when not detected. Hence one could use
115      the detection with a natural single condition. For example:
116    </p>
117<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">predef</span><span class="special">.</span><span class="identifier">h</span><span class="special">&gt;</span>
118<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
119
120<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
121<span class="special">{</span>
122  <span class="keyword">if</span> <span class="special">(</span><span class="identifier">BOOST_COMP_GNUC</span><span class="special">)</span>
123    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"This is GNU GCC!"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
124  <span class="keyword">else</span>
125    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Not GNU GCC."</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
126  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
127<span class="special">}</span>
128</pre>
129<p>
130      And since the predef's are preprocessor definitions the same is possible from
131      the preprocessor:
132    </p>
133<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">predef</span><span class="special">.</span><span class="identifier">h</span><span class="special">&gt;</span>
134<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
135
136<span class="preprocessor">#if</span> <span class="identifier">BOOST_COMP_GNUC</span>
137  <span class="preprocessor">#if</span> <span class="identifier">BOOST_COMP_GNUC</span> <span class="special">&gt;=</span> <span class="identifier">BOOST_VERSION_NUMBER</span><span class="special">(</span><span class="number">4</span><span class="special">,</span><span class="number">0</span><span class="special">,</span><span class="number">0</span><span class="special">)</span>
138    <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span> <span class="identifier">the_compiler</span> <span class="special">=</span> <span class="string">"GNU GCC, of at least version 4."</span>
139  <span class="preprocessor">#else</span>
140    <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span> <span class="identifier">the_compiler</span> <span class="special">=</span> <span class="string">"GNU GCC, less than version 4."</span>
141  <span class="preprocessor">#endif</span>
142<span class="preprocessor">#else</span>
143  <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span> <span class="identifier">the_compiler</span> <span class="special">=</span> <span class="string">"Not GNU GCC."</span>
144<span class="preprocessor">#endif</span>
145
146<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
147<span class="special">{</span>
148  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">the_compiler</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
149  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
150<span class="special">}</span>
151</pre>
152<p>
153      In addition, for each version macro defined there is an <code class="computeroutput"><span class="special">*</span><span class="identifier">_AVAILABLE</span></code> macro defined only when the particular
154      aspect is detected. I.e. a definition equivalent to:
155    </p>
156<pre class="programlisting"><span class="preprocessor">#if</span> <span class="identifier">BOOST_PREDEF_ABC</span>
157  <span class="preprocessor">#define</span> <span class="identifier">BOOST_PREDEF_ABC_AVAILABLE</span>
158<span class="preprocessor">#endif</span>
159</pre>
160<p>
161      Also for each aspect there is a macro defined with a descriptive name of what
162      the detection is.
163    </p>
164<h4>
165<a name="predef.using_the_predefs.h0"></a>
166      <span class="phrase"><a name="predef.using_the_predefs.the_emulated_macros"></a></span><a class="link" href="using_the_predefs.html#predef.using_the_predefs.the_emulated_macros">The
167      <code class="computeroutput"><span class="special">*</span><span class="identifier">_EMULATED</span></code>
168      macros</a>
169    </h4>
170<p>
171      Predef definitions are guaranteed to be uniquely detected within one category.
172      But there are contexts under which multiple underlying detections are possible.
173      The well known example of this is detection of GCC and MSVC compilers which
174      are commonly emulated by other compilers by defining the same base macros.
175      To account for this detection headers are allowed to define <code class="computeroutput"><span class="special">*</span><span class="identifier">_EMULATED</span></code> predefs when this situation is
176      detected. The emulated predefs will be set to the version number of the detection
177      instead of the regular predef macro for that detection. For example MSVC will
178      set <code class="computeroutput"><span class="identifier">BOOST_COMP_MSVC_EMULATED</span></code>
179      but not set <code class="computeroutput"><span class="identifier">BOOST_COMP_MSVC</span></code>,
180      and it will also set <code class="computeroutput"><span class="identifier">BOOST_COMP_MSVC_AVAILABLE</span></code>.
181    </p>
182<h4>
183<a name="predef.using_the_predefs.h1"></a>
184      <span class="phrase"><a name="predef.using_the_predefs.using_the_boost_version_number_m"></a></span><a class="link" href="using_the_predefs.html#predef.using_the_predefs.using_the_boost_version_number_m">Using the
185      <code class="computeroutput"><span class="identifier">BOOST_VERSION_NUMBER</span></code> macro</a>
186    </h4>
187<p>
188      All the predefs are defined to be a use of the <code class="computeroutput"><span class="identifier">BOOST_VERSION_NUMBER</span></code>
189      macro. The macro takes individual major, minor, and patch value expressions:
190    </p>
191<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_VERSION_NUMBER</span><span class="special">(</span> <span class="identifier">major</span><span class="special">,</span> <span class="identifier">minor</span><span class="special">,</span> <span class="identifier">patch</span> <span class="special">)</span> <span class="special">...</span>
192</pre>
193<p>
194      The arguments are:
195    </p>
196<div class="orderedlist"><ol class="orderedlist" type="1">
197<li class="listitem">
198          Major version number, as a constant value expression in the range [0,99].
199        </li>
200<li class="listitem">
201          Minor version number, as a constant value expression in the range [0,99].
202        </li>
203<li class="listitem">
204          Patch-level version number, as a constant value expression in the range
205          [0,99999].
206        </li>
207</ol></div>
208<p>
209      The ranges for each are "enforced" by the use of a modulo ("%"),
210      i.e. truncation, as opposed to a clamp. And hence this means that the limits
211      are enforced only enough to keep from having out-of-range problems. But not
212      enough to prevent other kinds of problems. Like exceeding the range and getting
213      false detections, or non-detections. It is up to the individual predefs to
214      ensure correct usage beyond the range guarantee.
215    </p>
216<p>
217      The values for the arguments can be any preprocessor valid constant value expression.
218      Only constant value arithmetic is used in the definition of the <code class="computeroutput"><span class="identifier">BOOST_VERSION_NUMBER</span></code> macro and in any of
219      the other predef macros. This means that any allowed base is possible, i.e.
220      binary, octal, decimal, and hexadecimal. For example:
221    </p>
222<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">MY_APPLICATION_VERSION_NUMBER</span> <span class="identifier">BOOST_VERSION_NUMBER</span><span class="special">(</span><span class="number">2</span><span class="special">,</span><span class="number">0xA</span><span class="special">,</span><span class="number">015</span><span class="special">)</span>
223</pre>
224<p>
225      Is equivalent to:
226    </p>
227<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">MY_APPLICATION_VERSION_NUMBER</span> <span class="identifier">BOOST_VERSION_NUMBER</span><span class="special">(</span><span class="number">2</span><span class="special">,</span><span class="number">10</span><span class="special">,</span><span class="number">13</span><span class="special">)</span>
228</pre>
229</div>
230<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
231<td align="left"></td>
232<td align="right"><div class="copyright-footer">Copyright &#169; 2005-2019 Rene Rivera<br>Copyright &#169; 2015 Charly Chevalier<br>Copyright &#169; 2015 Joel Falcou<p>
233        Distributed under the Boost Software License, Version 1.0. (See accompanying
234        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
235      </p>
236</div></td>
237</tr></table>
238<hr>
239<div class="spirit-nav">
240<a accesskey="p" href="../predef.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../predef.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><a accesskey="n" href="adding_new_predefs.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
241</div>
242</body>
243</html>
244