在C中显示#define值

0 c c-preprocessor

我有一系列来自这种库文件头的#defines:

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 */
Run Code Online (Sandbox Code Playgroud)

有什么方法我可以打印这些值,所以例如

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

我可以使用很多其他的,但我想知道是否有一个更聪明的方式.请注意,无论如何我都无法更改库头文件的内容.

Oli*_*rth 7

人们似乎对它们有着复杂的感觉,但X-macro是一种可能的解决方案.

但是如果你不能改变标题,那么你唯一的两个选项(AFAIK)是:

  • 手动完成
  • 构建过程中的某种形式的代码生成步骤.在您的情况下,应该可以通过解析头文件sed(假设您正在使用Linux)来实现.