4

I am using two different c++ libraries. Both libraries use a name for a type, let's say, called fofo. In lib1.h:

typedef short fofo;

In lib2.h:

namespace LIB2
{

    typedef struct
    {
        uint16_t toto;    
    } fofo;
}
using LIB2::fofo;

I have a C++ project/VS 2008 that uses both libraries: when I do in my project

#include lib1.h

I get the error: error C2874: using-declaration causes a multiple declaration

I would like to use in my project both libraries but without making any change to these libraries. The only thing that I can modify is my VS2008 project. Thanks for your help!

loisir2022
  • 163
  • 2
  • 11
  • Are the definitions of `fofo` in the global namespace in both libraries? – Jonathan Sharman Jul 10 '18 at 01:05
  • 5
    This is why blowing away namespaces with global `using` directives is a bad idea. – Pete Becker Jul 10 '18 at 01:07
  • 2
    Note that neither of these names is a variable. Both are types. – Pete Becker Jul 10 '18 at 01:08
  • 1
    Hi Jonathan, I just updated my question. Lib2 uses a namespace but in fofo in lib1 is just a global variable. Thanks – loisir2022 Jul 10 '18 at 01:08
  • Hi Pete, I agree. I didn't write this code and unfortunately I can't rename any of the types... – loisir2022 Jul 10 '18 at 01:11
  • Is there a fix/hack for this problem? – loisir2022 Jul 10 '18 at 01:22
  • 2
    Let's begin with the fact that neither "fofo" is a variable. Details matter. When faced with a tricky C++ integration situation, it is crucial to fully understand what is what, and which is which. If you do not really understand how C++ works, there's little chance that you could figure out a solution for a tricky C++ problem. Here, the first "fofo" is not a variable, but a type. And the second "fofo" is also not a variable, but is also a type, that's also imported into the global namespace. Now, once you fully understand what all these are, now you can begin figuring out what to do about it. – Sam Varshavchik Jul 10 '18 at 01:28
  • Corrected the wording, but still looking for a fix ;) – loisir2022 Jul 10 '18 at 01:36
  • 2
    The fix also depends on the details. There is no law that says that just because you have a project that must use both libraries, you must `#include` the headers from both libraries in the same C++ source file. The preferred solution is to use only one of those headers in your C++ source, and have different parts of your code, that each uses one of those libraries, talk or communicate via a common API. Or, perhaps use preprocessor trickery, like it's been suggested. But there is no magic button anywhere you can push, and make the symbol conflict go away by itself. You have to make it happen. – Sam Varshavchik Jul 10 '18 at 01:40
  • @SamVarshavchik Can you develop more on your idea of using a common API...Thanks – loisir2022 Jul 10 '18 at 15:21
  • I don't quite see what's to be "developed" here. You have multiple C++ source files. Some of them `#include` one of the libraries, and do whatever needs to be done using that library. Some of the others `#include` the other library, and do whatever needs to be done with that library. For tasks that need to involve both libraries, the code that uses one of those libraries would call the code that uses the other. If you're looking for a Magic Button somewhere that only needs to be pushed, then everything works: sorry, it doesn't exist. You must code everything yourself. C++ is hard. – Sam Varshavchik Jul 10 '18 at 17:33

1 Answers1

4

this (dirty) workaround will probably work (depending how the headers are being used)

instead of including lib1.h directly ,wrap it by making a new headerfile

lib1wrapped.h

#define fofo fofo_wrap
#include "lib1.h" // OR <lib1.h>
#undef fofo
engf-010
  • 3,980
  • 1
  • 14
  • 25
  • instead of including lib1.h directly ,wrap it like I showed. Since fofo is merely a typedef and not a real type nothing really changes but the error should disappear. – engf-010 Jul 10 '18 at 01:40
  • It looks it solved the problem. Now it compiles. I will try to use the libraries tomorrow and let you know how it goes... Thanks! – loisir2022 Jul 10 '18 at 01:56
  • NB this ONLY works because fofo is an alias or typedef ! Were it a variable or a real type ,this wouldn't work ! – engf-010 Jul 10 '18 at 02:00
  • when I added the lib1wrapped.h file to a header file in my project, it generates other errors (C2371 error : redefinition; different basic types) for many other variables. I wrapped some of these variable as you suggested for fofo but it generates even more errors. I am wondering if there is a better way... – loisir2022 Jul 10 '18 at 15:18
  • Surprisingly when I added this header to the *.cpp it doesn't cause any problem... – loisir2022 Jul 10 '18 at 15:25
  • A better solution for this is to split your usage of the 2 (or at least the usage of one of them) conflicting headers into separate sourcefiles in which you use either one but not both headers. However ,it may turn out that combining the 2 libraries will cause linker errors or ODR-usage violations (these may or may not be diagnosed). Then you might solve your problem by wrapping your usage of one library into a DLL. – engf-010 Jul 10 '18 at 16:00