3

I am moving some code from VS2017 on one pc to another pc with VS2019. Everything is fine excepted that I cannot use std::filesystem. In my former code, I was using C++14 and had std::experimental::filesystem. In the new code, I want to move to C++17 so I changed to std::filesystem (as shown in my code below). The weird thing is that intellisense (not sure it is the right name of the thing) shows no error. It even displays filesystem when I type std::f...

enter image description here

But the code won't build and give the error "namespace "std" has no member "filesystem"".

I changed C++ Language Standard to c++latest, VS2019 version is Community 16.6.5.

#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
    std::string path = "C:\\";
    for (const auto& entry : fs::directory_iterator(path))
        std::cout << entry.path() << std::endl;
}

EDIT: The last line of my initial question might not have been clear enough: I already changed "C++ Language Standard" to C++17 or C++latest.

EDIT: As requested, the output: enter image description here

Thanks to @drescherjm, we found that it is a Cuda issue. Any idea from a Cuda specialist?

Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
Philippe Huber
  • 147
  • 1
  • 9
  • 5
    Did you specify `/std:c++14`, `/std:c++17` or `/std:c++latest`? –  Jul 22 '20 at 14:32
  • 2
    You should set your C++ language standard to C++17 in Project Property - C/C++ - C++ Language Standard to utilize C++17's features. – John Park Jul 22 '20 at 14:33
  • I already did that as mentioned at the end of my message – Philippe Huber Jul 22 '20 at 14:40
  • Do you use VS2019 platform toolset, or some older toolset, like VS2015 XP toolset? – Alex Guteniev Jul 22 '20 at 14:46
  • Is this an actual error or an Intellisense error? Can you show the exact text of the error message from the Output Tab? And yes I mean Output Tab and not the Errors List. – drescherjm Jul 22 '20 at 14:47
  • 3
    It seems the error is from the Cuda compiler and not Visual Studio. I am not familiar with that but I would not expect cuda to be dealing std::filesystem. – drescherjm Jul 22 '20 at 14:53
  • @drescherjm You are right, I got rid of it and it works now. I have now to figure out why Cuda is messing it up. Any idea? – Philippe Huber Jul 22 '20 at 14:55
  • I added the tag for cuda hoping to attract people more familiar with cuda. – drescherjm Jul 22 '20 at 14:57
  • 1
    @drescherjm Thanks a lot for your help! – Philippe Huber Jul 22 '20 at 14:57
  • There is no CUDA compiler in this case, just VS2019. The code is passed straight to the host compiler. All that can possibly happen is a change in passed through compiler settings – talonmies Jul 22 '20 at 15:12
  • @talonmies, I have no idea. THe only thing I can say is that it compiles well outside Cuda. – Philippe Huber Jul 22 '20 at 15:27
  • You mean it compiles well outside a CUDA project. If the code has a .cpp extension, it gets passed straight through to the normal VS compiler. All that can be happening is that the C++17 flag is getting overwritten by another flag. Check the compiler log carefully – talonmies Jul 22 '20 at 15:50
  • 2
    Currently you have it set up as a CUDA project (which is fine). In that case, in VS2019 go to Project..Properties..Configuration Properties...CUDA C/C++...Command Line Then you will see a box in the right hand side bottom of the dialog labelled "Additional Options". In that box, type the following: `-std=c++17 -Xcompiler "/std:c++17 "` then click "Apply" then rebuild. – Robert Crovella Jul 22 '20 at 20:18
  • 1
    The above statement works correctly whether the file ends in .cpp or .cu, if you are building a CUDA project. However if you are not building a CUDA project, then the method to enable C++17 support is [different](https://stackoverflow.com/questions/41308933/how-to-enable-c17-compiling-in-visual-studio). – Robert Crovella Jul 22 '20 at 20:28
  • @RobertCrovella, thanks a lot! It worked like a charm. – Philippe Huber Jul 23 '20 at 14:52

1 Answers1

6

Using CUDA 11, the nvcc compiler-driver is capable of supporting usage of certain C++17 language features. Currently, in VS2019, this doesn't appear to be the default behavior.

The following method should work to enable C++17 support when compiling a cuda project in VS2019:

go to Project..Properties..Configuration Properties...CUDA C/C++...Command Line Then you will see a box in the right hand side bottom of the dialog labelled "Additional Options". In that box, type the following:

-std=c++17 -Xcompiler "/std:c++17"

then click "Apply" then rebuild. Project Properties

(These instructions may change for future versions of CUDA or future versions of Visual Studio.)

Note that this method applies to CUDA projects only (i.e. when nvcc is invoked for compilation), and should be workable whether your code is in a file ending in .cpp or in a file ending in .cu. For non-CUDA projects, this may be helpful.

The std::filesystem features appear to require C++17. The CUDA nvcc compiler-driver is documented here.

Robert Crovella
  • 143,785
  • 11
  • 213
  • 257