[英]can't use filesystem in g++ / msys2 on windows
So, I am writing C++ code on my windows 10 machine.所以,我正在我的 windows 10 机器上编写 C++ 代码。 I generally use cmake (3.17.2) and minGW (g++ 9.3.0) I found out about "filesystem" and decided to use it, but kept getting the error that it was undefined.
我通常使用 cmake (3.17.2) 和 minGW (g++ 9.3.0) 我发现了“文件系统”并决定使用它,但一直收到它未定义的错误。 Apparently "filesystem" wasn't implemented on minGW until version 9 and at the time, I was using version 8. So, I upgraded to version 9. After upgrading, my program wouldn't compile because of a threading issue which I was able to resolve by installing msys2 and using its package manager to install the full toolchain.
显然“文件系统”直到版本 9 才在 minGW 上实现,当时我使用的是版本 8。所以,我升级到版本 9。升级后,由于线程问题,我的程序无法编译通过安装 msys2 并使用其 package 管理器安装完整的工具链来解决。
My current problem is that when I query msys2 about it says it's installed:我目前的问题是,当我查询 msys2 时说它已安装:
$ pacboy -Ss filesystem
msys/filesystem 2020.02-2 (base) [installed]
Base filesystem
but when I try to compile a program from command line, I get this:但是当我尝试从命令行编译程序时,我得到了这个:
F:\things\software\Template-CMD>g++ main.cpp
main.cpp: In function 'int main(int, char**)': main.cpp:5:10: error: 'std::filesystem' has not been declared
5 | std::filesystem::current_path();
| ^~~~~~~~~~
This is the output from compiling the following small test program consisting of one file, "main.cpp":这是 output 编译以下由一个文件“main.cpp”组成的小测试程序:
#include <iostream>
#include <filesystem>
int main(int, char**) {
std::filesystem::current_path();
std::cout << "Hello, world!\n";
}
I can use windows file search to confirm that a file named "filesystem" does exist at:我可以使用 windows 文件搜索来确认名为“filesystem”的文件确实存在于:
C:\msys64\mingw64\include\c++\9.3.0\filesystem
C:\msys64\mingw64\include\c++\9.3.0\filesystem
so I tried to copy it to just the include folder:所以我试图将它复制到包含文件夹:
C:\msys64\mingw64\include\filesystem
C:\msys64\mingw64\include\filesystem
but that didn't work either.但这也没有用。 My PATH has only one g++ compiler in it:
我的 PATH 中只有一个 g++ 编译器:
C:\msys64\mingw64\bin
C:\msys64\mingw64\bin
To show that g++ is version 9:要显示 g++ 是版本 9:
F:\things\software\Template-CMD>g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-9.3.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++ --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts=yes --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check --enable-lto --enable-libgomp --disable-multilib --enable-checking=release --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --enable-plugin --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev2, Built by MSYS2 project' --with-bugurl=https://sourceforge.net/projects/msys2 --with-gnu-as --with-gnu-ld
Thread model: posix
gcc version 9.3.0 (Rev2, Built by MSYS2 project)
File system is a general term in computing which can mean different things in different contexts.文件系统是计算中的通用术语,在不同的上下文中可能意味着不同的事物。
The MSYS2 package you found named filesystem
is just a basic package for MSYS2 that installs a bunch of files and folders inside MSYS2.您找到的名为
filesystem
的 MSYS2 package 只是用于 MSYS2 的基本 package,它在 MSYS2 中安装了一堆文件和文件夹。 Like any other MSYS2 package, you can investigate filesystem
to see exactly what it installs by running pacman -Ql filesystem
.与任何其他 MSYS2 package 一样,您可以通过运行
pacman -Ql filesystem
来调查filesystem
以查看其安装的确切内容。 But this package is irrelevant to the problem at hand.但是这个 package 与手头的问题无关。
The library you load by writing #include <filesystem>
inside a C++ program is part of the C++ standard library, and it helps you access the file systems of your computer.您通过在 C++ 程序中写入
#include <filesystem>
加载的库是 C++ 标准库的一部分,它可以帮助您访问计算机的文件系统。 This part of the standard library was added in C++17 , but GCC 9.3.0 defaults to using some older version of C++ when you don't specify what version to use.这部分标准库是在C++17中添加的,但是 GCC 9.3.0 默认使用一些旧版本的 C++ 当你不指定使用什么版本时。 To use
filesystem
, you should compile your program with a command like this, which specifies that you want to use C++17:要使用
filesystem
,您应该使用如下命令编译程序,该命令指定您要使用 C++17:
g++ -std=gnu++17 main.cpp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.