9

When debugging a non-managed C++ project in Visual Studio 2008, I occasionally want to see the value of a global variable. We don't have a lot of these but those that are there all declared within a namespace called 'global'. e.g.

namespace global
{
  int foo;
  bool bar;

  ...
}

The problem is that when the code is stopped at a breakpoint, the default debugging tooltip (from pointing at the variable name) and quickwatch (shift-f9 on the variable name) don't take the namespace into consideration and hence won't work.

So for example I can point at 'foo' and nothing comes up. If I shift-f9 on foo, it will bring up the quickwatch, which then says 'CXX0017: Error: symbol "foo" not found'. I can get around that by manually editing the variable name in the quickwatch window to prefix it with "global::" (which is cumbersome considering you have to do it each time you want to quickwatch), but there is no fix for the tooltip that I can work out. Setting the 'default namespace' of the project properties doesn't help.

How can I tell the VS debugger to use the namespace that it already knows the variable is declared in (since it has the declaration right there), or, alternatively, tell it a default namespace to look for variables in if it doesn't find them?

My google-fu has failed to find an answer. This report lists the same problem, with MS saying it's "by design", but even so I am hoping there is some way to work around it (perhaps with clever use of autoexp.dat?)

Chris
  • 91
  • 1
  • 2
  • 2
    You're lucky that your namespace has a name. If the variables are in an unnamed namespace, it's even impossible to see their value in the debugger (see http://stackoverflow.com/questions/1334989/debugging-data-in-anynomous-namespaces-c). – Patrick Jun 07 '10 at 07:53

2 Answers2

2

Using the full name including the namespace in the source solved it for me.

e.g.: write

global::bar = (global::foo==0)

instead of

bar = (foo==0)
R Risack
  • 69
  • 8
2

If the symbol is located in a different DLL, you can use the following syntax in the Watch window:

{,,<dllname>}<fully qualified symbol name>

e. g.

{,,foobar64d.dll}global::foo

See https://learn.microsoft.com/en-us/visualstudio/debugger/context-operator-cpp?view=vs-2017 or search for "visual studio context operator".

Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
  • Thanks! The alternate syntax from the MS doc you linked might be preferable eg.: foobar64d.dll!global::foo – Greg Mar 04 '19 at 02:12