ar2015
ar2015

Reputation: 6170

c++17 `filesystem` is not a namespace-name

I am wondering why in the following code, the namespace filesystem is not found:

g++ -std=c++17 main.cpp -lstdc++

// #include <filesystem>   <- error, so changed to the following:
#include <experimental/filesystem>

namespace fs = std::filesystem;

int main()
{
    return 0;
}

error:

main.cpp:3:21: error: ‘filesystem’ is not a namespace-name
 namespace fs = std::filesystem;
                     ^
main.cpp:3:31: error: expected namespace-name before ‘;’ token
 namespace fs = std::filesystem;

gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)

Upvotes: 25

Views: 80249

Answers (3)

M.Ali El-Sayed
M.Ali El-Sayed

Reputation: 1779

If you are using visual studio go and change the C++ language standard

  • Right-click the project name
  • Go to the general tab and select the Iso c++ 20 enter image description here

Upvotes: -1

PiotrNycz
PiotrNycz

Reputation: 24440

<experimental/..> means experimental namespace:

namespace fs = std::experimental::filesystem;

See: http://en.cppreference.com/w/cpp/experimental/fs/path

Upvotes: 24

Miles Budnek
Miles Budnek

Reputation: 30714

GCC 5.4.0 was released in June of 2016; over a year before the C++17 standard was adopted. It and its version of libstdc++ have very limited C++17 support. You can see when GCC added C++17 language features here and when libstdc++ added C++17 standard library features here.

At the time of GCC 5.4's release, the filesystem library was not yet implemented in the std::filesystem namespace. It, along with any other <experimental/...> headers that are included in that version, are in the std::experimental namespace.

Upvotes: 31

Related Questions