在C中显示#define值 [英] displaying #define values in C

查看:132
本文介绍了在C中显示#define值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这类库文件头中获得了一系列#defines:

I have a series of #defines from a library file header of this sort:

typedef int Lib_error;   

#define   LIB_ERROR_A      ((Lib_error) 0x0000) 
#define   LIB_ERROR_D      ((Lib_error) 0x0100)                   
#define   LIB_ERROR_F      ((Lib_error) 0x0200)                      
#define   LIB_ERROR_K      ((Lib_error) 0x0300)                
#define   LIB_ERROR_O      ((Lib_error) 0x0400)                
#define   LIB_ERROR_P      ((Lib_error) 0x0500)
#define   LIB_ERROR_R      ((Lib_error) 0x0600) 
#define   LIB_ERROR_X      ((Lib_error) 0x0700)             
#define   LIB_ERROR_Y      ((Lib_error) 0x0800) 
#define   LIB_ERROR_M      ((Lib_error) 0x0900) 
 /* and so on */

是否有其他方法可以打印这些值,例如

Is there any way apart I can print these values so if for example

uint MyError;
   /* printf("Error = %s",MyError); It should print the string LIB_ERROR_F instead of say 0x200 */

我可以使用很多但我想知道是否有更聪明的方法。请注意,我仍然无法更改库头文件的内容。

I could use lot's of if else for this but I was wondering if there was a cleverer way. Please note that I can't change the contents of library header file in anyway.

推荐答案

人们似乎对它们有不同的感觉,但 X宏 是一种可能的解决方案。

People seem to have mixed feelings about them, but X-macros are one possible solution.

但是如果您不能更改标题,则只有两个选项(AFAIK):

But if you can't change the header, then your only two options (AFAIK) are:


  • 手动执行

  • 某种形式的代码生成过程会在您的构建过程中进行。在您的情况下,应该可以通过使用 sed 解析头文件来实现(假设您在Linux上工作)。

  • Doing it manually
  • Some form of code generation step in your build process. In your case, it should probably be possible by parsing the header file with sed (assuming you're working on Linux).

这篇关于在C中显示#define值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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