任何用于测试扩展 C/C++ #define 宏的实用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3019609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:49:14  来源:igfitidea点击:

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

c++macrosc-preprocessor

提问by Randy

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 the bigger question is whether there is any utility someone could recommend, to quickly display what a macro is actually doing? It seems like even the slow trial and error process would go much faster if I could see what is wrong.

似乎我经常花太多时间试图让 #define 宏完全按照我的意愿去做。我将在下面发布我当前的困境,任何帮助表示赞赏。但真正更大的问题是是否有人可以推荐任何实用程序来快速显示宏实际在做什么?如果我能看到问题所在,似乎即使是缓慢的试错过程也会变得更快。

Currently, I'm dynamically loading a long list of functions from a DLL I made. The way I've set things up, the function pointers have the same nanes as the exported functions, and the typedef(s) used to prototype them have the same names, but with a prepended underscore. So I want to use a define to simplify assignments of a long long list of function pointers.

目前,我正在从我制作的 DLL 中动态加载一长串函数。按照我的设置方式,函数指针与导出的函数具有相同的 nanes,用于原型化它们的 typedef(s) 具有相同的名称,但带有下划线。所以我想使用一个定义来简化一长串函数指针的赋值。

For example, In the code statement below, 'hexdump' is the name of a typedef'd function point, and is also the name of the function, while _hexdump is the name of the typedef. If GetProcAddress() fails, a failure counter in incremented.

例如,在下面的代码语句中,'hexdump'是一个typedef的函数点的名称,也是函数的名称,而_hexdump是typedef的名称。如果 GetProcAddress() 失败,则失败计数器递增。

if (!(hexdump = (_hexdump)GetProcAddress(h, "hexdump"))) --iFail;

So let's say I'd like to replace each line like the above with a macro, like this...

所以假设我想用宏替换上面的每一行,就像这样......

GETADDR_FOR(hexdump )

Well this is the best I've come up with so far. It doesn't work (my // comment is just to prevent text formatting in the message)...

嗯,这是迄今为止我想出的最好的。它不起作用(我的 // 注释只是为了防止消息中的文本格式)...

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

And again, while I'd APPRECIATE an insight into what silly mistake I've made, it would make my day to have a utility that would show me the error of my ways, by simply plugging in my macro.

再说一次,虽然我很欣赏我犯了多么愚蠢的错误,但我会很高兴拥有一个实用程序,只需插入我的宏即可向我展示我的方法中的错误。

回答by Mohit

enter image description hereGo 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.

在此处输入图片说明转到https://godbolt.org/。在左窗格中输入您的代码并选择 compiler as gcc 将参数作为 -E 在右窗格中。您的预处理代码将出现在右侧。

回答by Mark Rushakoff

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.cto only preprocess the file.

在 GCC 中,它gcc -E foo.c只是对文件进行预处理。

Visual Studio usesthe /Pargument.

Visual Studio 使用/P参数。

回答by Omnifarious

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

您似乎对 C 预处理器宏中的字符串化或标记粘贴的确切语法感到困惑。

You might find this page about C preprocessor macros in generalhelpful.

您可能会发现这个关于C 预处理器宏的页面通常很有帮助。

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.

尾随;应该被跳过,因为您很可能将其键入为GETADDR_FOR(hexdump);,否则它在您的 C 代码中看起来会很奇怪并混淆许多语法高亮显示。

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

正如其他人提到的那样,gcc -E将运行预处理器并跳过其他编译步骤。这对于调试预处理器问题很有用。

回答by Jerry Coffin

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.

您可能想看看Boost Wave。像大多数 Boost 一样,它实际上更像是一个库而不是一个实用程序,但它确实有一个驱动程序来充当完整的预处理器。