'filesystem' is not a namespace-name

Why does it say filesystem is not a namespace-name??
1
2
3
4
5
6
7
8
9
10
11
  #define _WIN32_WINNT 0x0500
#include <Windows.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <filesystem>


namespace fs = std::filesystem; 


I'm using VS Code on windows 10, with g++.exe as a compiler using C++17
Some MinGW implementations (that's what g++ on Windows is) are a little behind the curve when it comes to particular features, especially ones that require the most interaction with the OS, like std::filesystem. You would need to look up filesystem support for the particular version of g++/MinGW you have.

It may be that you have experimental filesystem support with C++17, so maybe try
1
2
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;


or
1
2
#include <filesystem>
namespace fs = std::experimental::filesystem;

instead. Still not guaranteed it will work. I assume you are compiling with at least -std=c++17.

Edit: And if you still can't get it to work, others have made their own header files you can download and use.
https://github.com/gulrak/filesystem
In the meantime, for anyone wants an almost identical experience to std::filesystem, you can try ghc::filesystem from https://github.com/gulrak/filesystem, which is a drop-in replacement for std::filesystem. I am using it in a personal project, and it seems to be doing exactly what I want from it, just like std::filesystem. It's a header only dependency, and all you need to do beside including ghc/filesystem.hpp is just replace every instance of std::filesystem with ghc::filesystem.
Last edited on
I'd be careful using OS-dependent headers like <filesystem>, <windows.h>, and <conio.h>, mainly if you're going to be giving this code to someone else, because if they don't have the header file, then they can't use the program.

But if you're just going to use it for personal stuff and it won't be used by anyone off your computer, that's fine. Just a warning.
<filesystem> is NOT os-dependent. It is a standard part of C++17.
Welp, my compiler doesn't have it. Even though it has C++17...?
That probably means that your compiler is out of date and should be upgraded. What compiler/version are you using?

My compiler is:
Apple LLVM version 10.0.0 (clang-1000.10.44.4)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /usr/bin


It has the ability to compile as C++20, with the -std=c++2a flag, but apparently it doesn't have the header.
Well according to this page you would need Clang 11 at a minimum for filesystem support on a Mac.

https://en.cppreference.com/w/cpp/compiler_support/17
Last edited on
I have had some trouble with <filesystem> as well.
Almost everything in it can be done without trivially, its just a lot cleaner if you have it.
@jlb,
Yeah, I figured something like that. Guess I should start looking into an update. Especially considering my computer is 12 years old...
Last edited on
Topic archived. No new replies allowed.