如何正确处理某些 3dparty 代码中定义的相同宏名称?

How to properly deal with the same macro names defined in some 3dparty code?

我想在我的自定义 header 文件中使用 TRUEFALSE 宏,供不同的人使用.c 文件作为一些通用的宏定义文件。

我遇到的问题如下: 3dparty 和我都定义了相同的宏,但这些值可能不同(TRUE 或 FALSE 可能是一个非常简单的情况,因为更明显的是哪个值去哪里)。

我想强调的是,我不能在我的自定义代码中包含 3party header,因为 图书馆不是每个项目中常用的东西。更何况,还显得奇怪? (或者没有 - 但它虽然有效:))。 我看到的解决方案也可以做一些前缀,比如 CUSTOM_TRUE - 但它看起来很难看。

或者我根本不应该为此烦恼,只是让它以我选择的任何方式工作?

部分案例建模:

main.c

#include "3dparty.h"
#include "b.h"

/* TRUE defined from "custom" header and == 2
 - in this case we are good */

int res = cus_is_digit("1");
if (res == TRUE)
{
    ...
}

/* 3dparty is returning TRUE as 1 but we are checking for 2 */
int res2 = is_something(...);
if (res2 == TRUE)
{

} 

...

b.h

#undef TRUE
#define TRUE 2
#endif

OR

#ifndef
#define TRUE 2
#endif

/* This file uses TRUE macro defined with value 2. */
/*Sorry for definition in header */  
int cus_is_digit(const char *sdigit)
{
    /* some logic here */
    return TRUE; /* return 2; */
}
...

3dparty.h

#define TRUE 1

int is_something(...);
...

3dparty.c

 #include 3dparty.h

 int is_something(...)
 {
     ...
     return TRUE; /* return 1; */
 }
    
 ...
  1. 没有一种“正确”的方法。
  2. 如果您有相同的宏名称,但它们的含义完全不同,那么唯一的方法就是使用不同的更具体的名称。

示例:

另一个头文件定义

#define LINELENGTH   64

并且您的代码需要不同的大小 - 只需在头文件中添加一些前缀即可。如果您的模块名为 usart 那么

#define USART_LINELENGTH 128