简体   繁体   English

如何在 Msys2 上使用 Boost 编译 C++ 代码

[英]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.还有 msys2 安装文档推荐的通用开发库。 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?我如何通过 msys2 工作获得提升?


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.由于@HolyBlackCat的问题,我发现安装了两个不同版本的gcc,gcc 10.2和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的意思是,按照此处“删除此 package 及其所有依赖项”。

$ 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更新 #2

I uninstalled gcc 10.2 and started mingw64 vs. msys and now I get this:我卸载了 gcc 10.2 并启动了 mingw64 vs. msys,现在我明白了:

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更新 #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:将 HolyBlackCat 的评论整理成对我有用的答案:

  1. Use mingw64 shell, not msys* .使用mingw64 shell,而不是msys* (Not sure what purpose the division into multiple binaries serves. Perhaps it matters for other windows versions.) (不确定分成多个二进制文件的目的是什么。也许这对其他 windows 版本很重要。)
  2. Search for the archive file under /mingw64/lib that matches what you wanted to compile against./mingw64/lib下搜索与您要编译的内容相匹配的存档文件。 So if foobar is name of the functionality you wanted look for libfoobar.a there.因此,如果foobar是您想要的功能名称,请在那里查找libfoobar.a
  3. compile with g++ main.cpp -o main -lfoobarg++ main.cpp -o main -lfoobar编译

Install them using pacman .使用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.使用 OLen 的回答,使用boost 创建一个 CMakeList.txt

See also: https://www.msys2.org/docs/cmake/另见: 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.在上面,我只使用了“mingw-64”,如果你的路径上的“/mingw64/bin”中有 DLL,这将允许二进制文件在常规 windows 上运行。

The other toolchain (un-prefixed g++) requires an msys2 shell to run the program from (at least that is the easiest way).另一个工具链(无前缀的 g++)需要一个 msys2 shell 来运行程序(至少这是最简单的方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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