Visual Studio 2019 C++ and std::filesystem

Viewed 12368

Now, I would have not expected That, after 'upgrading' to a 'newer' version of the compiler.

In VS2017 std::filesystem was available through std::experimental::filesystem, Now after upgrading to VS2019 to my surprise it is not available at all. Not in std::experimental nor std::filesystem.

and YES, I've tried setting c++17 from project's settings even the 'latest draft' thing, any ideas?

5 Answers

For the sake of completeness and people searching this in the future.

To switch to C++17' std::filesystem in Visual Studio (regardless VS2019 or VS2017) you need to:

  1. change the language standard in Project properties -> Configuration Properties -> C/C++ -> Language -> C++ Language Standard to at least ISO C++17 Standard (/std:c++17)
    (Can also be found in Project properties -> Configuration Properties -> General -> C++ Language Standard)
  2. change #include <experimental/filesystem> to #include <filesystem>
  3. change in the source code all appearance of std::experimental::filesystem to std::filesystem
  4. fix the possible differences between the experimental and final filesystem versions

For all those who struggle with porting their existing Visual Studio 2017 projects into Visual Studio 2019, having proper project settings and pulling their hair out to no avail: in file VC\Tools\MSVC\14.26.28801\include\filesystem there's:

#if !_HAS_CXX17

now for why this flag is not automatically being set when changing projects settings I have no idea. Thus I've used:

 #define _HAS_CXX17 1
#include <filesystem>

in my files as a workaround. Works fine.

Update: On another system, within project's file there was

<LanguageStandard>stdcpplatest</LanguageStandard>
<AdditionalOptions>/std:c++14 %(AdditionalOptions)</AdditionalOptions>

The latter line was resulting in problems (obviously). Switching higher-level project settings does not remove such optional settings (obviously).

I hit the same issue [include filesystem] with the 2019 version (Microsoft Visual Studio Community 2019 Version 16.6.0) in spite of C++17 language selection.

I had to explicitly change the platform and the Active Solution platform to x64 in the configuration window (though, I started with x64). With this, the error is gone.

The correct solution to that is: For Xcode:

  1. go to project properties
  2. under build Settings, scroll down to Apple Clang- Language- c++.
  3. there you have to select c++ language dialect and set it to C++17[-std=c++17].

cheers Singh

Related