cvisual-studio-codegdbvscode-debuggergdbserver

Create variable while in VS Code C/C++ debug console


While debugging the main function in Debug Console, I want to create a new variable called 'b' and assign a integer value 9 to it, but it compalins about 'b' being undefined. Why am I getting this error and how can I get around it?

-> int b = 9;
   identifier "b" is undefined
#include <stdio.h>
#include <stdint.h>

int main()
{
    int i = 0; 
    printf("i is %d", i);
    return 0;
}

Solution

  • In gdb in general, you can define convenience variables like so:

    set $b = 9
    

    in order to do this from the Debug Console, you must use the -exec prefix:

    -exec set $b = 9
    

    and you can then write lines like

    -exec p i + $b
    

    (where i is your C variable).


    In picture:

    enter image description here

    and you can even use these convenience variables in places like the Watch interface:

    enter image description here