VScode 使用C/C++插件(不是Code Run插件)如何指定.exe文件生成路径

只需修改默认生成的tasks.json和launch.json中的两行配置即可。

1. tasks.json中的修改

将args中的 ${fileDirname}\\${fileBasenameNoExtension}.exe 修改为 :
${workspaceFolder}/build/${fileBasenameNoExtension}.exe

"args": [
        "-g",
        "${file}",
        "-o",
        // "${fileDirname}\\${fileBasenameNoExtension}.exe"
        "${workspaceFolder}/build/${fileBasenameNoExtension}.exe"
      ],

2. launch.json中的修改

将configurations中的program的值 ${fileDirname}\\${fileBasenameNoExtension}.exe, 修改为:
${workspaceFolder}/build/${fileBasenameNoExtension}.exe

"configurations": [
    {
      "name": "gcc.exe - 生成和调试活动文件",
      "type": "cppdbg",
      "request": "launch",
      // "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
      "program": "${workspaceFolder}/build/${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "D:\\IDE\\VScode\\mingw64\\bin\\gdb.exe", // 注意此处为本机的gdb.exe路径,需要自己配置
      "setupCommands": [
        {
          "description": "为 gdb 启用整齐打印",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "C/C++: gcc.exe build active file"
    }
  ]

版权声明:本文为SMapleCR原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。