1

Recently I've been trying to move from CodeBlocks to Visual Studio Code, but the latter has been giving me problems especially when making projects with classes, in this case when trying to run it I get this window:

the preLaunchTask C/C++:g++.exe build active file terminated with exit code -1

If I click show errors it tells me no problems have been detected in the workspace

Then after clicking debug anyway this appears:

launch program does not exist

It's been a while since I've used VScode but I'm pretty sure that the launch program gets created when running the code of the program for the first time right?

Here are my launch.json and task.json:

launch.json:

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "(Windows) Launch",
        "type": "cppvsdbg",
        "request": "launch",
        "program": "${workspaceFolder}/myfile.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true
    },
    {
        "name": "(Windows) Attach",
        "type": "cppvsdbg",
        "request": "attach",
        "processId": "${command:pickProcess}"
    }
]

}

tasks.json:

{
"version": "2.0.0",
"tasks": [
    {
        "label": "build myfile",
        "type": "shell",
        "command": "g++",
        "args": [
            "-std=c++14",
            "-g",
            "-o",
            "myfile.exe",
            "myfile.cpp"
        ],
        "group": "build"
    },
    {
        "type": "cppbuild",
        "label": "C/C++: g++.exe build active file",
        "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
        "args": [
            "-fdiagnostics-color=always",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }
]

}

I got all the .json from this answer to a similar question: https://stackoverflow.com/a/50658089/19009898 I used to have different.json which I guess were the default ones but this change doesn't seem to make any trouble for other previous code without classes that I've made so I'm guessing it's ok.

Would appreciate the help.

Chalky
  • 11
  • 5
  • `I'm pretty sure that the launch program gets created when running the code of the program for the first time right`. Not really. If the compiler command ends with error code, you need to find out why first. It might be some source code problems or your compiler is not configured properly/missing. Why you click `debug anyway`? see the errors! – The Dreams Wind Jul 31 '22 at 19:03
  • Seems likely that the compilation is failing, especially as the problem only happens some of the time. – john Jul 31 '22 at 19:50
  • If you open the terminal tab, it should contain the compiler command VSCode tries to compile your code with. It should give some clue on why it fails – The Dreams Wind Jul 31 '22 at 22:52
  • @TheDreamsWind Sorry I forgot to add that part it's now on the post, when I click `see errors` it tells me `No problems have been detected in the workspace`. – Chalky Jul 31 '22 at 22:52
  • @TheDreamsWind At the end it says `collect2.exe: error: ld returned 1 exit status` – Chalky Jul 31 '22 at 22:54
  • Solved it, had nothing to do with the .json, apparently VScode needed me to include the `.cpp` of my classes, unlike CodeBlocks which would work with only the `.h`. – Chalky Aug 01 '22 at 00:27

1 Answers1

0

Found a way in the spanish stack overflow.

In tasks.json I added this:

"label": "g++.exe build active file",
 "args": [
"-g",
"${fileDirname}\\**.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
],

Then run by either debuging or running the C/C++ file NOT just clicking run code and then it works perfectly.

Chalky
  • 11
  • 5