打印名称和宏值 [英] Printing name and value of a macro

查看:553
本文介绍了打印名称和宏值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C程序有很多,可以启用或禁用与的#define 取值优化。当我运行我的程序,我想知道一直在编译时定义什么宏。

I have a C program with a lot of optimizations that can be enabled or disabled with #defines. When I run my program, I would like to know what macros have been defined at compile time.

所以我想写一个宏功能来打印宏的实际值。事情是这样的:

So I am trying to write a macro function to print the actual value of a macro. Something like this:

SHOW_DEFINE(X){\
  if( IS_DEFINED(X) )\
      printf("%s is defined and as the value %d\n", #X, (int)X);\
  else\
      printf("%s is not defined\n", #X);\
}

不过,我不知道如何使它工作,我怀疑这是不可能的,没有任何人对如何做到这一点的想法?

However I don't know how to make it work and I suspect it is not possible, does anyone has an idea of how to do it?

(注意,必须是没有定义宏,即使编译!)

(Note that this must compile even when the macro is not defined!)

推荐答案

编写一个可扩展到另一个宏将需要preprocessor在两次运行宏。

这是没有这样做。

Writing a MACRO that expands to another MACRO would require the preprocessor to run twice on it.
That is not done.

您可以写一个简单的文件,

You could write a simple file,

// File check-defines.c
int printCompileTimeDefines()
{
#ifdef DEF1
  printf ("defined : DEF1\n");
#else // DEF1
  printf ("undefined: DEF1\n");
#endif // DEF1
// and so on...
}

使用相同的编译时间定义在这个文件中的行与其他文件。

有时调用函数的开始。

Use the same Compile Time define lines on this file as with the other files.
Call the function sometime at the start.

如果你有一个源文件,而不是的Makefile #DEFINE 线/>
它们移动到另一个标题文件,包括头在所有源文件,

包括本入住defines.c

If you have the #DEFINE lines inside a source file rather than the Makefile,
Move them to another Header file and include that header across all source files,
including this check-defines.c.

希望你有相同的一组允许的所有源文件定义的。

否则,这将是审慎的做法是重新检查你定义的策略。

Hopefully, you have the same set of defines allowed across all your source files.
Otherwise, it would be prudent to recheck the strategy of your defines.


自动生成此功能的,

你可以使用 M4 宏语言(甚至是AWK脚本 实际上)。

M4 成为你的pre-pre-处理器。

To automate generation of this function,
you could use the M4 macro language (or even an AWK script actually).
M4 becomes your pre-pre-processor.

这篇关于打印名称和宏值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