I cannot run my code in the external terminal in VSCode macOS - c

I am trying to run my C codes in VSCode on a macOS machine. The only problem is I cannot figure out how to make the code run in the external terminal.
launch. json
{
"version": "0.2.0",
"configurations": [
{
"name": "gcc build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "gcc build active file"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "compile and run C",
"type": "shell",
"command": "gcc ${file} -o ${fileBasenameNoExtension} && ./${fileBasenameNoExtension} ",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
This is also the settings.json file which i tweaked a little bit.
{
"window.zoomLevel": 0,
"code-runner.runInTerminal": true,
"C_Cpp.updateChannel": "Insiders",
"terminal.integrated.rendererType": "dom",
"editor.fontSize": 14,
"workbench.tree.indent": 20,
"workbench.tree.renderIndentGuides": "always",
"workbench.editor.highlightModifiedTabs": true,
"terminal.explorerKind": "external",
"terminal.external.osxExec": "Terminal.app"
}

Related

Use gdb in vscode with -lm flag

As of https://code.visualstudio.com/docs/cpp/launch-json-reference, I can only attach to an already existing binary with vscode (in the example being a.out), but I would like to have it in a way so that I can debug without having to compile manually first (with the -lm flag). How would I go about that?
Figured it out. Instead of only using a launch.json, I also used a task.json as follows in order to build the c file first:
{
"version": "2.0.0",
"tasks": [
{
"label": "build C file",
"type": "shell",
"command": "gcc -g -o ${workspaceFolder}/sum/main ${file} -lm",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
In the launch.json, I use the task as a preLaunchTask:
{
"version": "0.2.0",
"configurations": [
{
"name": "C Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/sum/main", // Dummy value
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "build C file",
}
],
}
More info on tasks here: https://code.visualstudio.com/docs/editor/tasks

ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". VSCode with Ubuntu-22.04 using WSL

So I was trying to debug my C code using VSCode in WSL (Ubuntu 22.04) and after using different methods, I always get the same error.
Firstly I tried following this guide from this question,
Idk if I did something wrong, heres the "launch.json":
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
},
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/bin/main",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
and my "tasks.json":
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "make",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
}
Secondly I tried following this video which doesn't use the make file I used in the guide.
here's the "launch.json"
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/main",
"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++: g++ build active file"
},
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/bin/main",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file"
}
]
}
and "tasks.json"
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
I also tried executing vscode in admin mode and using chmod -x [my main file] in the terminal, where [my main file] is the location of the binary output from the build.
And finally I used the vscode extension C/C++ runner to see if I was missing something.
In ALL this cases the debug console showed the same issue.
ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x8001245
Cannot insert breakpoint 2.
Cannot access memory at address 0x8001260
And the terminal shows this warning:
&"warning: GDB: Failed to set controlling terminal: Operation not permitted\n"
[1] + Done "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-ri4karkl.dco" 1>"/tmp/Microsoft-MIEngine-Out-3varx35x.tnm"

Value of miDebugger path is invalid in VS code while executing c program

Getting continuous error "unable to start debugging. The value of miDebuggerPath is invalid". while running a c program in VS Code
Here is launch.json file
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ 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 gcc",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe"
}
]
}
and here is tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I tried to change the path of compiler but it's not working.

How do I get my breakpoints to work in VSCode?

I'm trying to debug this C source file in VS Code on Windows 10:
#include <stdio.h>
#include <stdlib.h>
int main(void){
printf("Test 1\n");
printf("Test 2\n");
printf("Break here\n");
printf("Test4\n");
system("pause");
return 0;
}
But when I do, all breakpoints are ignored by the debugger. It says "No symbols have been loaded for this document." as seen below:
I've read that the -g flag is required to be passed to g++ in order to be debugged, but even though it's there, I still get the same "No symbols have been loaded for this doucment." message.
tasks.json:
{
"tasks": [
{
"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",
"-static"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
},
],
"version": "2.0.0"
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "integratedTerminal",
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"preLaunchTask": "[!] C/C++: g++.exe build active file"
}
],
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe"
}
Is there anything else I need to do to get the breakpoints to work?
I narrowed down the problem. In the launch.json, I changed "type": "cppvsdbg" to "type": "cppdbg" as seen below:
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
// "console": "integratedTerminal",
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"preLaunchTask": "[!] C/C++: g++.exe build active file"
}
],
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe"
}
And now all the breakpoints are working properly.

Linking math library to visual studio code GCC

I'm trying to link the math library to the C debugger than comes with visual studio code, GCC. I'm want to mimic what the argument -lm does when I use a makefile. To my understanding I can do this using the launch.json file shown below.
{
"version": "0.2.0",
"configurations": [
{
"name": "gcc - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": ["nums"],
"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++: gcc build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
I looked through the documentation https://code.visualstudio.com/docs/cpp/launch-json-reference. The closest thing I could find was "miDebuggerArgs". I tried adding the line "miDebuggerArgs": "-lm" to the "configurations" list, but it didn't work. Any advice?
You posted the launch.json file. Go to the tasks.json and add -lm to the arguments.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/bin/${fileBasenameNoExtension}",
"-lm"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"isDefault": true,
"kind": "build"
},
"detail": "compiler: /usr/bin/gcc"
}
]
}

Resources