Hi,
I am trying to run some commands to load some library and environment variables before debugging. Is there anyway I can add pre build events like visual studio?
Thank yo very much.
@miaotian You can set environment variables with the environment field in your launch.json. See here for more details.
The tasks.json file can be used for pre-build events. You can chain commnads in a task. Here is the documentation for Custom Tasks in VsCode.
I am actually loading tons of environment variables used in 3rd party library. So it is unrealistic to add variables one by one.
Is it possible to debug the code I built custom tasks (I am doing c++ and want to use gdb in linux)?
Thank you very much.
I created a task running the command to load environmental variables, and I adds the task as preLaunchTask attributes in the launch.json for debugging. When I start debugging, the tasks runs without problem, but the main problem shows the environmental variables are not loaded.
So scripts for my tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "load_conf",
"type": "shell",
"command": "source load_conf.sh"
}
]
}
The scripts for 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"preLaunchTask": "load_conf",
"program": "${workspaceFolder}/build/exampleB1",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
@miaotian Sorry for the delay. One solution is to write your own script that setups the necessary environment variables and then at the end calls GDB. In your launch.json you will want MIDebuggerPath to point to that script. Make sure that your script takes in the command line arguments and passes all of it to GDB.
This is what you want to add to your launch.json.
"miDebuggerPath": "${workspaceRoot}/bin/script.sh",
Great. It works now. Thank you very much. @WardenGnaw
I initially tried to initiate in the .bashrc script. It works also, but kinds of cheating.
Glad the issue is closed.
Most helpful comment
@miaotian You can set environment variables with the
environmentfield in your launch.json. See here for more details.The tasks.json file can be used for pre-build events. You can chain commnads in a task. Here is the documentation for Custom Tasks in VsCode.