How to debug macros efficiently in VS?

前端 未结 9 1753
情书的邮戳
情书的邮戳 2021-02-01 22:38

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?

相关标签:
9条回答
  • 2021-02-01 22:39

    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.

    0 讨论(0)
  • 2021-02-01 22:39

    If you want to see the expanded code generated by the preprocessor then you can get it using the -E flag using gcc. The command would be like this:

    $ gcc -E <source code> -o <preprocessed file name>
    

    You can replace the original source code using macro with the expanded code and place whatever break point that you want to place there.

    0 讨论(0)
  • 2021-02-01 22:41

    If you're using Visual Studio to work on C or C++ projects that use macros, get Resharper for C++ (I believe that there is a trial version). It allows you to click on a macro and expand it completely, or in stages. We have some extremely complicated, nested macros and it's the only way to understand them.

    0 讨论(0)
  • 2021-02-01 22:45

    Yes, you can.

    Locate the preprocessed file generated by the preprocessor in the Debug folder.

    So if your macro is written in testing.c file, the preprocessed file would be called testing.i

    There you can see how the macro have been expanded.

    To debug it, just copy paste this code to yours :)

    0 讨论(0)
  • 2021-02-01 22:50

    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

    0 讨论(0)
  • 2021-02-01 22:50

    I learned from http://www.codeproject.com/Tips/320721/Macro-expansion-in-VCplusplus the best way to debug macros.

    And what i do is to create a cxx file named "test.cxx" and inside i only put the macro and a some uses example of a test.cxx:

    #define GetMacro(param1) \
      public: void Get##param1(){ return m_##param1; }
    
    GetMacro(MyVariable);
    

    and in a command line i enter:

    c:\ cl /EP test.cxx > output.cxx
    

    When i open the output.cxx file there should be some blank lines and at the bottom the expanded macro, like:

    public: void GetMyVariable(){ return m_MyVariable; };
    

    You can test macros without compiling and that makes it quick.

    0 讨论(0)
提交回复
热议问题