Nisba
Nisba

Reputation: 3448

How can I see global variables when debugging C/C++ in VS Code?

I am using Visual Studio Code and when I debug (I am debugging C++ code compiled with Clang) I see only local variables. I do not see any global variables list.

How can I see all variables?

enter image description here

In this case I am inside a loop and I see only all the variables defined inside the loop, not the one defined outside.

Upvotes: 23

Views: 21543

Answers (4)

starball
starball

Reputation: 51139

As stated in previous answers, instead of the "VARIABLES" subview, use the "WATCH" subview, press the "+" button, and type the name of the global variable you want to watch. Ex. my_global_variable, or if there are any namespaces involved, my_top_level_namespace::my_inner_namespace::my_global_variable.

If there are any unnamed namespaces involved, you might need to find out the mangled name of the variable and use that instead (not sure what else you would/could do, actually). One way if you're using gcc to find it is to dig through nm -nC path/to/my_executable_filename | less.

Note that if the global variable temporarily shows up in the VARIABLES subview at any point, you could also right click it there and select "Add to watch".

In the latest version of the C/C++ extension, editing global variables from that subview should also be supported (right click the variable and click "Set Value"), though at some point in the past it was broken, which could be worked around by writing the expression for the address of that variable (Ex. &my_variable), and then expanding that item in the subview to get to the value of the variable, and then right click and do "Set Value" there.

Upvotes: 1

Rain
Rain

Reputation: 3936

After you follow the stepes in Andrew L's answer you can modify the variable's value by the help of either:

1. Debug Console

enter image description here

2. Address-Of Operator

enter image description here

By prefixing the variable we want to watch with (&) we can now expand it and then right click on the dereferenced variable to set a value.

Upvotes: 2

Baptiste Florentin
Baptiste Florentin

Reputation: 151

In Visual Studio Code you can just go to the Watch pannel int the debug menu and click on + , then type the name of the variable you want to watch. Hope it helps !

Upvotes: 4

Andrew L
Andrew L

Reputation: 1224

You will need to manually add global variables to a watch window.

  1. Set a breakpoint
  2. Start debugging (Debug -> Start Debugging or F5)
  3. Open a Watch window (Debug -> Windows -> Watch -> Watch 1)
  4. Type in the name of the variable manually

Upvotes: 22

Related Questions