繁体   English   中英

C++ CodeBlocks 升级到 C++17 不能使用 std::filesystem

[英]C++ CodeBlocks upgrade to C++17 can't use std::filesystem

我需要能够列出目录中的文件,因此我尝试将 CodeBlocks 中的 C++ 版本升级到 C++ 17,以便我可以使用文件系统。 为此,我遵循了http://candcplusplus.com/enable-c17-in-code-blocks-mingw-gcc-for-all-version-with-pictures#:~:text=Enabling%20the% 中概述的步骤20C%2B%2B17,创建%20a%20project

我不需要做太多更改,CodeBlocks 20.03 和 MinGW 8.1.0 已经安装。 当我构建 wxWidgets 时,MinGW 已经在我的道路上。 设置->编译器...->工具链可执行文件选项卡我不必进行任何更改,并在代码块中显示为:

在此处输入图片说明

我还选中了在编译器设置中使用 C++ 17 的框,如下所示

在此处输入图片说明

我按照说明在网站上运行了测试程序并得到了“真!”。

但是,当我将基本测试程序更改为此,尝试使用文件系统读取目录中的文件时,出现错误:

#include <iostream>
#include <filesystem>

using namespace std;

int main()
{
    const int i=90;

    if constexpr (i) //'if constexpr' is part of C++17
    {
        cout << "True!";
    }
    else
    {
        cout<<"False" ;
    }

    std::string path = "../MagicProgCPP/files/debug images/";
    for (const auto & entry : filesystem::directory_iterator(path))
    {
        cout << entry.path() << std::endl;
    }


    cin.get();
    return 0;
}

程序停止构建,打开文件 fs_path.h 并在这一行停止:

#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
      if (__p.is_absolute()
      || (__p.has_root_name() && __p.root_name() != root_name()))     <----- ******STOPS HERE
    operator=(__p);
      else
    {
      string_type __pathname;
      if (__p.has_root_directory())
        __pathname = root_name().native();
      else if (has_filename() || (!has_root_directory() && is_absolute()))
        __pathname = _M_pathname + preferred_separator;
      __pathname += __p.relative_path().native(); // XXX is this right?
      _M_pathname.swap(__pathname);
      _M_split_cmpts();
    }
#else
      // Much simpler, as any path with root-name or root-dir is absolute.
      if (__p.is_absolute())
    operator=(__p);
      else
    {
      if (has_filename() || (_M_type == _Type::_Root_name))
        _M_pathname += preferred_separator;
      _M_pathname += __p.native();
      _M_split_cmpts();
    }
#endif
      return *this;
    }

我在构建日志中收到此错误:

C:\\Program Files\\CodeBlocks\\MinGW\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++\\bits\\fs_path.h|237|error: 'operator!=' 不匹配(操作数类型是'std::filesystem::__cxx11::path' 和 'std::filesystem::__cxx11::path')|

当我输入路径时,我非常确信路径存在并且其中有文件。 构建日志消息表明我可能没有使用 C++17? 但是当我单击构建时,这是程序用于构建的行:

g++.exe -Wall -fexceptions -g -Wall -std=c++17  -c E:\testc17\main.cpp -o obj\Debug\main.o

我究竟做错了什么? 谢谢

自 2018-07 年以来,错误 78870 已修复。
您应该将以下库添加到项目选项 -> 链接器设置 -> 链接库: stdc++fs 我尝试使用 MinGW gcc 8.1.0(通过 CodeBlocks)编译您的代码并且一切正常(显然使用另一个路径,因为我没有与您相同的目录)。 在此处输入图片说明 您还可以添加对搜索目录是否存在的检查,如下所示:

namespace fs = std::filesystem;
std::string mypath { "../MyDir" };
if(fs::exists(mypath))
{
    for(const auto & entry : fs::directory_iterator(path))
    {
        cout << entry.path() << std::endl;
    }
}

看来这个确切的问题是 mingw 8.1 中的一个已知错误。 错误报告在这里: https : //sourceforge.net/p/mingw-w64/bugs/737/

并在同一位置出现错误:

operator != is declared and defined in line 550, but referenced in line 237.

The problem is triggered by operator/= in line 233:

    path& operator/=(const path& __p)
    {
#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
      if (__p.is_absolute()
      || (__p.has_root_name() && __p.root_name() != root_name()))
    operator=(__p);
      else
    {
      string_type __pathname;
      if (__p.has_root_directory())
        __pathname = root_name().native();
      else if (has_filename() || (!has_root_directory() && is_absolute()))
        __pathname = _M_pathname + preferred_separator;
      __pathname += __p.relative_path().native(); // XXX is this right?
      _M_pathname.swap(__pathname);
      _M_split_cmpts();
    }

错误报告说这是在 master 中修复的,这意味着您需要安装一个应用了修复程序的 mingw 版本。 我认为最好的方法是将 mingw 升级到 8.1 以上的版本

user4581301在上面的主要问题中评论说以下链接包含有关如何安装 mingw 的说明: 如何安装 MinGW-w64 和 MSYS2?

暂无
暂无

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

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