how to expand VC++ macro references using Visual Studio?

15,407

Solution 1

"Project + Properties, C/C++, Preprocessor, Preprocess to a File = Yes. Compile and open the .i file in the build directory.

Solution 2

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, where mostly simply used as functions, this became of crucial importance for me. Generating all preprocessed files with /P works but is overkilling and time taking. 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.

Solution 3

You can see the macro expansion in the quick info tooltips since VS2017. A very cool new feature.

Macro Expansions in Quick Info Tooltips

Share:
15,407
J Smith
Author by

J Smith

Updated on June 09, 2022

Comments

  • J Smith
    J Smith almost 2 years

    Rather than right clicking on the macro identifier and then on "go to definition", is it possible to expand macro references and see what the code that is passed to the compiler looks like?