Compiling multiple C files inside of Visual Studio Code - c

How can I compile multiple C files inside of Visual Studio Code? Right now I can only compile the main file and I want to be able to compile multiple files from multiple folders and then have the ability to rename that outputted exe and have it go into a designated folder. I can do this all from the command line but it would be way easier if I could just do it from within the IDE. For example lets say I have a Main.c file and an Addresses.c file within the the default directory (next to all of the folders like .vsCode and so on) and then I have another folder named gui that also contains C files. In that example how could I compile my project so that it correctly uses all of those files then changes the outputted exe's name to test.exe (instead of it being main.exe) and places it inside of a folder labeled Builds? I have watched and read so many articles and nothing works I would really appreciate any help thank you! The following snippets are of my launch.json file and my c_cpp_properties file if you need anything else please let me know! Also I am on windows 10 also I do also have header files but I never had to specify them when compiling from my command prompt so I did not mention them above.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:/MinGW/bin/gcc.exe",
"args": [
"-g",
"${fileDirname}\\**.c",
"${fileDirname}\\**.h",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
],
"options": {
"cwd": "C:/MinGW/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "c:/Users/me/Desktop/Coding/C and C++/Externel/NonKernel/Cube",
"program": "c:/Users/me/Desktop/Coding/C and C++/Externel/NonKernel/Cube/build/Debug/outDebug",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

Related

C Program isn't Stopping at Breakpoints During Debug For Visual Studio Code

So, basically I have a breakpoint but the debugger isn't stopping at the breakpoint.
To compile I am using
gcc -g balance1.c -o ./test
The program takes in input and outputs fine it's just that I am explicitly putting a breakpoint and it isn't stopping no yellow line or anything. Granted it's the first time I've coded with C or used the debugger in any capacity so, thanks! Below is the 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": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/balance1.exe",
"args": [],
"stopAtEntry": false,
"justMyCode": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "externalTerminal"
}
]
}

Visual studio code - debbugging stopped working [gdb, mingw, windows 10]

I searched for a solution for a week but couldn't solve this one without posting here.
I'm coding in C in Vs code and I had no issues so far (on a laptop). Few days ago debugging suddenly stopped working; this is how is goes:
simple code like:
#include <stdio.h>
int main(){
int x;
x=1;
x*=10;
printf("\ntest %d",x);
}
I press the green arrow play button to run it. I compiles and builds fine (as per task.json), but the breakpoint is not hit and also the "printf" does not get any result in the integrated terminal.
Also the "debug console" shows nothing
This is my task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe compile active file",
"command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"isDefault": true,
"kind": "build",
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\gcc.exe"
}
]
}
and 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 - Compile and run debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Abilita la riformattazione per gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe compile active file"
}
]
}
I also tried and re-install everything Vs code, C/C++ Intellisense, Extention Run Code as well, unsitalled mingw and reintsalled it following the steps at "https://code.visualstudio.com/docs/cpp/config-mingw".
But the "missing debug" behaviour persisted.
On the other hand if I manually run the test.exe in the integrated terminal (powewrshell) by simply typing ".\test.exe" it work fine.
any idea?, thanks
The accepeted answer was given by olyBlackCat in the comments, which I'm quoting here to mark it as the accepted answer:
I'm seeing a lot of questions like this. Maybe try this extension instead of the default C++ one? It works for me on Linux. – HolyBlackCat Aug 16 at 17:28
Found the bug report. There's some advice there github.com/microsoft/vscode-cpptools/issues/7971

Where is my output when debugging C code in Visual Studio Code?

I have a simple c program, with prints something to screen.
When I debug the program I can see the DEBUG CONSOLE, however as soon as I use fgets I don't see any output. Where does my program run when debugging using VS Code?
If I just run my compiled .exe, everything is printed as expected.
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello World!\n");
printf("Enter your name\n");
char name[100];
// fgets(name, sizeof(name), stdin); // as soon as I uncomment this, no output is in the output console
printf("You name %s", name);
return 0;
}
my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/app.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false
}
]
}
One solution may solve your issue.
How to configure tasks.json and launch.json from VSCode
You don't need to do any type of coding for this, just follow the steps:
Delete tasks.json and launch.json located in .vscode folder
Press F5 (debugging shortcut) again focusing on that C program file, you'll see something like:
Select your compiler which is GCC (since you're trying to debug a C program and ensure the compiler is installed into your system).
You'll be prompted to select a configuration (assuming selected option: GCC), you'll get launch.json created automatically by VSCode as shown below:
Note: Keep the preLaunchTask configuration in your mind (located at the bottom-most of the config).
As soon as you press F5 again (this time, for creation of tasks.json) you'll get something shown below, simply select Configure Tasks:
Now, you'll be redirected into tasks.json, edit the label to the name you've selected in § 4 (remember that name). In other words, launch's preLaunchTask and tasks' label should be the same. Process shown below:
And now, you can make a successful debug. A working example:
Just add externalConsole": true to you configuration in launch.json.
For example:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "cl.exe build active file",
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
},
}
]
}

