1

I am able to use Linux command line to build a cppfront hello world program with g++-12. I have installed VS Code with 'cpp2 (cppfront) Syntax Highlighting v0.0.2' & 'C/C++ Extension Pack v1.3.0' extensions.

I'm looking to build and run this example and other programs within VS Code on Linux. How can I build and run cppfront programs with g++-12 (which is not the default installed version of g++) within VS Code?

Input for hw.cpp2 program build:

3rd/
    cppfront/
        include/
            cpp2util.h
inc/
src/
    hw.cpp2
        main: () = std::cout << "Hello, world\n";

Intermediate build results (only needed during build not for deliverable product files).

tmp/
    hw.cpp
        #include "cpp2util.h"
        auto main() -> int;
        auto main() -> int { std::cout << "Hello, world\n";  }

Exportable build results:

exp/
    hw

I would expect VS Code to handle new .cpp2 files in src/ w/o having to manually add each one to some configuration file.

CW Holeman II
  • 4,661
  • 7
  • 41
  • 72

1 Answers1

0

Here's the basic build-task + launch config approach that is used for these zero-buildsystem toy-example programs in the tutorials in the VS Code user docs for C++.

Put the following in the tasks property of your .vscode/tasks.json:

{
   "label": "cppfront",
   "type": "process",
   "command": "${workspaceFolder}/3rd/cppfront/cppfront",
   "args": [
     "-o", "${workspaceFolder}/tmp/hw.cpp",
     "${workspaceFolder}/src/hw.cpp2",
   ],
   "presentation": {
      "reveal": "always",
   },
},
{
   "label": "g++-12",
   "dependsOn": ["cppfront"],
   "type": "process",
   "command": "g++-12",
   "args": [
      "-std=c++23", // or whatever greater version you're using
      "-isystem", "${workspaceFolder}/3rd/cppfront/include",
      "-I", "${workspaceFolder}/inc",
      "-o", "${workspaceFolder}/exp/hw",
      "${workspaceFolder}/tmp/hw.cpp",
   ],
   "presentation": {
      "reveal": "always",
   },
},
{
   "label": "build",
   "dependsOn": ["cppfront", "g++-12"],
   "dependsOrder": "sequence",
   "group": "build",
}

Put this in the configurations property of your .vscode/launch.json:

{
   "name": "hw",
   "type": "cppdbg",
   "request": "launch",
   "program": "${workspaceFolder}/exp/hw",
   "cwd": "${workspaceFolder}/tmp/", // up to you what to put here
}

Modify those configurations as needed. The tasks.json part just uses plain VS Code features. The launch config's cppdbg type relies on that feature from the VS Code Cpptools extension.


Especially for non-toy projects, You might also be interested in Alex Reinking's https://github.com/modern-cmake/cppfront.

Unsolicited note: I'm more of a fan of the Pitchfork Layout.

CW Holeman II
  • 4,661
  • 7
  • 41
  • 72
starball
  • 20,030
  • 7
  • 43
  • 238