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

2022-01-11 00:00:00 macros c++ c-preprocessor

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

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.

目前,我正在从我制作的 DLL 中动态加载一长串函数.按照我的设置方式,函数指针与导出的函数具有相同的名称,用于原型化它们的 typedef(s) 具有相同的名称,但带有前置下划线.所以我想用一个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.

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

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.

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.

推荐答案

转到 https://godbolt.org/.在左窗格中输入您的代码并选择编译器作为 gcc 将参数作为 -E 在右窗格中.您的预处理代码将显示在右侧.

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.

相关文章