Search code examples
boostcmdg++

Using boost in console programming environment with mingw g++ compiler


I installed boost following instructions here: https://phylogeny.uconn.edu/tutorial-v2/part-1-ide-project-v2/setting-up-the-boost-c-library-v2/#

works in Visual Studio, but doesn't work when compiling using mingw g++ in windows cmd.

Editor is vim.

source code is like:

#include <boost/algorithm/string.hpp>
...

compiling like this:

> g++ -o test test.cpp
test.cpp:7:10: fatal error: boost/algorithm/string.hpp: No such file or directory
 #include <boost/algorithm/string.hpp>

How can I make my mingw g++ available for boost?


Solution

  • You used the config from the page:

    import option ; 
    using msvc ; 
    option.set keep-going : false ; 
    libraries = --with-program_options --with-regex --with-filesystem --with-system ;
    

    using msvc should probably tell you something. It uses the MSVC toolchain. Because these are not interoperable (different C++ runtime libraries, for starters, different ABI likely), you can't link to those libraries.

    Header Only

    If, like the code you showed, you only require headers, then that is no issue, just add the include path to your compiler flags, as the tutorial also told you: enter image description here

    So either typing command line options directly:

    g++ -I C:\boost_1_65_0
    

    Or adding to a variable in your build script(s), like Makefile:

    CPPFLAGS+=-I C:\boost_1_65_0
    

    Or CMake:

    INCLUDE_DIRECTORIES(C:\boost_1_65_0)
    

    With Linking

    To use pre-built shared libraries, you need to build different versions for mingw. See e.g. these steps: https://gist.github.com/zrsmithson/0b72e0cb58d0cb946fc48b5c88511da8

    I installed from that last week (context) and it worked fine. (I also ended up going no-IDE with Vim, though VsCode was ok as well)

    Check that it is using the mingw toolchain (e.g. mgw81) which also means that mgw81 shoud be part of the library filenames. Following just that tutorial already gives you this, but your previously existing configs might interfere if you're not careful.