user3162266
user3162266

Reputation: 87

Visual Studio Code: using filesystem library

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");
}

And I get the following error:

'filesystem' is not a namespace-name

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.

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?

Upvotes: 1

Views: 3617

Answers (2)

Mate, I had the same issue. The solution to this is to change the C++ version to the latest one under solution properties > C++ and change it from the default C++ version (most likely C++ 14) to the latest on or anything above C++ 17.

Upvotes: 2

user3162266
user3162266

Reputation: 87

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

  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.

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

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

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

Upvotes: 0

Related Questions