如何在 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
的意思是,按照 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
$
更新#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 的评论整理成对我有用的答案:
- 使用
mingw64
shell,而不是 msys*
。 (不确定分成多个二进制文件的目的是什么。也许这对其他 windows 版本很重要。)
- 在
/mingw64/lib
下搜索与您要编译的文件相匹配的存档文件。因此,如果 foobar
是您想要的功能名称,请在此处查找 libfoobar.a
。
- 用
g++ main.cpp -o main -lfoobar
编译
使用 pacman
.
安装它们
$ pacman -S mingw-w64-x86_64-boost mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain
创建一个 CMakeList.txt with boost,使用 OLen 的回答。
另请参阅: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',这将允许二进制文件 运行 正常 windows 如果你在 '/mingw64/bin' 中有 DLL你的道路。
另一个工具链 (un-prefixed g++) 需要 msys2 shell 到 运行 程序(至少这是最简单的方法)。
我(认为)我已经安装了我需要的所有库,例如,
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
的意思是,按照 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
$
更新#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 的评论整理成对我有用的答案:
- 使用
mingw64
shell,而不是msys*
。 (不确定分成多个二进制文件的目的是什么。也许这对其他 windows 版本很重要。) - 在
/mingw64/lib
下搜索与您要编译的文件相匹配的存档文件。因此,如果foobar
是您想要的功能名称,请在此处查找libfoobar.a
。 - 用
g++ main.cpp -o main -lfoobar
编译
使用 pacman
.
$ pacman -S mingw-w64-x86_64-boost mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain
创建一个 CMakeList.txt with boost,使用 OLen 的回答。
另请参阅: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',这将允许二进制文件 运行 正常 windows 如果你在 '/mingw64/bin' 中有 DLL你的道路。
另一个工具链 (un-prefixed g++) 需要 msys2 shell 到 运行 程序(至少这是最简单的方法)。