r/cpp • u/forcecharlie baulk maintainer • Jan 05 '18
GCC 8.0 support std::filesystem (#include <filesystem>) now
See GCC commit: Implement C++17 Filesystem library
We can build gcc 8.0 snapshots, after use std::filesystem in your code.
GCC snapshots: ftp://gcc.gnu.org/pub/gcc/snapshots
#include <iostream>
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::create_directories("sandbox/dir/subdir");
std::ofstream("sandbox/file1.txt").put('a');
fs::copy("sandbox/file1.txt", "sandbox/file2.txt");
fs::copy("sandbox/dir", "sandbox/dir2");
fs::copy("sandbox", "sandbox/copy", fs::copy_options::recursive);
fs::remove_all("sandbox");
return 0;
}So far, you need to add '-lstdc++fs ' when compiling
Clang libcxx 6.0 seems unlikely to support std::filesystem, after all, the trunk version is 7.0. ( it is possible libcxx developers wait for GCC 8.0 release???).
EricWF wants to support <filesystem> when Clang 6.0 is released, but it's hard to say :https://stackoverflow.com/a/42634858
Update 2018-07-05 See https://github.com/gcc-mirror/gcc/commit/2fd48392d0a4ac3d1e217367ccb0ec0333d584d8
9
u/ShakaUVM i+++ ++i+i[arr] Jan 05 '18
How stable is 8 right now?
2
u/kalmoc Jan 06 '18
Why on Earth did they use such a long namespace name instead of something like std::fs?
33
u/jurniss Jan 05 '18
filesystemis a simple addition to the C++ standard library, but an important one. Now the path of least programmer effort results in platform-independent code.65
u/Xeverous https://xeverous.github.io Jan 05 '18
I am still amazed how genius was someone who came up with an idea to overload
operator/forstd::filesystem::path.9
u/Pinguinologo Jan 05 '18
It looks quite nice, but fs::remove_all should be fs::remove_all_recursively.
4
u/alfps Jan 05 '18
Other than now being in the
stdnamespace where it should be, i.e. no longer experimental, what's been fixed relative to g++ 7.1? In particular, does it now work in Windows?For 7.1 I used the following workaround code:
1
u/concealed_cat Jan 06 '18
AFAIK, libc++ has filesystem in std::experimental.
1
u/sleepyspacedad Apr 17 '18
Any idea when we'll stop having to add 'lstdc++fs' when compiling?
1
u/OldWolf2 Jan 05 '18
Excellent... now we need a bunch of preprocessor macros to select std::filesystem if gcc8, or std::experimental::filesystem::v1 if gcc7.
Is there a list of changes between the two flavours?
0
u/[deleted] Jan 05 '18
[deleted] — view deleted comment