summaryrefslogtreecommitdiff
path: root/doc/html/predef/using_the_predefs.html
blob: 625e139c0db18a3d1b62f6163ff364cdf8d1a688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Using the predefs</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../predef.html" title="Chapter&#160;28.&#160;Boost.Predef 1.6">
<link rel="prev" href="../predef.html" title="Chapter&#160;28.&#160;Boost.Predef 1.6">
<link rel="next" href="adding_new_predefs.html" title="Adding new predefs">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="predef.using_the_predefs"></a><a class="link" href="using_the_predefs.html" title="Using the predefs">Using the predefs</a>
</h2></div></div></div>
<p>
      To use the automatically defined predefs one needs to only include the single
      top-level header:
    </p>
<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>
</pre>
<p>
      This defines <span class="bold"><strong>all</strong></span> the version macros known
      to the library. For each macro it will be defined to either a <span class="emphasis"><em>zero</em></span>
      valued expression for when the particular item is not detected, and to a <span class="emphasis"><em>positive</em></span>
      value if it is detected. The predef macros fall onto five categories each with
      macros of a particular prefix:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          <code class="computeroutput"><span class="identifier">BOOST_ARCH_</span></code>for system/CPU
          architecture one is compiling for.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">BOOST_COMP_</span></code> for the compiler
          one is using.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">BOOST_LANG_</span></code> for language
          standards one is compiling against.
        </li>
<li class="listitem">
          <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
          library in use.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">BOOST_OS_</span></code> for the operating
          system we are compiling to.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">BOOST_PLAT_</span></code> for platforms
          on top of operating system or compilers.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">BOOST_ENDIAN_</span></code> for endianness
          of the os and architecture combination.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">BOOST_HW_</span></code> for hardware
          specific features.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">BOOST_HW_SIMD</span></code> for SIMD
          (Single Instruction Multiple Data) detection.
        </li>
</ul></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        The detected definitions are for the configuration one is targeting during
        the compile. In particular in a cross-compile this means the target system,
        and not the host system.
      </p></td></tr>
</table></div>
<p>
      One uses the individual definitions to compare against specific versions by
      comparing against the <code class="computeroutput"><span class="identifier">BOOST_VERSION_NUMBER</span></code>
      macro. For example, to make a choice based on the version of the GCC C++ compiler
      one would:
    </p>
<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>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
  <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>
    <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>
  <span class="keyword">else</span>
    <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>
  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      As you might notice above the <code class="computeroutput"><span class="keyword">else</span></code>
      clause also covers the case where the particular compiler is not detected.
      But one can make the test also test for the detection. All predef definitions
      are defined as a zero (0) expression when not detected. Hence one could use
      the detection with a natural single condition. For example:
    </p>
<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>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
  <span class="keyword">if</span> <span class="special">(</span><span class="identifier">BOOST_COMP_GNUC</span><span class="special">)</span>
    <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>
  <span class="keyword">else</span>
    <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>
  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      And since the predef's are preprocessor definitions the same is possible from
      the preprocessor:
    </p>
<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>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>

<span class="preprocessor">#if</span> <span class="identifier">BOOST_COMP_GNUC</span>
  <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>
    <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>
  <span class="preprocessor">#else</span>
    <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>
  <span class="preprocessor">#endif</span>
<span class="preprocessor">#else</span>
  <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>
<span class="preprocessor">#endif</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
  <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>
  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      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
      aspect is detected. I.e. a definition equivalent to:
    </p>
<pre class="programlisting"><span class="preprocessor">#if</span> <span class="identifier">BOOST_PREDEF_ABC</span>
  <span class="preprocessor">#define</span> <span class="identifier">BOOST_PREDEF_ABC_AVAILABLE</span>
<span class="preprocessor">#endif</span>
</pre>
<p>
      Also for each aspect there is a macro defined with a descriptive name of what
      the detection is.
    </p>
<h4>
<a name="predef.using_the_predefs.h0"></a>
      <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
      <code class="computeroutput"><span class="special">*</span><span class="identifier">_EMULATED</span></code>
      macros</a>
    </h4>
<p>
      Predef definitions are guaranteed to be uniquely detected within one category.
      But there are contexts under which multiple underlying detections are possible.
      The well known example of this is detection of GCC and MSVC compilers which
      are commonly emulated by other compilers by defining the same base macros.
      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
      detected. The emulated predefs will be set to the version number of the detection
      instead of the regular predef macro for that detection. For example MSVC will
      set <code class="computeroutput"><span class="identifier">BOOST_COMP_MSVC_EMULATED</span></code>
      but not set <code class="computeroutput"><span class="identifier">BOOST_COM_MSVC</span></code>,
      and it will also set <code class="computeroutput"><span class="identifier">BOOST_COMP_MSVC_AVAILABLE</span></code>.
    </p>
<h4>
<a name="predef.using_the_predefs.h1"></a>
      <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
      <code class="computeroutput"><span class="identifier">BOOST_VERSION_NUMBER</span></code> macro</a>
    </h4>
<p>
      All the predefs are defined to be a use of the <code class="computeroutput"><span class="identifier">BOOST_VERSION_NUMBER</span></code>
      macro. The macro takes individual major, minor, and patch value expressions:
    </p>
<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>
</pre>
<p>
      The arguments are:
    </p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
          Major version number, as a constant value expression in the range [0,99].
        </li>
<li class="listitem">
          Minor version number, as a constant value expression in the range [0,99].
        </li>
<li class="listitem">
          Patch-level version number, as a constant value expression in the range
          [0,99999].
        </li>
</ol></div>
<p>
      The ranges for each are "enforced" by the use of a modulo ("%"),
      i.e. truncation, as opposed to a clamp. And hence this means that the limits
      are enforced only enough to keep from having out-of-range problems. But not
      enough to prevent other kinds of problems. Like exceeding the range and getting
      false detections, or non-detections. It is up to the individual predefs to
      ensure correct usage beyond the range guarantee.
    </p>
<p>
      The values for the arguments can be any preprocessor valid constant value expression.
      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
      the other predef macros. This means that any allowed base is possible, i.e.
      binary, octal, decimal, and hexadecimal. For example:
    </p>
<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>
</pre>
<p>
      Is equivalent to:
    </p>
<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>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2005-2016 Rene Rivera<br>Copyright &#169; 2015 Charly Chevalier<br>Copyright &#169; 2015 Joel Falcou<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        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>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
</body>
</html>