Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2019 C++ and std::filesystem

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?

like image 373
Vega4 Avatar asked Jun 08 '20 07:06

Vega4


3 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
like image 197
user1810087 Avatar answered Sep 24 '22 04:09

user1810087


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).

like image 25
Vega4 Avatar answered Sep 23 '22 04:09

Vega4


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.

like image 21
particlereddy Avatar answered Sep 25 '22 04:09

particlereddy