neoislife
neoislife

Reputation: 69

Visual Studio Code with Bash Terminal (WSL) in C++ builds only .out files - not .exe

I'm using Visual Studio Code on Windows 10 with the Linux Subsystem. (Ubuntu)

I've created a small c++ file and when I'm building it with the bash terminal - only a .out file is created. Which is fine, but I want to debug it as well and in that case - I can only open .exe files.

When I switch to powershell from bash - the build extension is an .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;
}

I'm really not sure what to do - as I'm not able to debug .out files and I'd like to choose the build extension myself.

Upvotes: 0

Views: 2022

Answers (2)

tomdinh
tomdinh

Reputation: 198

In order to run/debug cross-platform, your have to use cross-compilers such as MinGW. You can install MinGW-w64 in both Windows, Linux (or WSL).

VScode-tools with gdb.exe from MinGW can debug *.out as well as *.exe files (Tested).

To compile 64-bit version you need to include some static depedency library for C/C++ which are libgcc and libstdc++ while compile. so the command and args in task.json should be:

"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) 
            ],

You can learn more about C/C++ Standard Library Dependencies here

Upvotes: 0

neoislife
neoislife

Reputation: 69

Found the solution:

sudo apt-get install mingw-w64

And then inside tasks.json

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

That compiles a 32 bit exe - but the 64 bit version with x86_64-w64-mingw32-g++ somehow doesn't work. Creates an invalid exe.

Upvotes: 1

Related Questions