20

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.

0

4 Answers 4

17

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
2
  • But I can not change the value, if it's in the Watch window...
    – Robin Hsu
    Mar 17, 2020 at 14:35
  • 3
    Manually type isn't a long term solution.
    – Tan Nguyen
    Jan 29, 2022 at 15:50
2

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 !

0

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.

0

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.