简体   繁体   中英

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

I (think) that I have all the libraries installed that I need, eg,

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.

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

Install them using pacman .

 $ pacman -S mingw-w64-x86_64-boost mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain

Create a CMakeList.txt with boost , using OLen's answer.

See also: https://www.msys2.org/docs/cmake/

$ cmake ..
-- Building for: Ninja
-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/msys64/usr/bin/cc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/msys64/usr/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: C:/msys64/mingw64/include (found suitable version "1.78.0", minimum required is "1.40") found components: program_options
-- Configuring done
-- Generating done
-- Build files have been written to: build

In the above, I have used only 'mingw-64', this will allow the binary to run on regular windows if you have DLLs in '/mingw64/bin' on your path.

The other toolchain (un-prefixed g++) requires an msys2 shell to run the program from (at least that is the easiest way).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM