Using Boost C++ libraries with gcc / g++ under Windows

Stefan - 9. Mai 2008

A lot of the power of C++ comes from the STL, the C++ Standard Library. And even more power is available using the Boost libraries, which are about to become part of the upcoming new STL version. Thus, using Boost libraries in your project is not even very convenient, it will also guarantee compatibility between platforms and future STL releases. (To all you C++ gurus out there: I hope this short introduction is not too misleading.)

While the Boost libraries are easy to install on Unix machines and the Boost websites offers binaries for Visual C++ 2005, it is more complicated to use Boost on Windows with the GNU Compiler Collection (aka gcc). In fact, it took me several hours to figure out how to use Boost with my favourite IDE, Netbeans. Netbeans? The Java IDE from Sun? Yes, I prefer Netbeans over Eclipse, because Netbeans is easier to use, but it is still a full-featured IDE and it even has a great C/C++-Plugin.

After doing some experiments with MingW, I decided that Cygwin would be a better choice. (Maybe I was just to stupid to get things working with MingW.) Here’s what you have to do to get Boost, cygwin with gcc/g++ and Netbeans working under Windows.

  1. Install Cygwin. Make sure that you also install gcc, g++, boost, make and gdb – you should find all of them within the Devel section of the Cywin installer.
  2. Put your Cygwin home and Cygwin bin directory in your PATH variable. In my case I had to add C:cygwinhome;C:cygwinbin; to my PATH variable. (Some help for changing the PATH variable.)
  3. Now open a windows command prompt and execute the following statements: ‘gcc –version’, ‘g++ –version’, ‘make –version’ and ‘gdb –version’. You should get something like this:
    C:Dokumente und Einstellungensf>gcc --version
    gcc (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
    Copyright (C) 2004 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  4. Now install Netbeans. After installation, open Tools – Plugins. Under “Available Plugins” you should see “C/C++”. Install this plugin and restart Netbeans.
  5. Now open, again in Netbeans, Tools – Options – C/C++. In the section “Build Tools” click “Add ..” and choose your Cygwin bin directory (C:cygwinbin) as your base directory and click “Okay”. Netbeans now finds all your compiler and make tools from the Cygwin installation.
  6. In the section “Code Assistance” you have to choose the sub-section “C++ Compiler”. Add the include directory for your Boost libraries: In my installation this is C:cygwinusrincludeboost-1_33_1. Click “Okay”.
  7. Create a new Netbeans C++ project and write some code. Right click your project and choose “Set Configuration – Manage Configurations …”. In the “C++ Compiler” section, include the path to your Boost libraries int the “Include Directories” folder. My path is C:cygwinusrincludeboost-1_33_1.
  8. In the “Linker” section put your Cygwin bin directory (C:cygwinbin) in the “Additional Library Directories” field. Then edit the “Libraries” field below: Click “Add Library File …”, change file type to “.dll” and choose the libraries you need for your project. For my small regex example, I need cyboost_programm_options-gcc-mt-1_33_1 and cyboost_regex-gcc-mt-1_33_1.
  9. While editing your C++ file, hit F6 and Netbeans will compile, link and start your programm. You’re finally done.

This is my example code:

#include <iostream>
#include <string>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
int main() {
    string s = "This is my simple sample text, really.";
    regex re(",|:|-|\s+");
    sregex_token_iterator my_iter(s.begin( ), s.end( ), re, -1);
    sregex_token_iterator my_end;
    while (my_iter != my_end)
        cout << *my_iter++ << 'n';
    return (EXIT_SUCCESS);
}

You can also build this little program on the command line. Open your Cygwin Bash Shell and navigate to your code directory. (Cygwin maps your windows drives under /cygdrive – thus cd /cygdrive/c/ navigates you to your windows C: drive.)
To compile your source file called regexp.cpp, just type:
g++ -c -I/usr/include/boost-1_33_1/ -o regexp.o regexp.cpp
And to link and build it:
g++ -o regexp.exe regexp.o -lboost_program_options-gcc-mt-s -lboost_regex-gcc-mt-s
That’s all. Now type ./regexp.exe to start your compiled program.

Abgelegt in: Development

17 Kommentare:

hello,

i applied this tutorial in my linux system but i get this error:

error while loading shared libraries: libboost_regex-gcc42-mt-1_35.so.1.35.0: cannot open shared object file: No such file or directory

what can be the cause?have you any idea?
thanks…

This tutorial just addresses Cygwin installations; I don’t know what problems may arise with linux. Sorry.

bahadir:
you could try adding the line “/usr/local/lib” without quotes to the file /etc/ld.so.conf (create it first if it doesn’t exist), run the command “export LD_LIBRARY_PATH=/usr/local/lib”, and then run the command ldconfig. This should (hopefully) ensure that when programs try to load dynamic libraries (like libboost_regex-gcc42-mt-1_35.so.1.35.0), they will search through /usr/local/lib, which seems to be the default installation directory for boost.

Now if only I could figure out how to use a compiler flag like -l boost_regex, instead of -l boost_regex-gcc42-mt-1_35.so.whatever … kind of defeats the whole point of being platform independent

Thank you.
Your advice worked great.
I found this using Google.

Hey thanks for this write up.

regex.cpp:8: error: unknown escape sequence ‘s’

i ended up changing the argument for regex initialization to:
regex re(“,|:|-|\s+”);

is this just my compiler or is there another way to escape so i can just leave it as s?

>kind of defeats the whole point of being platform independent

here’s my Makefile
all: regex.o
g++ -Wall -o targetexe regex.cpp -lboost_regex

also concerning your code sample; line with cout seems to be missing the backslash for newline.

you know any other blogs dedicated to c++?

Stefan,

Excellent work – this worked a treat for me, oh how I will now despise you for allowing me to code multi-threaded apps in C++ (in time).

Thanks and keep up the good work – loads of people appreciate your efforts.

Dean

Hey Stefan,
I was working through the steps but i am confused at step 8 (I have got CygWin, Netbeans and boost 1.37)you have mentioned that we need following “cyboost_programm_options-gcc-mt-1_33_1 and cyboost_regex-gcc-mt-1_33_1″ dll for the sample code, I am not sure where these files are located or did I miss some installation step and why do we need these dlls, pardon my stupidity :)

