如何解析两个同名的结构?

Jay*_*Jay 6 c structure

在我的代码库中,我发现两个模块具有相同名称的结构.它给出了名称冲突错误.有没有办法解决它而不更改代码?

Sco*_*les 9

这是一个可怕的黑客攻击,但是可以使用宏来重新定义结构的名称,就像这样

// a.h
struct collide {
    int a;
};

// b.h
struct collide {
    float b;
};

// test.c
#define collide a_collide
#include "a.h"
#undef collide
#include "b.h"
int main(){
    struct a_collide a;
    struct collide b;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

您可能希望重命名两个标头的结构,以便在有人不可避免地使用错误的结构时给出错误,可能在包装器标头中

// wrap_a.h
#define collide a_collide
#include "a.h"
#undef collide
Run Code Online (Sandbox Code Playgroud)

请记住取消宏,以免在整个代码中获得随机替换.