33

i'm facing a problem with filesystem library, it should be included in c++17 compiler, after 2 days i tried to install gcc-7.0.2 in raspberry pi but it didn't work, it couldn't recognize the command gcc-7 or g++-7 or even -std=c++17 so i had to install g++-6 and gcc-6 using apt-get install anyway, after installing the 6 version the compiler include c++17. i'm using codeblocks as IDE, i had to add a new compiler and add the option -std=c++17 to enable it,but in the main code when i include the filesystem library it says no such file or directory.

my question is, how i can add the c++17 compiler and its library (like filesystem) correctly ??

einpoklum
  • 118,144
  • 57
  • 340
  • 684
shadow
  • 767
  • 3
  • 8
  • 20
  • 6
    Just because the switch says "C++17" doesn't mean that it implements *all* of C++17. – Nicol Bolas Aug 24 '17 at 17:10
  • 5
    More specifically, it can only implement the *language* parts. The *library* parts are outside the compiler's purview. – Ignacio Vazquez-Abrams Aug 24 '17 at 17:12
  • 4
    @IgnacioVazquez-Abrams: Well, the compiler switches are also used to activate the library parts, since they're included with the compiler in the distro. But you're right that libstdc++ is updated separately from gcc. – Nicol Bolas Aug 24 '17 at 17:15
  • @Nicol It's not updated seperately at all: specific GCC releases are quite tightly bound to specific libstdc++ snapshots. The version numbering works slightly different and both are developed semi-independently (as language feature implementation in the compiler permits). – rubenvb Aug 22 '18 at 14:28
  • @rubenvb If I install gcc 8 in Ubuntu, will I have 2 different libstdc++ library or merely the original one get updated? – Rick Nov 27 '18 at 14:40
  • 1
    @Rick you'll probably have two even though the newer one should work as a drop-in replacement for the old one. – rubenvb Nov 27 '18 at 15:44

2 Answers2

72

GCC v7 still does not implement <filesystem> but it does have the Filesystem Technical Specification which is in <experimental/filesystem>

#include <experimental/filesystem>

// for brevity
namespace fs = std::experimental::filesystem;

int main()
{
    fs::path p = "/path/to/my/file"; // etc...
}

This is also available in GCC v6.

To link with the library you need to add -lstdc++fs to the command line.

Note: There may be some minor differences between the current Technical Specification and the final draft of <filesystem> that is decided upon by the Standards Committee.

Note 2: GCC v8 now implements <filesystem> with the -std=c++17 flag.

Galik
  • 47,303
  • 4
  • 80
  • 117
  • so it was right that gcc v7 not completed yet, anyway, adding experimental/filesystem works fine, thank you for that. – shadow Aug 24 '17 at 17:27
  • @shadow Support for `C++17` is not complete, no. Mainly because `C++17` has not yet been finalized by the ISO Standards Committee. It will probably take a few months after that for `GCC` to become feature complete. – Galik Aug 24 '17 at 19:29
  • 2
    @Galik Why wouldn’t GCC be feature complete by the time C++17 is published? The C++17 draft has been feature-complte for months, and if I’m reading the [standardization process](https://www.iso.org/stage-codes.html#40.20) right, it won’t be published until [some time after October 30th](https://www.wolframalpha.com/input/?i=2017-06-12+%2B+20+weeks). That should be plenty of time, since they’re [done with the core language](https://gcc.gnu.org/projects/cxx-status.html#cxx1z) and [mostly done with the library](https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.201z). – Daniel H Aug 28 '17 at 19:15
  • 2
    @DanielH According to the records the `std::filesystem` did not arrive in `GCC` until `v8.1` which was published in `May 2018` which is *more than 6 months* after the `C++17 Standard` was published. Even then it was considered *"experimental"* until `GCC v9.1` which was published in `May 2019`. – Galik May 21 '20 at 10:56
  • 1
    I am using GCC 9.3 on Ubuntu 20.04 and `` did not work (even with `-std=c++17` flag). Using the experimental worked thanks to this answer. – Sasan Apr 15 '21 at 08:40
5

First you should take at look at C++17 Support in GCC

GCC 8

Runtime Library (libstdc++)

  • Improved experimental support for C++17, including the following features:
    • Deduction guides to support class template argument deduction.
    • std::filesystem implementation.
    • std::char_traits<char> and std::char_traits<wchar_t> are usable in constant expressions.
    • std::to_chars and std::from_chars (for integers only, not for floating point types).

src: https://gcc.gnu.org/gcc-8/changes.html

GCC 9

Runtime Library (libstdc++)

  • Improved support for C++17, including:
    • The C++17 implementation is no longer experimental.
    • Parallel algorithms and <execution> (requires Thread Building Blocks 2018 or newer).
    • <memory_resource>.
    • Using the types and functions in <filesystem> does not require linking with -lstdc++fs now.

src: https://gcc.gnu.org/gcc-9/changes.html

Mizux
  • 8,222
  • 7
  • 32
  • 48