@d benke: you have to use the Unix libraries, not the windows ones.

Very clean article. Thanks.

Please make the menu items “Bold” for improved readability.

I followed your doc. But really confused at step as benke. And I did not very clear about the answer of RHH. Could you guide me? pls,email me.

HI, I found the dlls which are in cygwinbin. Thanks for you great work.

I’ve followed the instructions but I’m getting the following error:

/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/c/Documents and Settings/%USER%/My Documents/NetBeansProjects/hello/helloworld’
/usr/bin/make -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/helloworld.exe
make[2]: Entering directory `/c/Documents and Settings/%USER%/My Documents/NetBeansProjects/hello/helloworld’
mkdir -p build/Debug/Cygwin-Windows
rm -f build/Debug/Cygwin-Windows/main.o.d
g++.exe -c -g -I/cygdrive/C/cygwin/usr/include/boost-1_33_1 -MMD -MP -MF build/Debug/Cygwin-Windows/main.o.d -o build/Debug/Cygwin-Windows/main.o main.cpp
make[2]: *** No rule to make target `/cygdrive/C/cygwin/bin/cygboost_regex-gcc-mt-1_33_1.dll’, needed by `dist/Debug/Cygwin-Windows/helloworld.exe’. Stop.
make[2]: Leaving directory `/c/Documents and Settings/%USER%/My Documents/NetBeansProjects/hello/helloworld’
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/c/Documents and Settings/%USER%/My Documents/NetBeansProjects/hello/helloworld’
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 15s)

Any ideas where I’m gong wrong?

eh~~~
but how to compile the asio…python…thread library in gcc for windows with boost 1.41.0?

Hello there! i’ve follow all steeps but i couldnt find these dlls cyboost_programm_options-gcc-mt-1_33_1 on steep 8 , and now i’ve got this error :(

build/Debug/Cygwin-Windows/main.o: In function `_ZN5boost6detail15sp_counted_base7destroyEv':
/cygdrive/C/cygwin/usr/include/boost-1_33_1/boost/detail/sp_counted_base_gcc_x86.hpp:(.text$_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits > >::assign(char const*, char const*, unsigned int)]+0x22): undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits > >::do_assign(char const*, char const*, unsigned int)’
/cygdrive/C/cygwin/usr/include/boost-1_33_1/boost/detail/sp_counted_base_gcc_x86.hpp:(.text$_ZN5boost12regex_searchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsESD_[bool boost::regex_search<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits, std::allocator > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits, std::allocator > > > >, char, boost::regex_traits<char, boost::cpp_regex_traits > >(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits, std::allocator > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits, std::allocator > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits, std::allocator > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits, std::allocator > > > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits, std::allocator > >)]+0xc8): undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits, std::allocator > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits, std::allocator > > > >, boost::regex_traits<char, boost::cpp_regex_traits > >::find()’
build/Debug/Cygwin-Windows/main.o: In function `main':
/cygdrive/c/Documents and Settings/drios.MAFP/Mis documentos/NetBeansProjects/helloWorld/main.cpp:12: undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits, std::allocator > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits, std::allocator > > > >, boost::regex_traits<char, boost::cpp_regex_traits > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits > > const&, boost::regex_constants::_match_flags)’
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/Cygwin-Windows/helloworld.exe] Error 1
make[2]: Leaving directory `/cygdrive/c/Documents and Settings/drios.MAFP/Mis documentos/NetBeansProjects/helloWorld’
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/cygdrive/c/Documents and Settings/drios.MAFP/Mis documentos/NetBeansProjects/helloWorld’
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 10s)

Any help?

I did everything in the tutorial successfully, however I could it find the following dll files in my system (step 8 from your tutorial) cyboost_programm_options-gcc-mt-1_33_1 and cyboost_regex-gcc-mt-1_33_1.

Thanks

Schreibe einen Kommentar
benötigt
benötigt (wird nicht angezeigt)
optional

Suchen