c++cudavisual-studio-2019

How to enable C++17 code generation in VS2019 CUDA project


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?


Solution

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

    Alternatively, it may also be possible to enable the desired host C++ level here:

    enter image description here