Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve two structures with the same name?

Tags:

c

structure

In my code base I find that two modules have structures with the same name. It is giving a name conflict error. Is there a way to resolve it without changing the code?

like image 682
Jay Avatar asked Apr 21 '10 05:04

Jay


1 Answers

This is a terrible hack, but it would be possible to use a macro to redefine the name of the struct, like so

// 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;
}

You'd probably want to rename the struct for both headers to give errors when someone inevitably uses the wrong struct, perhaps in a wrapper header like

// wrap_a.h
#define collide a_collide
#include "a.h"
#undef collide

Remember to undef the macro so you don't get random replacements throughout your code.

like image 86
Scott Wales Avatar answered Sep 21 '22 22:09

Scott Wales