【问题标题】:can't use filesystem in g++ / msys2 on windows无法在 Windows 上的 g++/msys2 中使用文件系统
【发布时间】:2020-05-06 22:35:26
【问题描述】:

所以,我正在我的 Windows 10 机器上编写 C++ 代码。我通常使用 cmake (3.17.2) 和 minGW (g++ 9.3.0) 我发现了“文件系统”并决定使用它,但不断收到它未定义的错误。显然“文件系统”直到第 9 版才在 minGW 上实现,当时我使用的是第 8 版。所以,我升级到第 9 版。升级后,由于线程问题,我的程序无法编译通过安装 msys2 并使用其包管理器安装完整的工具链来解决。

我目前的问题是,当我查询 msys2 时说它已安装:

$ pacboy -Ss filesystem

msys/filesystem 2020.02-2 (base) [installed]
    Base filesystem

但是当我尝试从命令行编译程序时,我得到了这个:

   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();
          |          ^~~~~~~~~~

这是编译以下由一个文件“main.cpp”组成的小测试程序的输出:

#include <iostream>
#include <filesystem>

int main(int, char**) {
    std::filesystem::current_path();
    std::cout << "Hello, world!\n";
}

我可以使用 windows 文件搜索来确认名为“filesystem”的文件确实存在于:

C:\msys64\mingw64\include\c++\9.3.0\文件系统

所以我尝试将它复制到仅包含文件夹:

C:\msys64\mingw64\include\文件系统

但这也没有用。我的 PATH 中只有一个 g++ 编译器:

C:\msys64\mingw64\bin

显示 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)

【问题讨论】:

    标签: c++ g++ filesystems mingw msys2


    【解决方案1】:

    File system 是计算中的通用术语,在不同的上下文中可以表示不同的含义。

    您找到的名为filesystem 的 MSYS2 包只是 MSYS2 的一个基本包,它在 MSYS2 中安装了一堆文件和文件夹。与任何其他 MSYS2 软件包一样,您可以通过运行 pacman -Ql filesystem 调查 filesystem 以查看它安装的确切内容。但这个包与手头的问题无关。

    通过在 C++ 程序中写入 #include &lt;filesystem&gt; 加载的库是 C++ 标准库的一部分,它可以帮助您访问计算机的文件系统。这部分标准库是在C++17 中添加的,但是当您没有指定要使用的版本时,GCC 9.3.0 默认使用一些旧版本的 C++。要使用filesystem,您应该使用这样的命令编译您的程序,该命令指定您要使用C++17:

    g++ -std=gnu++17 main.cpp
    

    【讨论】:

    • -std=c++17,如果您不想意外使用非便携式 GNU 扩展。
    • 谢谢!我没有想到最新版本的 g++ 不会默认为 c++ 的最新标准。我做的大部分编程都是遗留的 c 编程,这是我第一次尝试使用所有可能的最新版本。
    猜你喜欢
    • 1970-01-01
    • 2021-04-25
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2015-09-20
    • 1970-01-01
    相关资源
    最近更新 更多