Any utility to test expand C/C++ #define macros?

前端 未结 5 818
渐次进展
渐次进展 2020-12-24 00:41

It seems I often spend way too much time trying to get a #define macro to do exactly what i want. I\'ll post my current dilemma below and any help is appreciated. But really

相关标签:
5条回答
  • 2020-12-24 01:18

    Go to https://godbolt.org/. Enter your code in the left pane and select compiler as gcc put the argument as -E in the right pane. Your pre-processed code will appear on the right.

    0 讨论(0)
  • 2020-12-24 01:26

    You can just run your code through the preprocessor, which will show you what it will be expanded into (or spit out errors as necessary):

    $ cat a.c
    #define GETADDR_FOR(a) if (!(a = (#_#a)GetProcAddress(h, "/""#a"/""))) 
    GETADDR_FOR(hexdump)
    
    $ gcc -E a.c
    # 1 "a.c"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "a.c"
    a.c:1:36: error: '#' is not followed by a macro parameter
    
    GETADDR_FOR(hexdump)
    

    In GCC, it's gcc -E foo.c to only preprocess the file.

    Visual Studio uses the /P argument.

    0 讨论(0)
  • 2020-12-24 01:32

    http://visualstudiogallery.msdn.microsoft.com/59a2438f-ba4a-4945-a407-a1a295598088 - visual studio plugin to expand macroses

    0 讨论(0)
  • 2020-12-24 01:34

    You might want to take a look at Boost Wave. Like most of Boost, it's really more a library than a utility, but it does have a driver to act as a complete preprocessor.

    0 讨论(0)
  • 2020-12-24 01:35

    You appear to be confused about what the exact syntax is for stringifying or token pasting in C preprocessor macros.

    You might find this page about C preprocessor macros in general helpful.

    In particular, I think this macro should read like this:

    #define GETADDR_FOR(a) if (!(a = (_##a)GetProcAddress(h, #a))) --iFail
    

    The trailing ; should be skipped because you will likely be typing this as GETADDR_FOR(hexdump);, and if you don't it will look very strange in your C code and confuse many syntax highlighters.

    And as someone else mentioned gcc -E will run the preprocessor and skip the other compilation steps. This is useful for debugging preprocessor problems.

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