繁体   English   中英

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

[英]How do I compile C++ code with Boost on Msys2

我(认为)我已经安装了我需要的所有库,例如,

pacman -S mingw-w64-x86_64-boost

还有 msys2 安装文档推荐的通用开发库。 我正在用这些来测试它,包括:

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

我已经尝试了各种排列,例如下面的相同错误:

$ 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.

我如何通过 msys2 工作获得提升?


更新:

由于@HolyBlackCat的问题,我发现安装了两个不同版本的gcc,gcc 10.2和mingw-w64-x86_64-gcc 10.3。 我不确定我应该摆脱哪一个。 见下文, 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

$
 

更新 #2

我卸载了 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

更新 #3

我需要调用编译器:

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

它编译得很好。 我想我应该在文档中找到指定这些规范的位置。

将 HolyBlackCat 的评论整理成对我有用的答案:

  1. 使用mingw64 shell,而不是msys* (不确定分成多个二进制文件的目的是什么。也许这对其他 windows 版本很重要。)
  2. /mingw64/lib下搜索与您要编译的内容相匹配的存档文件。 因此,如果foobar是您想要的功能名称,请在那里查找libfoobar.a
  3. g++ main.cpp -o main -lfoobar编译

使用pacman安装它们。

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

使用 OLen 的回答,使用boost 创建一个 CMakeList.txt

另见: 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

在上面,我只使用了“mingw-64”,如果你的路径上的“/mingw64/bin”中有 DLL,这将允许二进制文件在常规 windows 上运行。

另一个工具链(无前缀的 g++)需要一个 msys2 shell 来运行程序(至少这是最简单的方法)。

暂无
暂无

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

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