Visual Studio代码:使用文件系统库 [英] Visual Studio Code: using filesystem library

查看:75
本文介绍了Visual Studio代码:使用文件系统库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio Code中的文件系统库.我只是不明白为什么它不起作用.我只是使用该库中的示例代码:

I'm struggling with the filesystem library in Visual Studio Code. I just can't understand why it doesn't work. I'm simply using an example code from that library:

#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
 
int main()
{
    fs::create_directories("sandbox/a/b");
    std::ofstream("sandbox/file1.txt");
    std::ofstream("sandbox/file2.txt");
    for(auto& p: fs::directory_iterator("sandbox"))
        std::cout << p.path() << '\n';
    fs::remove_all("sandbox");
}

然后出现以下错误:

'filesystem' is not a namespace-name

我已经看到了一些与此相关的主题,但是它们都描述了创建makefile的过程.我并不是真的想这样做,只是要了解makefile无法解决的一些基本错误.有没有一种方法可以使它简单地与某些命令一起使用?该线程: Visual Studio代码:如何为g ++编译器添加参数?提到一行

I've seen a few threads about this, but they all describe creating makefiles. I don't really want to do this just to learn there was some fundamental error that the makefile doesn't solve. Is there a way to make it simply work with some command? This thread: Visual Studio Code: how to add arguments for g++ compiler? mentions a line

g++ -g -o debug main.cpp -std=c++17

所以我认为这是可能的.但是,当我键入它时,会出现很多错误,并且没有任何编译.

so I assume it IS possible. But when I type it, I get a ton of errors and nothing compiles.

由于某种原因,Visual Studio 2019没问题-我最初遇到了一些问题,但是只需将C ++标准更改为C ++ 17就可以解决.在Visual Studio Code中,我已经安装了C ++ 17,那么为什么在这种情况下它不起作用?

For some reason, Visual Studio 2019 has no issues - I had some problems initially, but it was simply solved by changing the C++ standard to C++ 17. After that, everything builds and works. In Visual Studio Code I already have the C++ 17 on, so why it doesn't work in this case?

推荐答案

感谢大家的投入.我已经解决了这个问题,尽管没有任何实际的解决方案.MinGW是我最大的嫌疑人,因为:

thank you all for your input. I'm done with this problem, although without any actual solution. MinGW is my biggest suspect, since:

  1. 我能够在Linux上通过ssh进行编译,我的GCC版本为 g ++(GCC)8.3.1 20190311(Red Hat 8.3.1-3)-实际上,这是我的目标,因此我不需要它在Windows上进行编译,并且
  2. 如果我必须针对Windows进行编译,则可以通过Visual Studio 2019来完成,据我所知,Visual Studio 2019默认情况下不使用MinGW.
  1. I am able to compile through ssh on Linux, where my version of GCC is g++ (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3) - this is actually my target, so I don't need it to compile it on Windows, and,
  2. if I have to compile for Windows, I can do it through Visual Studio 2019, which as far as I know, doesn't use MinGW by default.

无法编译的我的GCC版本是 g ++.exe(x86_64-posix-seh-rev0,由MinGW-W64项目构建)8.1.0

My GCC version, where the compilation doesn't work, is g++.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0

另外,在Linux上,当我输入如下命令时,它也可以工作:

Also, on Linux, it works when I type the command like this:

g++ main.cpp -std=c++17 -lstdc++fs -o main

这篇关于Visual Studio代码:使用文件系统库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