使用C ++中的Bash终端(WSL)的Visual Studio代码仅构建.out文件 - 而不是.exe

时间:2018-05-05 00:11:33

标签: c++ bash visual-studio-code windows-subsystem-for-linux

我在Windows 10上使用Visual Studio Code和Linux子系统。 (Ubuntu的)

我创建了一个小的c ++文件,当我用bash终端构建它时 - 只创建一个.out文件。这很好,但我想调试它,在那种情况下 - 我只能打开.exe文件。

当我从bash切换到powershell时 - 构建扩展名为.exe

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "echo",
      "type": "shell",
      "command": "g++",
      "args": [
        "-g", "main.cpp"
      ],
      "group": { "kind": "build", "isDefault": true }
    }
  ]
}

launch.json for debugging 

{
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/a.exe", // .out doesn't work here
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
      "preLaunchTask": "echo",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }

main.cpp

#include <iostream>

int main() {
  std::cout << "hello a" << std::endl;
}

我真的不知道该怎么做 - 因为我无法调试.out文件而且我想自己选择构建扩展程序。

2 个答案:

答案 0 :(得分:1)

找到解决方案:

sudo apt-get install mingw-w64

然后在tasks.json里面

"command": "i686-w64-mingw32-g++"

编译一个32位的exe - 但是x86_64-w64-mingw32-g++的64位版本在某种程度上不起作用。创建一个无效的exe。

答案 1 :(得分:0)

为了运行/调试跨平台,您必须使用跨编译器,例如MinGW。您可以在Windows,Linux(或WSL)中都安装 MinGW-w64

来自MinGW的带有gdb.exe的

VScode-tools 可以调试*.out*.exe文件(已测试)。

要编译64位版本,您需要包含一些C / C ++静态静态库,它们在编译时为 libgcc libstdc ++ 。因此task.json中的commandargs应该是:

"command": "x86_64-w64-mingw32-g++",
"args": [
           "-g",                  //Need for debug and compatibility
           "-static-libgcc",      //flag for the libgcc
           "-static-libstdc++",   //flag for the libgc++
           "helloworld.cpp",      //C++ source code
           "-o",                  //flag for output
           "a2.out"               //Output filename and extension (can be .exe) 
            ],

您可以了解有关C / C ++标准库依赖项here

的更多信息