Why Can't I Use <Experimental/Filesystem> with G++ 4.9.2

Why can't I use experimental/filesystem with g++ 4.9.2?

If we look at the libstdc++ status we see that they do have support for the File System TS:

Paper | Title | Status

........

N4100 | File System | Y

but it says:

This page describes the C++14 and library TS support in mainline GCC SVN, not in any particular release.

and from trying this on Wandbox it looks like this library is only available on the latest development branch 6.0 and I can not find more details beyond that.

Update

Update from Jonathan Wakely:

It's also now available in the gcc-5-branch in Subversion, and will be included in the GCC 5.3 release later this year.

Also accordingly to Jonathan Wakely's answer here we need to compile using -lstdc++fs. This is covered in the Linking section of gcc documents:

GCC 5.3 includes an implementation of the Filesystem library defined by the technical specification ISO/IEC TS 18822:2015. Because this is an experimental library extension, not part of the C++ standard, it is implemented in a separate library, libstdc++fs.a, and there is no shared library for it. To use the library you should include and link with -lstdc++fs. The library implementation is incomplete on non-POSIX platforms, specifically Windows support is rudimentary.

Due to the experimental nature of the Filesystem library the usual guarantees about ABI stability and backwards compatibility do not apply to it. There is no guarantee that the components in any header will remain compatible between different GCC releases.

Also see Table 3.1. C++ Command Options.

experimental::filesystem linker error

The Filesystem TS is nothing to do with C++1z support, it is a completely separate specification not part of the C++1z working draft. GCC's implementation (in GCC 5.3 and later) is even available in C++11 mode.

You just need to link with -lstdc++fs to use it.

(The relevant library, libstdc++fs.a, is a static library, so as with any static library it should come after any objects that depend on it in the linker command.)

Update Nov 2017: as well as the Filesystem TS, GCC 8.x also has an implementation of the C++17 Filesystem library, defined in <filesystem> and in namespace std::filesystem (N.B. no "experimental" in those names) when using -std=gnu++17 or -std=c++17. GCC's C++17 support is not complete or stable yet, and until it's considered ready for prime time use you also need to link to -lstdc++fs for the C++17 Filesystem features.

Update Jan 2019: starting with GCC 9, the C++17 std::filesystem components can be used without -lstdc++fs (but you still need that library for std::experimental::filesystem).

I can't compile the filesystem library

You can either compile your code using -lstdc++fs flag OR like @pete mentioned in the comment: remove experimental, as it is now part of standard C++17.

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

int main(){

fs::path pathToShow(fs::current_path());

std::cout << "exists() = " << fs::exists(pathToShow) << "\n"
<< "root_name() = " << pathToShow.root_name() << "\n"
<< "root_path() = " << pathToShow.root_path() << "\n"
<< "relative_path() = " << pathToShow.relative_path() << "\n"
<< "parent_path() = " << pathToShow.parent_path() << "\n"
<< "filename() = " << pathToShow.filename() << "\n"
<< "stem() = " << pathToShow.stem() << "\n"
<< "extension() = " << pathToShow.extension() << "\n";
return 0;
}

and then something like g++ -o fs filesystem.cpp will work fine.

macOS Clang C++17 filesystem header not found

Libc++, which is the C++ standard library on OS X, has not moved <experimental/filesystem> to <filesystem> yet because the specification is not stable.

Hopefully <filesystem> will be a part of the Clang 6.0 release. (We missed 5.0)

How does one access a file from subdirectory that contains specific termination?

If your environment supports the Filesystem TS (ISO/IEC TS 18822:2015) extension, the functionality of Boost.Filesystem is provided by std::.

#include <experimental/filesystem>
#include <algorithm>

#include <iostream>
#include <string>

int main()
{
// APPDATA, of course, is *NOT* portable
std::path path_to_folder( getenv("APPDATA") );
path_to_folder /= "Folder";
std::directory_iterator it( path_to_folder );
std::directory_iterator end;
std::string suffix=".suffix";
while ( it != end )
{
if ( suffix.length() <= filename.length()
&&
std::equals( suffix.rbegin(), suffix.rend(), filename.rbegin() )
{
std::cout << filename << "\n";
}
++it;
}
return 0;
}

This is supported, I am told, by MSVC 2012 and Clang 3.5, with GCC 5.3 catching up later in 2015.

Later versions will probably include <filesystem> (without the "experimental"), making this the future-compatible solution.



Related Topics



Leave a reply



Submit