2011-11-30 05:54:23 +00:00
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=US-ASCII" >
< title > Using the predefs</ title >
< link rel = "stylesheet" href = "../boostbook.css" type = "text/css" >
2013-01-05 15:31:59 -06:00
< meta name = "generator" content = "DocBook XSL Stylesheets V1.78.0" >
2011-11-30 05:54:23 +00:00
< link rel = "home" href = "../index.html" title = "Predef 1.0" >
< link rel = "up" href = "../index.html" title = "Predef 1.0" >
< link rel = "prev" href = "introduction.html" title = "Introduction" >
< link rel = "next" href = "adding_new_predefs.html" title = "Adding new predefs" >
</ head >
< body bgcolor = "white" text = "black" link = "#0000FF" vlink = "#840084" alink = "#0000FF" >
< div class = "spirit-nav" >
< a accesskey = "p" href = "introduction.html" >< img src = "../images/prev.png" alt = "Prev" ></ a >< a accesskey = "u" href = "../index.html" >< img src = "../images/up.png" alt = "Up" ></ a >< a accesskey = "h" href = "../index.html" >< img src = "../images/home.png" alt = "Home" ></ a >< a accesskey = "n" href = "adding_new_predefs.html" >< img src = "../images/next.png" alt = "Next" ></ a >
</ div >
2012-02-11 01:47:43 +00:00
< div class = "section predef_using_the_predefs" >
2011-11-30 05:54:23 +00:00
< 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" > < </ 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" > > </ 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 >
2012-02-11 01:47:43 +00:00
value if it is detected. The predef macros fall onto five categories each with
macros of a particular prefix:
2011-11-30 05:54:23 +00:00
</ p >
2013-01-05 15:31:59 -06:00
< div class = "itemizedlist" >< ul class = "itemizedlist" style = "list-style-type: disc; " >
2011-11-30 05:54:23 +00:00
< li class = "listitem" >
< code class = "computeroutput" >< span class = "identifier" > BOOST_ARCHITECTURE_</ span ></ code > for
system/CPU architecture one is compiling for.
</ li >
< li class = "listitem" >
< code class = "computeroutput" >< span class = "identifier" > BOOST_CXX_</ span ></ code > for the compiler
one is using.
</ li >
< li class = "listitem" >
< code class = "computeroutput" >< span class = "identifier" > BOOST_LANGUAGE_</ span ></ code > for language
standards one is compiling against.
</ li >
< li class = "listitem" >
< code class = "computeroutput" >< span class = "identifier" > BOOST_LIBC_</ span ></ code > and < code class = "computeroutput" >< span class = "identifier" > BOOST_LIBSTD_</ 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 >
</ ul ></ div >
< div class = "note" >< table border = "0" summary = "Note" >
< tr >
< td rowspan = "2" align = "center" valign = "top" width = "25" >< img alt = "[Note]" 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" > < </ 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" > > </ span >
< span class = "preprocessor" > #include</ span > < span class = "special" > < </ span >< span class = "identifier" > iostream</ span >< span class = "special" > > </ 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_CXX_GNUC</ span > < span class = "special" > > =</ 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" > << </ span > < span class = "string" > "GCC compiler is at least version 4.0.0"</ span > < span class = "special" > << </ 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" > << </ span > < span class = "string" > "GCC compiler is at older than version 4.0.0, or not a GCC compiler"</ span > < span class = "special" > << </ 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 >
2012-02-11 01:47:43 +00:00
clause also covers the case where the particular compiler is not detected.
2011-11-30 05:54:23 +00:00
But one can make the test specifically 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" > < </ 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" > > </ span >
< span class = "preprocessor" > #include</ span > < span class = "special" > < </ span >< span class = "identifier" > iostream</ span >< span class = "special" > > </ 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_CXX_GNUC</ span >< span class = "special" > )</ span >
< span class = "identifier" > std</ span >< span class = "special" > ::</ span >< span class = "identifier" > cout</ span > < span class = "special" > << </ span > < span class = "string" > "This is GNU GCC!"</ span > < span class = "special" > << </ 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" > << </ span > < span class = "string" > "Not GNU GCC."</ span > < span class = "special" > << </ 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" > < </ 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" > > </ span >
< span class = "preprocessor" > #include</ span > < span class = "special" > < </ span >< span class = "identifier" > iostream</ span >< span class = "special" > > </ span >
< span class = "preprocessor" > #if</ span > < span class = "identifier" > BOOST_CXX_GNUC</ span >
< span class = "preprocessor" > #if</ span > < span class = "identifier" > BOOST_CXX_GNUC</ span > < span class = "special" > > =</ 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" > << </ span > < span class = "identifier" > the_compiler</ span > < span class = "special" > << </ 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 >
< h4 >
< a name = "predef.using_the_predefs.h0" ></ a >
2013-01-05 15:31:59 -06:00
< 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
2011-11-30 05:54:23 +00:00
< 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
2012-02-11 01:47:43 +00:00
enough to prevent other kinds of problems. Like exceeding the range and getting
2011-11-30 05:54:23 +00:00
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 >
2013-01-05 15:31:59 -06:00
< td align = "right" >< div class = "copyright-footer" > Copyright © 2005 Rene Rivera< br > Copyright © 2008-2013 Redshift Software Inc< p >
2011-11-30 05:54:23 +00:00
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 = "introduction.html" >< img src = "../images/prev.png" alt = "Prev" ></ a >< a accesskey = "u" href = "../index.html" >< img src = "../images/up.png" alt = "Up" ></ a >< a accesskey = "h" href = "../index.html" >< img src = "../images/home.png" alt = "Home" ></ a >< a accesskey = "n" href = "adding_new_predefs.html" >< img src = "../images/next.png" alt = "Next" ></ a >
</ div >
</ body >
</ html >