c++ 如何在Linux上的VS Code中使用g++-12构建和运行cppfront程序?

qxsslcnc  于 2023-05-20  发布在  Linux
关注(0)|答案(1)|浏览(109)

我能够使用Linux命令行来构建一个cppfront hello world程序,使用g++-12。我已经安装了VS Code和'cpp 2(cppfront)Syntax Highlighting v0.0.2' & 'C/C++ Extension Pack v1.3.0'扩展。
我希望在Linux上的VS Code中构建和运行此示例和其他程序。如何在VS Code中使用g++-12(这不是g++的默认安装版本)构建和运行cppfront程序?
hw.cpp2程序构建的输入:

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

中间构建结果(仅在构建过程中需要,而不用于可交付产品文件)。

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

可导出的构建结果:

exp/
    hw

我希望VS Code能够处理src/ * 中的新.cpp2文件,而不必手动将每个文件添加到某个配置文件 * 中。

vi4fp9gy

vi4fp9gy1#

下面是基本的构建任务+启动配置方法,用于VS Code用户文档中的教程中的这些零构建系统玩具示例程序。

  • 这假设您将cppfront可执行文件构建到cppfront存储库的根目录。

在.vscode/tasks.json的tasks属性中添加以下内容:

{
   "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",
}

将以下内容放在.vscode/launch.json的configurations属性中:

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

根据需要修改这些配置。tasks.json部分只使用普通的VS Code特性。启动配置的cppdbg类型依赖于the VS Code Cpptools extension的该特性。
特别是对于非玩具项目,您可能也会对Alex Reinkinghttps://github.com/modern-cmake/cppfront感兴趣。
非征集性备注:我更喜欢the Pitchfork Layout

相关问题