How to debug a macro

Discussion in 'C++' started by AS, Mar 10, 2008.

  1. AS

    AS Guest

    Hello,
    Can anybody tell me how to debug a macro ?

    for ex,

    #define PRINTSTRING() \


    CString str = _T("hello"); \

    AfxMessageBox(str); \
     
    AS, Mar 10, 2008
    #1
    1. Advertising

  2. AS wrote:
    > Can anybody tell me how to debug a macro ?
    >
    > for ex,
    >
    > #define PRINTSTRING() \
    >
    >
    > CString str = _T("hello"); \
    >
    > AfxMessageBox(str); \


    Most compilers support an option to output the result of
    preprocessing. RTFM and see if yours does as well. Then
    use your macro and see what it expands into.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 10, 2008
    #2
    1. Advertising

  3. AS

    Ian Collins Guest

    AS wrote:
    > Hello,
    > Can anybody tell me how to debug a macro ?
    >

    Rewrite it as an inline function.

    --
    Ian Collins.
     
    Ian Collins, Mar 10, 2008
    #3
  4. Ian Collins wrote:
    > AS wrote:
    >> Hello,
    >> Can anybody tell me how to debug a macro ?
    >>

    > Rewrite it as an inline function.


    With all due respect, declarations cannot be rewritten as
    inline functions.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 10, 2008
    #4
  5. AS

    Ian Collins Guest

    Victor Bazarov wrote:
    > Ian Collins wrote:
    >> AS wrote:
    >>> Hello,
    >>> Can anybody tell me how to debug a macro ?
    >>>

    >> Rewrite it as an inline function.

    >
    > With all due respect, declarations cannot be rewritten as
    > inline functions.
    >

    Did you read the OP's example Victor? I assumed the question was asking
    how to debug a function like macro. The only use I can see for function
    like macros in C++ is as wrappers for functions when you want to pass
    stringified tokens or macros in as parameters.

    Something like

    void printHere( const char* file, unsigned line, void* );

    #define PRINT_HERE( x ) printHere( __FILE__, __LINE, (x) )

    --
    Ian Collins.
     
    Ian Collins, Mar 10, 2008
    #5
  6. Ian Collins wrote:
    > Victor Bazarov wrote:
    >> Ian Collins wrote:
    >>> AS wrote:
    >>>> Hello,
    >>>> Can anybody tell me how to debug a macro ?
    >>>>
    >>> Rewrite it as an inline function.

    >>
    >> With all due respect, declarations cannot be rewritten as
    >> inline functions.
    >>

    > Did you read the OP's example Victor?


    Yes, I did. It contains a declaration.

    > I assumed the question was
    > asking how to debug a function like macro.


    I assume nothing. That's the difference, I guess.

    > The only use I can see
    > for function like macros in C++ is as wrappers for functions when you
    > want to pass stringified tokens or macros in as parameters.
    >
    > Something like
    >
    > void printHere( const char* file, unsigned line, void* );
    >
    > #define PRINT_HERE( x ) printHere( __FILE__, __LINE, (x) )


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 10, 2008
    #6
  7. AS

    Ian Collins Guest

    Victor Bazarov wrote:
    > Ian Collins wrote:
    >> Victor Bazarov wrote:
    >>> Ian Collins wrote:
    >>>> AS wrote:
    >>>>> Hello,
    >>>>> Can anybody tell me how to debug a macro ?
    >>>>>
    >>>> Rewrite it as an inline function.
    >>> With all due respect, declarations cannot be rewritten as
    >>> inline functions.
    >>>

    >> Did you read the OP's example Victor?

    >
    > Yes, I did. It contains a declaration.
    >

    So it does. That'll teach me to post before breakfast!

    --
    Ian Collins.
     
    Ian Collins, Mar 10, 2008
    #7
  8. On 2008-03-10 13:00:26, AS wrote:

    > Can anybody tell me how to debug a macro ?


    I guess that depends on what exactly you want to debug.

    If you want to debug the macro expansion, follow the advice of creating a
    preprocessor output and analyse that.

    If you want to debug the macro function, it sometimes is helpful to take
    one macro instance, replace it with the expanded macro, and debug that
    piece of code. When it works, replace it again with the (now working)
    macro.

    But, as someone else also already wrote, make sure you need a macro and
    can't do it with an inline (or normal) function or other construct.

    Gerhard
     
    Gerhard Fiedler, Mar 11, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,132
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    590
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    512
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    485
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    474
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page