Setting up VS Code for C using Cygwin64 Compiler and Debugger on Windows - c

Would like to set up VS Code to work with Cygwin/Cygwin64.
Already have these set up:
Installed Cygwin64 on windows
Installed gcc (Compiler) and gdb (Debugger) packages from Cygwin installer
GCC and GDB are NOT in windows path.
Installed Visual Studio Code
Posting this because it took me a couple of days from multiple different sources to set this up.
This is specifically for Windows with Cygwin/Cygwin64 installed.
DISCLAIMER: I've only tested this for building single files.

Instructions here are for setting up on VS Code
Install the extension C/C++ on VS Code
Name: C/C++
Id: ms-vscode.cpptools
Description: C/C++ IntelliSense, debugging, and code browsing.
Version: 0.23.1
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools
If you have a workspace already, skip this step.
Create a folder and add this folder into VS Code. Then save workspace.
Set up launch.json
Go to "Debug > Open Configurations", this should open the launch.json file. Below is my configuration. If you're testing this and unsure of what you're doing, I suggest you save your original content somewhere before replacing things.
Note: "preLaunchTask": "gcc.exe build active file" runs the task labelled "gcc.exe build active file".
{
"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": [
{
"name": "PATH",
"value": "%PATH%;z:\\cygwin64\\bin"
}
],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\cygwin64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"logging": { "engineLogging": true }, //optional
"preLaunchTask": "gcc.exe build active file"
}
]
}
Set up task.json
Go to "Terminal > Configure Tasks..." and select "gcc.exe build active file"
The various "-W" flags in "args" are meant to make the compiler more strict. You can remove them if you'd like.
{
"tasks": [
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "C:\\cygwin64\\bin\\gcc.exe",
"args": [
"-g",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-Werror", // Optional
"-Wall", // Optional
"-Wextra", // Optional
"-ansi", // Optional
"-pedantic", // Optional
"${file}"
],
"options": {
"cwd": "C:\\cygwin64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
],
"version": "2.0.0"
}
Build and Debug Active File
Go to the C file you want to build, press Ctrl+Shift+P for "Command Palette > C/C++ Build and Debug Active File > gcc.exe build active file" or if you only want to build then go to "Terminal > Run Build Task".

Cygwin64 is inefficient because it is using a medium memory model. This makes it use 64 bit absolute addresses instead of 32 bit relative addresses for static data.
I will recommend to use the Clang plugin for Visual Studio instead, unless you have a specific reason to use Cygwin64. See https://devblogs.microsoft.com/cppblog/clang-llvm-support-in-visual-studio/
You also get rid of the cygwin DLL when using the Clang plugin.

Related

How to make a develop env by using singularity+vscode

I created a singularity image for arm development. I installed a singularity in my WSL.
I am going to code and debug at wsl,then move it to arm platform by using Cross compiler in my singularity.
How to use vscode to build my program (arm version)? Or how to use singularity integratly with vscode?
I tried to build my program with singularity shell,and it succeed.I have learnt something about vscode's task and launch,and build my program with wsl,but I dont know how to use them to bootup a singularity and then build my program.
To use Visual Studio Code (VSCode) to build your program for the ARM platform using a Singularity image, you will need to configure the tasks and launch settings in VSCode.
First, you will need to create a tasks.json file in the .vscode folder in your project directory. This file will define the command to run the build process using Singularity. The command would look something like this:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "singularity exec /path/to/your/image.sif arm-linux-gnueabihf-gcc -o main main.c",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
You will also need to configure the launch settings in launch.json. This file is used to configure how your program is executed. You can use the preLaunchTask field to specify the build task you created above, so that it runs before your program is launched:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "build"
}
]
}
Once you have configured the tasks and launch settings, you should be able to use the Tasks: Run Build Task command in VSCode to build your program using the Singularity image and the Cross compiler, and use the Debug: Start Debugging command to run and debug your program.
You can also use the vscode-singularity extension, which is a Singularity extension for VSCode. It allows you to manage Singularity images, containers and shells directly from VSCode.

Attaching source code belonging to a shared library [duplicate]

