Cannot run multiple C++ files in Visual Studio Code [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Whenever I try to run my code if I have more than one .cpp file, I get the following message: The preLaunchTask 'C/C++: g++.exe build active file' terminated with code -1.
I can debug anyway, show errors or abort. If I press show errors, nothing happens, it doesn't take me anywhere. If i press debug anyway, I get another message saying: lauch: program 'c:\Users\me\HelloWorld\program.exe' does not exist. I can either cancel, or press open launch.json. If I press the latter, it takes me to the launch.json file as expected, to the line with "name": "g++.exe - Build and debug active file",. I use the Mingw-w64 compiler.
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": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

To build a program that consists of multiple files you need to use sort of a build system. Two most popular ones are make and cmake. You need to get more familiar with them. make comes with mingw. cmake needs to be downloaded and installed separately, and finally also requires either make, ninja or full version of Visual Studio.
Alternatively, you can try to use Visual Studio 2019, where you just add source files to project and VS takes care of compiling everything on its own. However note that VS2019 does not support Mingw/gcc, and uses Microsoft C++ compiler instead. Depending on your task it may or may not work for you.

Related

I can't debug C in VSCode/CodeBlocks or Any IDE

This is my 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": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin\\gdb32.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]
}
It was GDB.exe here earlier, But it said it couldn't find it , Even I couldn't find it..
So I put gdb32.exe as i was able to find that out , But it still didn't debug anything
i am fed up of this problem now...
Code Blocks says Please specify executable path,It can't be found
Please help, Thanks
Code::Blocks can be installed without MinGW, which is okay as MinGW-w64 is much better maintained. But MinGW-w64 needs to be installed separately.
You can download a standalone MinGW-w64 from https://winlibs.com/ and the site also explains on how to add this compiler and debugger to the settings.

VSCode not compiling c++

Im running Ubuntu MATE and i program in C++. I wanted to transfer to vscode so i installed it. But overall i cant really compile or debug anything. This works in terminal to run a code.
g++ HelloWorld.cpp -o HelloWorld.o
./HelloWorld.o
But if i want to go through button to compile this thing happens. What should i do? Im adding launch.json down below.
{
// 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": "cpp - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: cpp build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
If anybody in the future wonder what was the solution, compile c++ under linux with g++ and then press ctrl+f5. That one solved my problem, couldnt figure it out for 2 days.

dbg not working on VScode with WSL2 - Unable to resolve non-existing file 'vscode-remote://wsl+ubuntu-20.04..'

I've installed WSL2 and build tool and everything is working well on linux console (including a test c++ program I was able to debug with dbg).
I'm now trying to have a working development environment on my windows10 machine, by installing VSCode and a couple of extensions (c/c++ and Remote WSL).
Build works just fine, but when I try to debug, after executing (F10) a couple of lines I get the error:
Unable to open 'libc-start.c': Unable to read file 'vscode-remote://wsl+ubuntu-20.04/build/glibc-YYA7BZ/glibc-2.31/csu/libc-start.c' (Error: Unable to resolve non-existing file 'vscode-remote://wsl+ubuntu-20.04/build/glibc-YYA7BZ/glibc-2.31/csu/libc-start.c').
From that point on debugger basically doesn't work anymore, every time I press F10 (or F11) I get a new popup with the same error
screenshot of error and dev environment
Pressing the "Create File" button results in an "Unable to write file 'vscode-remote://wsl+ubuntu-20.04...." (same file as above).
here following my launch.json file
{
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
and my tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C++ Compile",
"command": "/usr/bin/g++",
"args": ["-g","*.cpp","-o","${fileDirname}/${fileBasenameNoExtension}"],
"options": {"cwd": "${workspaceFolder}"},
"problemMatcher": ["$gcc"],
"group": {"kind": "build","isDefault": true}
}
]
}
I've searched quite a bit before posting. Other have had similar problems in older posts but most of them claimed that being an old bug and they say the problem was solved with the upgrade. I'm running latest version of everything I'm using (including VSCode and the plugins).
thank you in advance to anyone helping out.
Adding
"sourceFileMap": { "//wsl$/Ubuntu-20.04" : "/" }
to the launch.json resolved the problem for me.

Proble in debuging c++ code with visual studio code, with lunch.json file

I have Problem in debugging c++ programs in Visual studio code. Although, debugging was working before. I had not change any configuration or setting. When i opened visual studio code today then i saw that error.
Image
lunch.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": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
I have not changed anything in system/vc code/ configuration etc.
Then, why debugging stops working.
The program setting specifies the program you want to debug. Here it is set to the active file folder ${fileDirname} and active filename with the .exe extension ${fileBasenameNoExtension}.exe, which if helloworld.cpp is the active file will be helloworld.exe.
The main problem is vccode is not able to find the current file for debugging.
These are some links that might work for you.
Docs
Configuration

Compile AND run c++ from VS Code (macOS)

I'm trying out c++ for the first time - using VS Code, as I like that text editor.
I've followed this tutorial to set it up: https://code.visualstudio.com/docs/cpp/config-clang-mac
I have got the following tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",
"type": "shell",
"command": "clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"helloworld.cpp",
"-o",
"helloworld.out",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
The compilation works fine and I can run the .out-file from the terminal. But I would like to reduce those 2 steps to 1 (compile AND run). Is that possible?
I've heard of the Code Runner extension, but would like to try to set it up in the tasks-file - also to learn a little on how it works.
Anyways - Any hints and suggestions are most welcome! I'll post here if I figure it out on my own!
What you need to do is configure a launch task in your launch.json and add your build task to the prelaunch tasks. Mine looks like this (I use this on Windows with minGW, but on macOsX it should work the same). This launches your program with your debugger and executes your build task first. You don't need any extensions apart from the Microsoft C++ one to run and even have full debug support with breakpoints, etc.
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "Path to your executable",
"args": [],
"stopAtEntry": false,
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "Path to your debugger",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Name of your Build Task"
}
]
}