问题描述
我需要能够列出目录中的文件,因此我正在尝试将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,Create%20a%20project中概述的步骤进行操作。
我不必进行太多更改,已经安装了CodeBlocks 20.03和MinGW 8.1.0。从创建wxWidgets以来,MinGW已经在我的道路上了。设置->编译器...->工具链可执行文件选项卡,我不必进行任何更改,并在CodeBlocks中显示为:
我还选中了在这样的编译器设置中使用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 |错误:“ 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
我在做什么错?谢谢
解决方法
78870的错误已于2018-07年修复。
您应该在项目选项->链接器设置->链接库中添加以下库: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();
}
该错误报告说,此错误已在主版本中修复,这意味着您需要在应用了该修复程序的情况下安装mingw版本。我相信最好的方法是将mingw升级到大于8.1的版本。
user4581301在上述主要问题中指出,以下链接提供了有关如何安装mingw的说明:How to install MinGW-w64 and MSYS2?