tags:

views:

113

answers:

2

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?

+1  A: 

No really good ideas come to mind. Here are two not-so-good ones:

  • If you are very lucky, you can segregate your code so that no module every needs to access both types of structure at one time, then only include the proper header, and away you go. This is fraught with peril and will be a maintainability nightmare: anyone who comes after you will have cause to curse your name and all your descendants unto the seventh generation.

  • If the code is c89ish you could try compiling with a c++ compiler and wrapping one or more of the offending structures in a namespace. This introduces problems from all the picky little differences in the two language (casting rules, class as a reserved word, etc...), so it almost certainly violates your request to not change the code.

Good luck.

dmckee
+8  A: 

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.

Scott Wales
I wouldn't say it so terrible hack. Once I had to include two lex-generated scaners in my program and they clashed on a big number of symbols, so I used similar trick and it worked like a charm.
qrdl
I think that what you more have to worry about is someone being confused by the renaming of the structs, as presumably these clashing names have come from an external source. The documentation for function calls, etc. would no longer match up with the actual prototypes, so any users would have to be clear on what has happened.
Scott Wales