tags:

views:

506

answers:

9

I am trying to figure out what version of Boost my code thinks it's using. I want to do something like this:

#error BOOST_VERSION

but the preprocessor does not expand BOOST_VERSION.

I know I could print it out at run-time from the program, and I know I could look at the output of the preprocessor to find the answer. I feel like having a way of doing this during compilation could be useful.

A: 

You could write a program that prints out BOOST_VERSION and compile and run it as part of your build system. Otherwise, I think you're out of luck.

Chris Lutz
For the case of a software version defined in a header you're probably safe (and it's a good answer). But as a general solution, a possible downside would be in getting your test app and your real app to have the same value of the #define - depending on their include paths, other #defines that may be used to set the value of that one, the CFLAGS passed to the compiler, etc.
KeyserSoze
Print it out from your real program. If graphical, put it in the "about" dialog. If command-line, make it an option (part of --version, maybe). If a daemon, write it to a log file. If embedded, find some other way.
swillden
@swillden - The OP wanted it at compile time, not at runtime.
Chris Lutz
+7  A: 

As far as I know '#error' only will print strings, in fact you don't even need to use quotes.

Have you tried writing various purposefully incorrect code using "BOOST_VERSION"? Perhaps something like "blah[BOOST_VERSION] = foo;" will tell you something like "string literal 1.2.1 cannot be used as an array address". It won't be a pretty error message, but at least it'll show you the relevant value. You can play around until you find a compile error that does tell you the value.

KeyserSoze
That's good problem solving. +1
Charlie Salts
That didn't work, since BOOST_VERSION is an integer, but I got to see it with this statement: `std::vector<BOOST_VERSION>;` in gcc 4.4.1.Thanks!
Jim Hunziker
Note that with Visual C++, you would have to use Bojan Resnik's answer.
RaphaelSP
I tried to get this to work, but the error message GCC gave me were sadly undescriptive. But +1 for mentioning it.
Chris Lutz
A: 

Are you looking for

#if BOOST_VERSION != "1.2"
#error "Bad version"
#endif

Not great if BOOST_VERSION is a string, like I've assumed, but there may also be individual integers defined for the major, minor and revision numbers.

I think the submitter doesn't want to (just) enforce a particular value, they want to see what the current value is.
KeyserSoze
+2  A: 

You could also preprocess the source file and see what the preprocessor value evaluates to.

fbrereto
+1  A: 

Looking at the output of the preprocessor is the closest thing to the answer you ask for.

I know you've excluded that (and other ways), but I'm not sure why. You have a specific enough problem to solve, but you have not explained why any of the "normal" methods don't work well for you.

dwc
+1  A: 

BOOST_VERSION is defined in the boost header file version.hpp.

David Harris
+6  A: 

If you are using Visual C++, you can use #pragma message:

#include <boost/preprocessor/stringize.hpp>
#pragma message("BOOST_VERSION=" BOOST_PP_STRINGIZE(BOOST_VERSION))

Edit: Thanks to LB for link

Apparently, the GCC equivalent is (not tested):

#pragma message "BOOST_VERSION=" BOOST_PP_STRINGIZE(BOOST_VERSION)
Bojan Resnik
This looks like a nice feature, I'd love to see it in GCC :)
Paggas
That's called diagnostic pragmas, http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas
LB
A: 

Take a look at the Boost documentation as well, regarding how you are using the macro:

In reference to BOOST_VERSION, from http://www.boost.org/doc/libs/1_37_0/libs/config/doc/html/boost_config/boost_macro_reference.html#boost_config.boost_macro_reference.boost_helper_macros:

Describes the boost version number in XXYYZZ format such that: (BOOST_VERSION % 100) is the sub-minor version, ((BOOST_VERSION / 100) % 1000) is the minor version, and (BOOST_VERSION / 100000) is the major version.

bn
A: 

And I was thinking that I know C very well...

Arabcoder