【问题标题】:When writing a C++, if the source file is being saved, compile it写C++时,如果源文件正在保存,编译它
【发布时间】:2022-12-04 19:46:45
【问题描述】:

我有一个问题:当我在VsCode中保存一个C++源文件时,我总是需要通过this command运行一个任务,然后:this one,翻译成英文就是:"Compile this C++ active file using g++ compiler"。我想知道是否有办法确保如果文件是saved,它也会是compiled。我试图搜索所有可能的东西,但我真的无法提交有用的东西,而且我对 .json 语言不是很熟悉。

信息:

代码编辑器: Visual Studio Code

任务语言: .json

编译器: g++版本2.0.0

用于编译的终端: Windows PowerShell

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++.exe compila il file attivo",
      "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": ["$gcc"],
      "group": "build",
      "detail": "compilatore: C:\\msys64\\mingw64\\bin\\g++.exe"
    }
  ]
}

正常程序是保存文件,使用我在上面放置的命令,然后从终端运行代码。

【问题讨论】:

  • 可能有一些方法可以做到这一点,但我认为这是一个相当糟糕的主意:你应该养成强迫性地保存代码的习惯每时每刻(我每隔几次击键就保存一次!)而且大多数情况下,保存文件的状态将不是有效的、可编译的 C++ 代码。
  • 抱歉,我想知道您的要求。我没有使用 VS Code,而是使用 Visual Studio。如果您在 Visual Studio 中构建,所有修改过的文件之前都会自动保存。 (这对我来说很有意义。)但我也可以保存文件(因为我想离开办公桌或切换到另一个项目),同时知道当前正在进行的工作既不可编译也不可运行。因此,自动构建保存对我来说没有多大意义。

标签: c++ json g++


【解决方案1】:

是的,绝对可以在保存文件时在 VSC 中自动运行 build 任务。您可以在 tasks.json 文件中使用“保存时构建”设置:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++.exe compila il file attivo",
      "command": "C:msys64mingw64ing++.exe",
      "args": [
         "-fdiagnostics-color=always",
         "-g",
         "${file}",
         "-o",
         "${fileDirname}${fileBasenameNoExtension}.exe"
       ],
       "options": {
          "cwd": "${fileDirname}"
        },
        "problemMatcher": ["$gcc"],
        "group": "build",
        "detail": "compilatore: C:msys64mingw64ing++.exe",
        // Add this setting to automatically run the build task when a file is saved
        "isBuildCommand": true
       }
   ]
}

【讨论】:

  • 在您的新代码行中,文件显示:不允许使用 Property isBuildCommand。
猜你喜欢
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
  • 2014-01-24
  • 2010-10-23
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多