Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setting up pre build events in vs code? #1683

Closed
miaotian opened this issue Mar 13, 2018 · 5 comments
Closed

setting up pre build events in vs code? #1683

miaotian opened this issue Mar 13, 2018 · 5 comments
Assignees
Labels

Comments

@miaotian
Copy link

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.

@WardenGnaw
Copy link
Member

@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.

@WardenGnaw WardenGnaw self-assigned this Mar 13, 2018
@miaotian
Copy link
Author

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.

@miaotian
Copy link
Author

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
}
]
}
]
}

@WardenGnaw
Copy link
Member

@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",

@miaotian
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants