Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

Monday, March 31, 2008

Building Boost 1.35.0

Seems I always have troubles building Boost.

To start I followed the big 'Get Boost' button on the right side of boost.org; thinking, 'That sure is obvious'. That link takes you to a download page with boost version 1.35.0 right at the top. It shows the new libs with 'Detail' and 'Download'. I took a few seconds to read the new libs section and clicked on download. This took me to sourceforge.net where they have the source code only release. Well, I knew that Boost Consulting has maintained an installer for Windows assuming you are using boost with Visual Studio. So I jumped over to their boost installer site, only to find that they don't have one yet for 1.35.0.

No problem, I'll just go back and grab the source code from sourceforge.net and follow the instructions in the getting started document. I kicked off the download and started looking for the getting started document. After a while, I figure I must be crazy, because I can't find it. I finally do a google search in order to find it. Still thinking I must have just missed it, I went back and followed every link on the right of the the main boost site. It is not under 'Introduction' or any other of the sections. I finally did locate it, it is on the main page on the left under 'GETTING STARTED'. Funny how you can look right past something like that.

So now I figure all I have to do is follow the getting started guide to build boost.

Step 1. Get Boost. I already did that. My download had already completed from sourceforge.net. I unziped it (actually un-7zed it) to c:\boost\boost_1_35_0\.

Step 2. Explain Boost Distribution. Ok, nothing new here.

Step 3. Header only libs. Right, header only libs are nice. Just compile and go. However, Boost.Thread among others need to be compiled.

Step 4. Build with a header only lib. Been there done that many times. There are sub-steps for building with Visual Studio. I always use MPC, so this is not useful to me. I plan to create a separate post or two about building boost applications with MPC. When you see how easy it is to use MPC with Boost, I doubt that you will use anything else.

Step 5. Building Boost libraries. Alright, here we go.

Step 5.1. Install Boost libraries. Yeah, thats what I wanted to do to start with. However, Boost Consulting has not yet put out an installer for 1.35.0.

Step 5.2. Build from source.

Step 5.2.1. Get bjam. I put the executable in c:\boost\boost_1_35_0\.

Step 5.2.2. Identify toolset. I want: msvc.

Step 5.2.3. Select a build directory. It says this is not necessary.

Step 5.2.4. Invoke bjam. This is where I got in trouble. I tried various combinations of commands/arguments to bjam. I must have tried to build 3-4 times. Finally I settled on the following after looking at bjam --help.
cd c:\boost\boost_1_35_0
bjam --build-type=complete --prefix="c:\boost\boost_1_35_0" install
Seems like I should have included --toolset=msvc, but I forgot and it picked msvc-8.0 anyway.

This worked for me creating a c:\boost\boost_1_35_0\lib directory as the rest of the getting started document assumes. Actually the getting started doc has it under C:\Program Files\boost\boost_1_35_0\lib, but I like c:\boost\boost_1_35_0\lib better.

After the build you can delete the c:\boost\boost_1_35_0\bin.v2 directory to free up space.

Friday, February 29, 2008

std::min and std::max

Today I typed the following:
int t = (std::max)(timeout, lagtime);

Why did I put parentheses around std::max? Because windows.h defines (among other things) a max and a min macro. If you include windows.h the above code will not compile. For example the following:
#include "windows.h"
#include <algorithm>

void foo() {
int i = 5;
int j = 7;
int x = std::max(i,j);
}

Will produce the following error with Visual Studio C++ 2005:
1>test.cpp(7) : error C2589: '(' : illegal token on right side of '::'
1>test.cpp(7) : error C2143: syntax error : missing ';' before '::'

There are a number of ways to work around windows.h defining these two macros.

  • Use alternative names defined in windows.h.
    int x = _cpp_max(i,j);
    int y = _cpp_min(i,j);
    This is not portable; only works on Windows.

  • Define NOMINMAX before including windows.h. This might break existing code that assumes NOMINMAX is not defined.

  • Don't use std::min and std::max. Instead use the tertiary operator like so:
    int x = i > j ? i : j; // max(i,j)
    int y = i < j ? i : j; // min(i,j)
    This is portable but not as readable and more error prone.

  • Use using statements to make the code portable:
    using std::min;
    using std::max;
    int x = max(i,j);
    int y = min(i,j);
    This works but requires two more lines of code. You could also just use 'using namespace std;' but that might pull in more than you want.

  • Use std::min<int> and std::max<int>
    int x = std::max<int>(i,j);
    int y = std::min<int>(i,j);
    This requires you to specify the type. However in some cases this actually helps. For example:
    int i = 5;
    unsigned int j = 7;
    int x = (std::max)(i,j);
    int y = (std::min)(i,j);
    Note the 'unsigned'. Generates the following errors:
    1>test.cpp(7) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : 
    expects 3 arguments - 2 provided
    1> c:\program files\microsoft visual studio 8\vc\include\xutility(3190) :
    see declaration of 'std::max'
    1>test.cpp(7) : error C2782: 'const _Ty &std::max(const _Ty &,const _Ty &)' :
    template parameter '_Ty' is ambiguous
    1> c:\program files\microsoft visual studio 8\vc\include\xutility(3182) :
    see declaration of 'std::max'
    1> could be 'unsigned int'
    1> or 'int'
    By explicitly specifying type via <int> you remove the ambiguity.

  • Use (std::min) and (std::max)
    int i = 5;
    int j = 7;
    int x = (std::max)(i,j);
    int y = (std::min)(i,j);
    This works (as does the std::max<int>) because the C++ preprocessor requires '(' as the next preprocessing token following the macro name to preform the macro expansion.