C++ 17 Filesystem

I have typed g++ --version on may computer got following message:
g++ (GCC) 8.0.0 20170430 (experimental)
Copyright (C) 2017 Free Software Foundation, Inc.

Then I typed following g++ command and I am getting error with filesystem include file as shown below:

g++ -std=c++17 browse_files_in_dir.cpp -o browse_files
browse_files_in_dir.cpp:3:10: fatal error: filesystem: No such file or directory
#include <filesystem>
^~~~~~~~~~~~
compilation terminated.

I have pasted c++ code below.
Is filesystem library features not supported by the gcc 8.0.0 please?


1
2
3
4
5
6
7
8
9
10
11
#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
    std::string path = "C:\\cpp_filesystem";
    for (auto & p : fs::directory_iterator(path))
        std::cout << p << std::endl;
}
Have you tried: #include <experimental/filesystem>
As of this time C++17 is still a work in progress and may still be considered experimental.

namespace fs = std::experimental::filesystem::v1;


Many thanks for the information.
I tried and it is still giving error:

g++ -std=c++17 browse_files_in_dir.cpp -o file_browser
browse_files_in_dir.cpp:3:10: fatal error: experimental/filesystem: No such file or directory
#include <experimental/filesystem>
^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

I looked at the following folder path in which gcc 8.0.0 is installed and the filesystem file is not in this folder. Any way to find the filesystem file please?

C:\gcc_8\gcc\include\c++\8.0.0\experimental


#include <string>
#include <iostream>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;

int main()
{
std::string path = "C:\\cpp_filesystemy";
for (auto & p : fs::directory_iterator(path))
std::cout << p << std::endl;
}


AFAIK the GNU implementation of the filesystem TS is not part of their standard C++ library; it is in a separate library called libstdc++fs.a. This library is not available / is thoroughly broken on non-posix platforms.
On my Linux system I linked to stdc++fs :

g++ -std=gnu++1z -Wall -Wextra -pedantic-errors browse_files_in_dir.cpp -o file_browser -lstdc++fs

On my system the libray file is built with debug info, so the executable comes out quite large. This was fixed by using objcopy -g with the executable file, on Linux. It might be different on Windows.

Thanks to mbozzi for helping me with that.

Edit:

Filesystem is a technical specification (as opposed to the core language), you might need -std=gnu++1z or -std=c++1z flag instead of -std=c++17 to use it - I did.

Can you search for the filesystem header file?

Last edited on
I looked in my computer's path C:\gcc_8\gcc\lib
I did not find libstdc++fs.a file. I have only libstdc++.a file on my windows 10 computer.
Thanks again.
Have a look at boost filesystem instead?
TheIdeasMan,
Thanks for info.
I tried both as follows and both did not file the filesystem include file:

C:\cpp\browse_files_in_dir>g++ -std=c++1z browse_files_in_dir.cpp -o file_browser
browse_files_in_dir.cpp:3:10: fatal error: experimental/filesystem: No such file or directory
#include <experimental/filesystem>
^~~~~~~~~~~~~~~~~~~~~~~~~

C:\cpp\browse_files_in_dir>g++ -std=gnu++1z browse_files_in_dir.cpp -o file_browser
browse_files_in_dir.cpp:3:10: fatal error: experimental/filesystem: No such file or directory
#include <experimental/filesystem>
^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

I searched for filesystem file in Windows explorer and found it in Microsoft\Visual Studio 9.0\VB folder. But this is NOT the file C++ compiler needs I believe.
@AlexCantor

My reply was ninja'd by the great JLBorges. So it isn't supported on windows, that's why I suggest looking at boost.

For your compiling generally you should at least use these options :

-Wall -Wextra -pedantic-errors

Warnings are your friend :+)
On Windows, recent versions of the microsoft compiler have implementations of the Filesystem TS.
http://www.cplusplus.com/forum/beginner/211374/#msg990138
@JLBorges,

Beautiful. Above Microsoft compiler information is very helpful.
I have downloaded Microsoft Visual Studio Community Edition 2017 from Microsoft.com
The example filesystem program you have provided at the above cplusplus.com link
beginner/211374/#msg990138 worked like a charm. The program re-cursed through folders and listed using cout the folder names and file names.

Many, many thanks for very useful information.

May I please know links to other example programs that utilize various filesystem features to perform various text files processing utilities?
Boost Filesystem tutorial has a number of examples. Though these examples use the boost filesystem library,
they are in alignment with std::filesystem / std::experimental::filesystem
http://www.boost.org/doc/libs/1_64_0/libs/filesystem/doc/tutorial.html
The complete .cpp files are here: http://www.boost.org/doc/libs/1_64_0/libs/filesystem/example/

cppreference.com has small example code snippets under many of the topics.
http://en.cppreference.com/w/cpp/filesystem
Topic archived. No new replies allowed.