Skip to content

Cpp2 setup: Visual Studio

Herb Sutter edited this page Oct 19, 2023 · 16 revisions

At the end of my CppCon 2023 talk, I showed debugging Cpp2 in Visual Studio. Several people asked how I set it up. Here's what I did.

Overview:

  • Add the .cpp2 file to the project, and ensure the .cpp is in C++20 mode
  • Tell VS to build that file using a custom build tool to invoke cppfront (I assume you already have cppfront installed)
  • That's it, once it builds the IDE and debugger should just pick up the error messages and debug step through the .cpp2 file

Step by step

1. Add the .cpp2 file to the project, and ensure the .cpp is in C++20 mode

In the Solution Explorer, I just right-clicked on Source Files and picked Add to add the file to the project.

And then also in Solution Explorer, right-click on the .cpp file Properties and make sure it's in C++20 (or C++latest) mode.

2. Tell VS to build that file using a custom build tool to invoke cppfront

In Solution Explorer I could right-click on the .cpp2 file and select Properties:

And add the custom build tool:

Note that I had to also tell it that the custom build tool produced the .cpp file.

Also, be sure to put the /cppfront/include directory on your INCLUDE path, in Solution Explorer right-click the app and select Properties:

And add it to the VC++ Directories > Include Directories:

That's it

That's all I did, and all that was just to invoke cppfront to build.

The IDE just picks up the rest from:

  • the cppfront error messages in filename(line, col) format which VS recognizes and merges any diagnostic output in the normal Errors window (if your IDE prefers filename:line:col just use the cppfront -format-colon-errors command line option)
  • the #line directives cppfront emits in the generated .cpp file so the debugger knows to step through the .cpp2 file (#line emission is on by default, but if you choose -clean-cpp1 these will be suppressed and then the debugger will step through the generated C++ code instead)
  • every "syntax 2" type/function/object is still just an ordinary C++ type/function/object, so the debugger visualizers just work (including for all the std:: types which are used directly as usual, and any custom visualizers you have for your own types or popular library types)