Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug macros efficiently in VS?

I've got a pretty complicated macro inside my (unmanaged) C++ code. Is there any way to expand macros in VS debugger? Or maybe there is another way to debug macros there?

F.e. I'd like to place a breakpoint inside it.

(Yes, I know macros are bad.)

like image 671
liori Avatar asked Sep 07 '09 15:09

liori


People also ask

How do I debug a macro in Visual Studio?

Start debugging in Visual Studio by pressing F5. In Excel, open up your worksheet and start debugging your VBA code using Excel's debugger. When the managed code gets executed where you set the breakpoint, you'll stop in Visual Studio.

How do you debug a C++ program in VS code?

To debug a memory dump, open your launch. json file and add the coreDumpPath (for GDB or LLDB) or dumpPath (for the Visual Studio Windows Debugger) property to the C++ Launch configuration, set its value to be a string containing the path to the memory dump.


3 Answers

Go to either project or source file properties by right-clicking and going to "Properties". Under Configuration Properties->C/C++->Preprocessor, set "Generate Preprocessed File" to either with or without line numbers, whichever you prefer. This will show what your macro expands to in context. If you need to debug it on live compiled code, just cut and paste that, and put it in place of your macro while debugging.

like image 86
Jim Buck Avatar answered Sep 27 '22 20:09

Jim Buck


I heard all possible negative answers on the topic:

  • macros can only be expanded not evaluated
  • preprocessor should parse also include files
  • nested macros can be over-complicated
  • conditional preprocessing can be tricky
  • macros are evil just avoid them
  • etc etc....

They are all true, but IMO they collide with the reality of everydays programming.

In fact, working on old C project, where macros were mostly simply used as functions, this became of crucial importance for me. Generating all preprocessed files with /P works, but is overkilling and time consuming. I just needed a tool that expands a simple macro defined a few lines above or at maximum in other file.

How to do that?

  1. On Linux simply use GDB and his expand macros capabilities
  2. On windows I use https://www.jetbrains.com/resharper-cpp/ integrated into Visual Studio

So, Yes, in a practical sense, it is possible.

like image 25
OttoVonBrak Avatar answered Sep 27 '22 21:09

OttoVonBrak


The compiler expands macros nicely. Get a compilation listing with the macros expanded.

Insert the expanded macro into your code. Recompile and you should be able to step through it, to do a simple test.

Generally when I am building a complicated macro, I hard code it first, and test it. Then I turn it into a macro.

For 2008, this may be of help

like image 27
EvilTeach Avatar answered Sep 27 '22 22:09

EvilTeach