VSCode C trouble: Debug and Build C on Windows

I'm trying to setup VSCode to build and debug C on Windows.
I have installed MinGW.
I'm trying to generate an .exe file for the following code:
#define USE_PTS true
#include "Cartography.h"
static Cartography cartography ;
static int nCartography = 0;
int main(void)
{
nCartography = loadCartography("map (1).txt", &cartography);
showCartography(cartography, nCartography);
interpreter(cartography, nCartography);
return 0;
}
I have two other files Cartography.hand Cartography.c.
If I run the following command using the powershell terminal, it generates an .exe file perfectly:
gcc -std=c11 -o Main Cartography.c Main.c -lm
But if I try to build it using VSCode (using Ctrl + Shift + B) it doesn't recognize the other files:
> Executing task in folder Projeto2LAP: gcc -std=c11 -o Main Cartography.c Main.c -lm <
gcc.exe: error: Cartography.c: No such file or directory
gcc.exe: error: Main.c: No such file or directory
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
Here is my tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: gcc.exe build active file",
"command": "gcc -std=c11 -o Main Cartography.c Main.c -lm",
"options": {
"cwd": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
The other issue I have is when I'm trying to use the built in VSCode debugger.
I generate the .exe file using the command I mentioned, so an .exe file is present in the current folder.
(I should also mention that the folder I'm working on only contains the files and the .vscode folder, there are no subfolders or anything that could cause an error).
When I click on debug it gives me the following error message:
Error message prompt
Here is 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": "${workspaceFolder}/Main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
What am I doing wrong?
Your first question:
When you use the powershell command, it gets your *.c files from your current wording directory via relative paths. This is what you attempt to do in the task command, but your CWD is set to the wrong place. In tasks.json, you need to either specify the absolute paths of your files in the gcc command or change your CWD to the directory of the *.c files. Or better, VSC lets you set "cwd": "${workspaceFolder}". This means if you change the name or do any other edits, your task will still work. (For bonus points, you could set the CWD to the enclosing folder of the currently selected file.) First question part 2: The task error probably isn't you. It appears to be a known bug. Instructions here to mitigate it.
Sorry for the late response
I don't see any reason why you would get the debugging error, so my only guess is a permissions problem. Your debugger may not be able to "see" that that directory exists. Make sure neither it nor any of its parent folders have unduly tights restrictions.
Rather than pass one long string to launch GCC in tasks.json, consider using the "args" list variable, placing each arg in a separate element of the list.
Assuming your source files are located within the same directory that was opened in VSCode, the previously-mentioned "${workspaceFolder}" macro will construct the correct paths.
Otherwise, provide absolute paths to your source files as earlier suggested.
Example:
{
"label": "GCC Build Debug (64-bit)",
"type": "shell",
"command": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gcc",
"args": [
"--std=c11",
"-g",
"-lm",
"-o",
"${workspaceFolder}/Main"
"${workspaceFolder}/Main.c",
"${workspaceFolder}/Cartography.c"
],
"problemMatcher": [
"$msCompile"
]
},

Auto run executable after save in Visual Studio Code

I want to use visual studio code for C programming and I want to automate the procedure of saving -> compiling -> running. At the moment I have MinGW installed and using the C/C++ extension I achieved the saving and compiling using Ctrl+S (I changed the shortcut of compiling) but in order to execute I need to go to the cmd prompt and execute the program.
Is there any way I can, using one button, achieve this?
The closest I came to the aswer is to define my shortcuts
[
{
"key": "ctrl+s",
"command": "workbench.action.tasks.build"
},
{
"key": "ctrl+d",
"command": "workbench.action.tasks.test"
}
]
Then in tasks.json
{
"version": "0.1.0",
"command": "cmd",
"isShellCommand": true,
"args": ["/C"],
"showOutput": "silent",
"tasks": [
{
"taskName": "saveNcompile",
"suppressTaskName": true,
"isBuildCommand": true,
"args": ["gcc main.c -o main.exe"]
}, {
"taskName": "execute",
"suppressTaskName": true,
"isTestCommand": true,
"args": ["main.exe"]
}
]
}
And now using ctrl+s save and compile and ctrl+d execute.

Resources