Sunday, May 18, 2008

Building Boost C++ lib for MINGW and coding in Code::Blocks

boost + splash_new blank

Boost C++ Libraries is one of the most highly regarded and expertly designed C++ library projects in the world. It provides many efficient algorithms to help App developers to improve their program. However, the Boost's official website offers only source files and Build Tools ( Boost JAM ), we must build the suitable Lib according to the compiler ( like GNU GCC Compiler, Borland C++ Compiler or VC++ Compiler ). Althought, there're many pre-builded destro providered by some unofficial website, most of them are for VC++ Compiler.

Here's a tutorial for building Boost C++ Lib for MINGW GCC Compiler.

  • First of all, Download Boost from the Boost website. You will need the actual Boost package (source) and the Boost Jam package (binary NTx86). Unpack the first package to any location on you harddisk and unpack the file "bjam.exe'' from the second package to a directory contained in your search path (e.g. "C:\MinGW\bin'' or "C:\Program Files\CodeBlocks\MinGW\bin").
  • Then open a shell (cmd.exe) and change into the directory you have unpacked the Boost package to. Type the following command:
    C:\....\boost_1_35_0\>bjam --toolset=gcc --prefix=C:\Boost install
  • While building, it'll take 30 mins up to couple hours depends on your machine. So be patiant.
  • Then Boost C++ Lib will be installed in "C:\Boost\include\boost-version"

After that. we can use Boost to code for MINGW GCC Compiler in Code::Blocks IDE. Before coding, add the Boost "include" and "lib" into the compiler search dictionary.

  • Open the Code::Blocks IDE, then click on "settings""compiler and debugger...", change to "search dictionary" page and add "C:\Boost\include\boost-1_35" into "compiler" page.sshot-1
  • Then add "C:\Boost\lib" into  "Linker" page.

Creat a new Console Application project for C++,  click "Project" → "Build options...". Add the lib you need in your project (e.g. C:\Boost\lib\boost_signals-mgw34-mt-1_35.lib)

sshot-1

Then code following code in main.cpp

// Boost.Signals library

// Copyright Douglas Gregor 2001-2003. Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// For more information, see http://www.boost.org

#include
#include
#include
#include

struct HelloWorld
{
void operator()() const
{
std::cout << "Hello, World!" << std::endl;
}
};

int
test_main(int, char* [])
{
// Signal with no arguments and a void return value
boost::signal sig;

// Connect a HelloWorld slot
HelloWorld hello;
sig.connect(hello);

// Call all of the slots
//sig();
return 0;
}


Compilation will be completed.

If you have any question, it's great to post in comment :-)


3 comments:

Anonymous said...

Thanks man, I spent hours trying to do it myself.

史丹力 said...

if there's anything wrong in my post. please let me know... thanks

Anonymous said...

thanks for this it worked for me until i tried to run a program then i get boost\lib permission denied error