问题
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?)
回答1:
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)
回答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://docs.microsoft.com/en-us/visualstudio/debugger/context-operator-cpp?view=vs-2017 or search for "visual studio context operator".
来源:https://stackoverflow.com/questions/2983316/viewing-namespaced-global-variables-in-visual-studio-debugger