Thursday, 15 May 2014

Setting up Code::Blocks & Boost on Windows

Updated for 2016:

Below is an older post, please see the new YouTube Video, like subscribe & if you benefited the tip jar is just there on the right!

Xel - 5th JUne 2016.

Building boost with Mingw32, installed via CodeBlocks....

First things first, download the installer for CodeBlocks for Windows, I selected the mingw32 compiler, at the time of writing this is:


From the downloads page.

A quick example program can then be thrown together from the IDE, I'm interested in pure C++ using C++11, so I set the compiler to use -std=c++11 switches, as well as fix up the warning settings.

Once we're happy with the test project in the IDE we can go to the same file in a command prompt:

What we've just seen is, adding the compiler installed by codeblocks to the environment path:

PATH=%PATH%;C:\program files\CodeBlocks\MinGW\bin

We then edited the file with note pad:

#include <iostream>

using namespace std;

int main ()
cout << "Hello World" << endl;

Then from the command line we built the program:

mingw32-g++ -std=c++11 -Wall -o main.exe main.cpp

This gave us our application, and we could run "main.exe".

Now we've got all that working, my project uses the C++ boost libraries, so we need to build them with the mingw32 tools we've installed.

So, again we need a command prompt and the same PATH setting we have above:

PATH=%PATH%;C:\program files\CodeBlocks\MinGW\bin

Then from the boost folder we've extracted the boost source to, we need to build the boost build engine:

bootstrap.bat mingw

Once this is complete, we can build the libraries themselves:

b2 toolset=gcc

We now have the libs for linking in the boost sub folder /stage/lib

And we can find the headers in /boost...

We won't cover accessing them through the command line, instead BACK TO CODE BLOCKS!

So, we fire up code blocks, create a new project, its empty, we create our main.cpp and add "Hello World" in there.

We set the compiler to "-Wall" and "-Weff" and "=std=c++11", and check the compile & link worked.

Then we have to add the boost libraries, these are the system library, now we are in debug mode for this project, so we link against the libboost-system library with an added 'd' for debug.  If we switch to release with the drop down at the top, we need to add the link to the library which is not debug!

We also add the libboost-filesystem library, again in debug.

Finally, in the search directories we tell everything were to find boost itself, this allows our #includes to find boost headers.

I keep the paths relative, so I know whether I move my code and boost to "C:\Code" or "C:\Users\Jon\Desktop" the paths will be okay.

Then in the code, we'll include a check for a file on the file system, so include the boost filesystem header.  Then we add a path to "C:\hello.txt", and an exists check on that file...

And we build, now this is the first quirk you'll find, if you just build now the compiler will go off and start to rebuild boost... This is just stupid, so as the text is streaming past, ABORT the build, and then right click on the source file and build from there, you should see nothing needs doing.  Then rebuild again and the project is done almost instantly, this is a quirk of Code::Blocks, as it caches boost into the list of items built.... But we already built boost... 

Follow the video, it'll make sense.

#include <iostream>
#include <string>

#include <boost/filesystem.hpp>

using namespace std;

int main ()
    cout << "HEllo World" << endl;

    cout << "Testing if \"C:\\Hello.txt\" exists" << endl;

    boost::filesystem::path l_path("c:\\Hello.txt");
    if ( boost::filesystem::exists(l_path) )

        cout << "File does exist!" << endl;

        cout << "File missing" << endl;


From the command line, we would have to add "-l libboost_system-mgw47-mt-d-1_55.a" and "-l libboost_filesystem-mgw47-mt-d-1-55.a", to link against boost.

The boost libraries we have are of course the dynamic libraries, if you wish to contain the libraries into the application you're distributing rather than having to hand out the libraries you've also built, you need to build the application static and rebuild boost with the static flags, which is a topic for another day.


  1. Extremely useful. Thank you!

  2. worked perfectly. thanks for the post