18

I have updated version of gcc, gcc --version produces the following output

    gcc (Ubuntu 8.1.0-5ubuntu1~16.04) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

i can include filesystem in header file without any error

#include<filesystem>

But when i try to access the namespace filesystem like below then i get the error

namespace fs = std::filesystem;

Error message

error: ‘filesystem’ is not a namespace-name
 namespace fs = std::filesystem;

This seems to be weird since the gcc 8 has support for std::filesystem and it is not available in namespace, am i doing anything wrong in accessing std::filesystem?

and yes i built with -std=c++17

Naveen
  • 769
  • 1
  • 9
  • 28

3 Answers3

26

Add the filesystem library as an argument to your compiler that will be forwarded to the linker. Also make sure you are using C++17. Both g++ and clang++ accepts this particular format:

--std=c++17 -lstdc++fs
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • it didnt work, i have accidentally removed the namespace line when i made the previous reply, still i get the same error. – Naveen Nov 08 '18 at 05:43
  • 1
    Well, linking would probably work but you're blocked by problems that doesn't even get you that far. Try some filesystem examples on [std::filesystem @ cppreference](https://en.cppreference.com/mwiki/index.php?title=Special%3ASearch&search=std%3A%3Afilesystem) and if that doesn't work, just edit the question. – Ted Lyngmo Nov 08 '18 at 05:48
  • @Naveen Did you ever figure out what was wrong? This old answer still gets upvotes so it seems it helps others :-) – Ted Lyngmo Jun 11 '20 at 09:10
  • Unfortunately no, none of the solutions worked, i stopped programming in c++ after that :-) – Naveen Jun 11 '20 at 15:34
  • @Naveen :-D Oh no, that's unfortunate. You may be happy to know that the status of the compilers have improved greatly since then and I'm sure we could work it out if you gave C++ another chance :-) – Ted Lyngmo Jun 11 '20 at 15:37
  • If it doesn't work for you, make sure you're putting `-lstdc++fs` after the input file names, and make sure that your gcc is new enough (`gcc --version`). – serg06 Jul 31 '20 at 20:03
  • `--std=c++17` is still necessary to use parts of `` in gcc 9.3.0 (this is the version that Ubuntu 20.04 supports) , and the implementation is still not complete. I was surprised that even even after 3 years the situation is still a bit rough. Since I only need a couple things out of `` I'm considering pursuing other options (even ones that are less portable and might require a different implementation for Windows). – jrh Aug 21 '20 at 18:51
  • 1
    @jrh `--std=c++17` or `--std=c++` will be needed for as long as C++17 or later is not the default version that the compiler uses so that's hardly surprising. What part of the `` implementation is not complete in [tag:g++]? – Ted Lyngmo Aug 23 '20 at 08:24
  • @TedLyngmo The post I read that mentioned that `` was incomplete was outdated I think. The [g++ standards support page](https://gcc.gnu.org/projects/cxx-status.html) seems to say that C++17 has "almost full support". Maybe it isn't that surprising that C++17 was still not complete in 2019, maybe gcc always needed [some time](https://stackoverflow.com/a/44735016) to implement the latest standard into the compiler. Still it caught me off guard. – jrh Aug 23 '20 at 14:42
  • As an aside VS2013 has limited C++11 support, but (rather confusingly), it appears to do the opposite strategy, and opts you into using their cherry picked C++11 features unless you explicitly say you want to use a certain C++ standard. FWIW the VS 2013 strategy is more annoying to deal with in 2020. – jrh Aug 23 '20 at 14:48
  • 1
    @jrh The C++17 language support may not be 100% but what's relevant is the state of the standard [library](https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017) implementation. It seems gcc 8 has [non-conforming `basic_fstream` path overloads](https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B17_library_features) but I'm not sure if that's the case in gcc 9 or later. – Ted Lyngmo Aug 23 '20 at 16:22
  • despite not necessary anymore, seems backwards-compatible and also works with gcc9. – Croc Dialer May 29 '21 at 17:17
  • @CrocDialer People using LTS OS:es may still stumble upon this. – Ted Lyngmo May 29 '21 at 20:47
8

Because of the silly rep system, I can't make this a comment on slashmais's answer.

When using an IDE, make sure that you also set the compiler to be used to GCC8 or above.

In my case, despite being installed, CodeLite was using a lower version of GCC and causing headaches (re: not finding the header)!

Manually setting CodeLite to use gcc-8 (instead of just gcc fixed this problem.

LCWilliams
  • 148
  • 3
  • 8
0

If you are using an IDE, eg codeblocks, then make sure the default compiler flags for gcc: -std=c++17 is set, not just the current project's, but the global flags.

Had this issue with CB and setting this flag under [Settings/Compiler] fixed it (no need for adding the lib)

slashmais
  • 7,069
  • 9
  • 54
  • 80