In a Python project, how do you tell the built-in VSCode debugger to step into the code of functions from other libraries on execution?
I know it is possible for functions implemented in standard libraries by adding a
"debugOptions": ["DebugStdLib"]
to your configuration in launch.json as specified here, however it does not seem to be possible to force the debugger to step into the code of non-standard modules, such as the ones you have written yourself and imported into the current file.
In order to improve the accepted answer by John Smith, it is worth mentioning that now the option has been renamed again. The new option is
"justMyCode": false
and as per the documentation
When omitted or set to True (the default), restricts debugging to
user-written code only. Set to False to also enable debugging of
standard library functions.
This is done by customising your debugger.
If you haven't already, you need to initialise the debugger customisation. You can do this by opening the debugger section in the side bar and selecting create a launch.json file.
Once this is done, a launch.json file will be created in a .vscode folder in your workspace.
Edit this file. It will look something like this:
{
...,
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
Add "justMyCode": false to the "Python: Current File" configuration, like this:
{
...,
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false
}
]
}
True as of Visual Studio Code version 1.59.0.
Reference:
https://code.visualstudio.com/docs/python/debugging
A debugger configuration with
"debugOptions": ["DebugStdLib"]
added in launch.json in fact will step into user-defined and pip-installed modules, contrary to what's written in the main question.
Most of the time, I debug unit tests rather than the running application.
If that is also the case on your side and you use:
Visual Studio Code
pytest
The Python Test Explorer for Visual Studio Code (A test runner extension (adds a debug menu and provided debug buttons above tests directly in the IDE)) https://marketplace.visualstudio.com/items?itemName=LittleFoxTeam.vscode-python-test-adapter
Then use the following launch.json as mentionned in the plugin page:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug test",
"type": "python",
"request": "attach",
"console": "externalTerminal",
"justMyCode": false,
"stopOnEntry": true,
"envFile": "${workspaceFolder}/.env.test",
"purpose": ["debug-test"]
}
]
}
For those using the standard VSCode Debugger, a little more clarification on how to configure this in VS Code might be needed
Create Your Debugger Configuration As Follows
Open up your .vscode/launch.json
Add a configuration {} to the configurations list:
"configurations": []
This one will be recognized by the built-in VSCode Debugger:
{
"name": "Python: Debug Tests",
"type": "python",
"request": "launch",
"program": "${file}",
"purpose": ["debug-test"],
"console": "integratedTerminal",
"justMyCode": false
}
Key points:
purpose should be set to ["debug-test"]
"justMyCode": should be set to false.
References: Official VSCode Docs
The configurations in launch.json file as follows work for me.
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "test",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false,
"purpose": ["debug-in-terminal"]
}
]

Is it possible to debug WM from VSCode?

I wanna continue update dwm (Dynamic Window Manager) for myself. Previously I just built and ran dwm on another tty via startx. But this not provide debugging via IDE.
Can I satrt dwm from VSCode with debugging?
I tried to add simply launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/dwm",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
But when I run it I get error:
dwm: another window manager is already running

Could not find the task 'C/C++: gcc build active file'

I am using C/C++ v1.6.0 extension by Microsoft
and when I press F5 to run my code in debug mode I receive the following error:
I read some other posts that suggested configureing launch.json, but I don't even have this file:
The content of tasks.json is as follow:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/clang"
}
]
}
You can try to press F1 and type 'build' or 'debug'. Visual Studio Code has great support for automatically generating such tasks.
You can also look in the Run and Debug pane (button on the left or Ctrl+Shift+D). Maybe there are some hints.
For me my problem was I was using a subsystem of WSL in VS Code. So reinstalling the C/C++ extension off of the market place again in my workspace worked for me. Also, I noticed under your JSON file you have
"command": "/usr/bin/clang"
And
"detail": "compiler: /usr/bin/clang"
My JSON File is slightly different from yours hope this helps.
tasks.json file

Office Add-in Debugging in Chrome

I'm trying to get debugging of Office Add-ins working through VS Code. I've installed the Microsoft Office Add-in Debugger extension as well as the Debugger for Chrome extension. I have a section in my launch.json file that has a preLaunchTask that uses start:web.
I'm confused about how this process works, currently it opens two chrome windows, which I didn't expect. It looks like only one of the windows will actually cause VS Code to break on my breakpoints.
I'd like to hard code my document URL while debugging, so I don't have to keep entering it with an inputs promptString. I guess I'm confused about how these two files work together to start debugging and where do I need to enter the URL to my Excel file in SharePoint.
launch.json
{
"name": "Office Online (Chrome)",
"type": "chrome",
"request": "launch",
"webRoot": "${workspaceFolder}",
"preLaunchTask": "Debug: Web",
"url": "https://UrlToMyTestExcelDocumentInSharePoint"
}
tasks.json
{
"label": "Debug: Web",
"type": "npm",
"script": "start:web -- --document https://UrlToMyTestExcelDocumentInSharePoint",
"presentation": {
"clear": true,
"panel": "shared",
"showReuseMessage": false
},
"problemMatcher": []
}

Resources