Search code examples
c++boostmsys2

How do I compile C++ code with Boost on Msys2


I (think) that I have all the libraries installed that I need, e.g.,

pacman -S mingw-w64-x86_64-boost

Also the common development libraries that the msys2 install documentation recommends. I am testing it out with these includes:

#include <iostream>
#include <vector>
#include <string>
#include <boost/program_options.hpp>

I've tried various permutations such as below with the same error:

$ g++ -std=c++11 main.cpp -lboost_programoptions -I /mingw64/include/boost/ -o main
main.cpp:10:10: fatal error: boost/program_options: No such file or directory
   10 | #include <boost/program_options>
      |          ^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

How do I get boost with msys2 working?


Update:

Because of the question of @HolyBlackCat I discovered that there are two different versions of gcc installed, gcc 10.2 and mingw-w64-x86_64-gcc 10.3 . I'm not sure which one I should get rid of. See below, pacman -Rcns is intended to mean, "remove this package and all of its dependencies" as per here.

$ pacman -Rcns gcc
checking dependencies...

Packages (6) binutils-2.36.1-4  msys2-runtime-devel-3.2.0-14
             msys2-w32api-headers-9.0.0.6158.1c773877-1
             msys2-w32api-runtime-9.0.0.6158.1c773877-1
             windows-default-manifest-6.4-1  gcc-10.2.0-1

Total Removed Size:  319.23 MiB

:: Do you want to remove these packages? [Y/n] n

$ pacman -Rcns mingw-w64-x86_64-gcc
checking dependencies...

Packages (7) mingw-w64-x86_64-gcc-ada-10.3.0-5
             mingw-w64-x86_64-gcc-fortran-10.3.0-5
             mingw-w64-x86_64-gcc-objc-10.3.0-5  mingw-w64-x86_64-isl-0.24-1
             mingw-w64-x86_64-libgccjit-10.3.0-5
             mingw-w64-x86_64-windows-default-manifest-6.4-3
             mingw-w64-x86_64-gcc-10.3.0-5

Total Removed Size:  507.78 MiB

:: Do you want to remove these packages? [Y/n] n

$
 

Update #2

I uninstalled gcc 10.2 and started mingw64 vs. msys and now I get this:

user@host MINGW64 /c/Users/user/boostexample
$ g++ main.cpp -o main -lboost_program_options
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lboost_program_options
collect2.exe: error: ld returned 1 exit status

Update #3

I needed to invoke the compiler with:

g++ main.cpp -o main -lboost_program_options-mt

and it compiled fine. I suppose I should find where in the documentation these norms are specified.


Solution

  • Curating HolyBlackCat's comments into an answer that worked for me:

    1. Use mingw64 shell, not msys*. (Not sure what purpose the division into multiple binaries serves. Perhaps it matters for other windows versions.)
    2. Search for the archive file under /mingw64/lib that matches what you wanted to compile against. So if foobar is name of the functionality you wanted look for libfoobar.a there.
    3. compile with g++ main.cpp -o main -lfoobar