I'm trying to run the debugger in VSCode using Bazel on a C++ application on MacOS 11. When I try to run this, I get the error message : "Could not load source 'testcpp.cc': 'SourceRequest' not supported". I'm not sure what I need to do to resolve this...it compiles and debugs properly on the command line. Here is my BUILD file:
package(
default_visibility = ["//visibility:public"],
)
cc_binary(
name="testcpp",
srcs=["testcpp.cc"],
)
Here is my .vscode/tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"label": "test dummy app",
"type": "shell",
"command": "bazel build -c dbg --strip=never --spawn_strategy=standalone :testcpp"
},
]
}
And here is my .vscode/launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"preLaunchTask": "test dummy app",
"request": "launch",
"program": "${workspaceFolder}/bazel-bin/testcpp",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
Any help would be appreciated. Thanks!
I have the same problem and the only thing I could find was on this GitHub issue: https://github.com/microsoft/vscode-cpptools/issues/3831
I wasn't able to fix mine that way (maybe because I'm using lldb instead of gdb) but it seems like you should change the current working directory to the executable's and then add the appropriate source file map as written in the last post of the issue.
Related
I am new to cpp programming, and I am using Visual Studio Code as my IDE/editor. I created a custom build task which builds the current active file (file open in editor that I am working on). Below is my taks.json.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-10 build active file",
"command": "/usr/local/bin/g++-10",
"args": [
//"-g", // include to add debugging support
"-O2",
"${file}",
"-o",
//"${fileDirname}/${fileBasenameNoExtension}"
"build/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/local/bin/g++-10"
},
{
"type": "cppbuild",
"label": "C/C++: g++-10 run active file",
"command": "${workspaceFolder}/build/${fileBasenameNoExtension}",
"args": [],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "none",
"detail": "compiler: /usr/local/bin/g++-10"
}
]
}
and the 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++-10 - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "build/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
// "preLaunchTask": "C/C++: g++-10 build active file"
}
]
}
I am able to run the build task to generate cpp object file. But how do I run the file just built from within IDE. I essentially just want to emulate build and run for quickly checking programs. I tried creating a new task (the second item in tasks.json's "tasks"), but when run it does not works. I tried finding online but to no avail.
Currently I build the file using this tasks and then switch to terminal and run the file. This workflow is ok, but had this been achieved, I would have been able to do build and run as supported in common IDEs. Is there a way to achieve this?
Thanks!
Attaching screen snips for F5 debugging issue.
To read from standard input using a debugging session, standard input must be redirected from a text file.
To redirect standard input, you need a command like this
program_name < input.txt
So you have to add these commands to you launch
"args": [
"<",
"input.txt"
],
In launch.json change
"externalConsole": false,
to
"externalConsole": true,
I am new to Visual Studio Code. I am trying to debug a simple C++ code.
I edited my launch.json to be able to debug the app like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/Calculator",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
When I start debugging, the terminal is opened in the correct folder but the program is not executed. So the Visual Code does not stop in the breakpoints I want to check in the program.
In my task.json I have the following code:
{
"version": "2.0.0",
"tasks": [
{
"label": "Echo vars",
"command": "echo",
"args": [
"${env:USERNAME}",
"workspaceFolder = ${workspaceFolder}"
],
"type": "shell",
"problemMatcher": []
},
{
"label": "build",
"type": "shell",
"command": "g++ -g Calculator.cpp -o Calculator",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
}
Can anybody help me on this?
Thanks in advance
As mentioned in the comments: if your Calculator.cpp is not compiled with debug symbols ie. g++ called without -g flag, you will not be able to debug it.
Hence, add to your launch.json a prelaunchTask entry which will make sure your build task which is compiling your source with debug symbols is always executed prior to launching the debugger.
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "build"
edit your launch.json to add debugger log output to your project:
"logging": { "engineLogging": true, "trace": false, "traceResponse": false }
I get this message when I click on a warning. This is a screenshot of the case.
The error reads,
Unable to open 'warning.cpp': Unable to read file
'/Users/dimen/code/C++/Users/dimen/code/C++/warning.cpp'
(Error: Unable to resolve non-existing file
'/Users/dimen/code/C++/Users/dimen/code/C++/warning.cpp').
My script is located in /Users/dimen/code/C++/warning.cpp so vscode reiterates the path for some reason.
I suspected that the linter setting must've been written erroneously but I'm not sure where I should edit. As some side notes,
Using Microsoft's C/C++ extension.
tasks.json have been customized so that all the builds go inside the build folder
I was facing this issue also.
For resolving this issue, I closed the VSCode and again imported folder again as Path of folder got changed in mine conditions.
If this doesn't work, you can uninstall VSCode and then reinstall it.
You need to edit the problemMatcher part of tasks.json so that it has a variable called fileLocation that is set to absolute. Here is an example of what it should look like:
"problemMatcher": {
"base" : "$gcc",
"fileLocation" : "absolute"
}
I hope this helps.
Check this link. It seems to require detailed configurations for the directory. In task.json the problemMatcher takes the file directory as relative so you got repeating path. Setting "fileLocation" to "absolute" works on my laptop.
For me this worked in tasks.json:
{
"tasks": [
{
//"type": "cppbuild",
"label": "build my project",
"command": "/home/user/path/build.bash",
"args": [
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": {
"base": "$gcc",
"fileLocation": "absolute",
},
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
For some reason i had to specifically remove "type": "cppbuild" for it to work.
I fixed this issue by making a change in 'launch.json' file. I changed the value of "cwd" below "name": "(gdb) Launch eds" in "configurations" field. I set it to the absolute path of the folder containing the project. Below is my launch.json file. I'm using VS Code on Ubuntu.
{
// 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 eds",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build_x86_debug/bin/fcc",
"args": ["-a=${workspaceFolder}/build_x86_debug/bin/", "-d=${workspaceFolder}/build_x86_debug/bin/", "-c=${workspaceFolder}/build_x86_debug/bin/"],
"stopAtEntry": false,
"cwd": "/home/aiden/dev2",
"environment": [{"name": "LD_LIBRARY_PATH", "value": "/usr/lib:${workspaceFolder}/build_x86_debug/bin:${workspaceFolder}/pocolib1.7.5"}],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Launch",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/build_x86/bin/emvadapter",
"cwd" : "/home/vendor",
"env" : { "LD_LIBRARY_PATH":"/home/vendor/lib" }
},
{
"name": "Unit Tests",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/build_x86/bin/",
"linux": { "cwd": "${workspaceFolder}/vendor"}
}
]
}
I got this too on ApiLogicServer, which uses this feature extensively.
Took a few, but this resolved it by clicking container (lower left) and then rebuild-container:
I am trying to setup visual studio code to build/run and debug c++ files using g++ compiler on macOS. However when I build or debug the code, I can see that output file is created and i can run it as well however i am not able to debug it in vscode, for debugging gives weird behaviour.
So far I have been able to write tasks which builds the .cpp file and and executes it. The output comes into the terminal of vscode. However when I try with debug a new Terminal opens and no breakpoint is hit.
Here is the tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with g++",
"type": "shell",
"command": "g++",
"args": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "run",
"type": "shell",
"command": "cd ${fileDirname}/ && ./${fileBasenameNoExtension}",
"dependsOn": ["Build with g++"]
}
]
}
and here is my launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"showDisplayString": false,
"environment": [],
"externalConsole": true,
"internalConsoleOptions": "openOnSessionStart",
"MIMode": "lldb",
"logging": {
"moduleLoad": false,
"programOutput": true,
"trace": false
},
"preLaunchTask": "Build with g++",
"osx": {
"MIMode": "lldb"
}
}
]
}
I expect that when i hit debug or press f5 the terminal in the vscode should run and I am able to debug the program.
As #alan-birtles pointed out adding -g would be the solution.
Also I found out here that there is no way to use integrated terminal of vscode for c++ as lldb doesn't support it.
Here are the final configuration.
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"showDisplayString": false,
"environment": [],
"externalConsole": true,
"internalConsoleOptions": "openOnSessionStart",
"MIMode": "lldb",
"logging": {
"moduleLoad": false,
"programOutput": true,
"trace": false
},
"preLaunchTask": "Build with g++",
"osx": {
"MIMode": "lldb"
}
}
]
}
task.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with g++",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-Wall",
"-Wextra",
"-Wpedantic",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "run",
"type": "shell",
"command": "cd ${fileDirname}/ && ./${fileBasenameNoExtension}",
"dependsOn": ["Build with g++"]
}
]
}
Because your VSCode Debugger Extension has bugger.
Successfully Setup C++ Debug with VSCode on mac:
I use clang/gcc as compiler, Code-Runner(VSCode Extension) as runner, C/C++ Clang Command Adopter(VSCode Extension) as to provide static detection, CodeLLDB(VSCode Extension) to debug C++.
Do Not Download Microsoft C/C++ (Identifier: ms-vscode.cpptools, for C/C++
IntelliSense, debugging, and code browsing. Microsoft Officially produced Extension), Because This CPPtools and that CodeLLDb conflict.
To avoid any issues, please do not download any VSCode Extensions yet.
Here is my configuration about debug C++ with VSCode on mac:
VSCode v1.63.2;
Code Runner v0.11.6 (Author: Jun Han)
C/C++ Clang Command Adapter v0.2.4 (Author: Yasuaki MITANI)
CodeLLDB v1.4.5 (Author: Vadim Chugunov)
Clang v9.0.0 gcc v4.2.1 Intel CPU macOS 10.12
Details of successful configuration refer to here:
Debug C++11 manually with VSCode(mac)
I am trying to set up the VScode with C++ Compiler
By following this tutorial
enter link description here
and the problem I got is (By running the code with code runner)
'g++' is not recognized as an internal or external command,
operable program or batch file.
enter image description here
and if I run with the debug option I got this problem
The preLaunchTask 'build & run file' terminated with exit code 1.enter image description here
This is my "c_cpp_properties.json"
{
"configurations": [
{
"name": "Win32",
"intelliSenseMode": "clang-x64",
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=7",
"__stdcall=attribute((stdcall))",
"__cdecl=__attribute__((__cdecl__))",
"__cplusplus=201703L"
],
"includePath": [
"${workspaceFolder}/include",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/backward",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../include",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include-fixed",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/include"
],
"browse": {
"path": [
"${workspaceFolder}/include",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/backward",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../include",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/include-fixed",
"C:/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/include"
],
"limitSymbolsToIncludedHeaders": false,
"databaseFilename": ""
},
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
This is my "launch.json"
{
"version": "0.2.0",
"configurations": [
{
"name": "Run C/C++",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/MinGW64/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "build & run file"
},
{
"name": "Debug C/C++",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/MinGW64/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "build & debug file"
}
]
}
And this is my "tasks.json"
{
"version": "2.0.0",
"tasks": [
{
"label": "build & debug file",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-o",
"${fileBasenameNoExtension}",
"${file}"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "build & run file",
"type": "shell",
"command": "g++",
"args": [
"-o",
"${fileBasenameNoExtension}",
"${file}"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I just want to study C++ and I try a lot of way on the Internet for days and I still can't fix it Please help.
Thank you.
You have to add g++ to your PATH variable.
First, find out if you have installed g++ and find where it is located.
You have not mentioned what OS you are using.
If you are using Windows, it may be located in C:\mingw or C:\Program Files\mingw.
If you haven't installed g++, you can install it via https://sourceforge.net/projects/mingw-w64/
Then open System Properties -> Advanced -> Environment Variables.
Then under Environment Variables for <username> choose PATH and click on Edit.
Click on New and then click on Browse and find the bin directory in your mingw installation and add that to the path and you're done.
Restart VSCode if you have it open and it would automatically find g++.
[Sloved] I had the same problem and I solved it:
Simply add a new path as: "C:\MinGW\bin\g++.exe"
Now reopen your VS code and you are done:
You have to add mingw.
First, find out if you have installed g++ and find where it is located.
First copy mingw file in c drive.
mingw download link: https://drive.google.com/file/d/112lN_esKLgOJ-bXbw7iGgbTOU5wKfW-_/view?usp=sharing
1
If you are using Windows, it may be located in C:\mingw\bin .
2
Then open System Properties -> Advanced -> Environment Variables -> Edit
3
Then click New -> paste the link C:\mingw\bin then OK.
4
Restart VSCode if you have it open and it would automatically find g++
First, open this page and install and Configure Mingw on your System:
http://www.mingw.org/wiki/getting_started
after download and install, you must Add Mingw->bin to your paths with this Link:
https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10/
then to ensure, type g++ --version in CMD or gcc --version
then you can use this Link for Your purpose.
for more help, use
https://code.visualstudio.com/docs/cpp/config-mingw
this Link probably is very useful for you