10

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?

Vega4
  • 969
  • 1
  • 11
  • 25
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/215506/discussion-on-question-by-vega4-visual-studio-2019-c-and-stdfilesystem). – Samuel Liew Jun 08 '20 at 09:43

5 Answers5

25

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
user1810087
  • 5,146
  • 1
  • 41
  • 76
  • 3
    It still doesn't work this way. Even in default console "Hello World" project with all the above steps applied. – Amphyby Jan 17 '21 at 08:44
  • @Amphyby could you elaborate? – user1810087 Jan 17 '21 at 21:44
  • I am having this issue in VS 2019 Community 16.7.3 in a newly created project. It appears that _HAS_CXX17 is not being set for some reason in this version with the #1 step being applied. – drescherjm Mar 12 '21 at 15:10
  • @drescherjm I cannot reproduce the issue. Nor with version 16.9.3 Community or even after installing version 16.7.3 Professional from [Visual Studio 2019 Releases](https://learn.microsoft.com/en-us/visualstudio/releases/2019/history#installing-an-earlier-release). `_HAS_CXX17` is defined and a simple example code `int main(int argc, char* argv) { std::cout << _HAS_CXX17 << ":" << std::filesystem::current_path().u8string().c_str(); return 0; }` compiles and works as expected. Note, the version you mentioned is from November 2020, maybe try the current version 16.9.3. – user1810087 Apr 11 '21 at 20:14
  • @drescherjm: a bit late to the partty, but I'm guessing you updated the settings for one build config (e.g. Release), but not the other(s). – Ed S. Aug 29 '21 at 18:51
7

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

Vega4
  • 969
  • 1
  • 11
  • 25
  • on another system it did not do the trick and more errors reported within 'filesystem' itself, definitely something with project settings stuff that should be taken care of by VS the right way... – Vega4 Jun 08 '20 at 09:22
  • 2
    Visual Studio does have some issues with the defines. Even though [Microsoft claims to have fixed it](https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/), my test still give me the value **199711** for the define `__cplusplus` ... However, it also could be a misconfiguration or some code preventing define `_HAS_CXX17` (found in **vcruntime.h**) from being defined properly. You could search your project for things like __cpluplus and _HAS_CXX17. – user1810087 Jun 08 '20 at 09:36
  • Additional options can be found in *Project Settings -> Configuration Properties -> C/C++ -> Command Line -> Additional Options* – user1810087 Jun 08 '20 at 09:46
  • 2
    One should not be setting _HAS_CXX17, that's supposed to be controlled by `/std:` – Billy ONeal Jun 08 '20 at 18:52
  • 2
    i have a problem also , using VS2019 , the _HAS_CXX17 was working and suddenly VS can't find it changed to cpp 17 in config and still getting the error – user63898 Feb 03 '21 at 06:27
3

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.

greybeard
  • 2,249
  • 8
  • 30
  • 66
0

You will have to update your Visual C++ redistributable if you haven't. Then under project properties > Configuration Properties > C++ Language Standard: Select C++17 or higher. You could try setting it to that by default. See this:How to change default C++ language standard in Visual Studio 2019?

-2

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