6

I am creating a GUI to control hardware with Qt Creator on Ubuntu 14.04. I have a class to control a camera (camera.h) and a class to control a light source that is connected to a USB RS232 serial converter (light.h). The two header files of this classes include headers provided by the manufacturer: uEye.h and ftdi2xx.h for the camera and the serial converter, respectively. Both libraries work perfectly if I use them separately. However when I try to include them in to my mainwindow.h I get following error messages (its around 14):

/home/g/Desktop/release/WinTypes.h:14: error: conflicting declaration
   'typedef unsigned int BOOL'
   typedef unsigned int BOOL;

/usr/include/uEye.h:1570: error: 'BOOL' has a previous declaration as
   'typedef int32_t BOOL'
   typedef int32_t BOOL;

and so on. What I understood from other posts, there seems to be no easy fix in C++. Any suggestions how to solve that (except using different hardware or having two separate programs)?

Update:

Finally I found a workaround, although it's still not the exact answer to my question. I did following: I went to the ftdi2xx.h file and commented the trouble causing #include WinTypes.h out. In light.h I included uEye.h first (I guess also this header includes some kind of WinTypes.h). Then I needed to add some missing typedef declarations that were not covert byuEye.h before including ftdi2xx.h. It works, however it is not a very clean and nice solution because it involves messing around with 3rd party stuff.

Hans
  • 141
  • 3
  • 10
  • 3
    You could wrap the 2 libraries, so that both headers are never included in the same translation unit: each would only be included in the implementation of your wrapper component. (I expect it would work, but whether or not you hit Undefined Behavior depends exactly what the 2 headers clash on.) – BoBTFish Jun 15 '17 at 13:13
  • Do you mean something like shown in this post: https://stackoverflow.com/questions/6670738/is-it-a-good-idea-to-wrap-an-include-in-a-namespace-block – Hans Jun 15 '17 at 13:24
  • 2
    Ehw, no, that's a terrible idea. I mean basically you create your own component providing the same functionality but with your own interface, that is implemented by using the existing library. It may be a bit of hassle, but gives you control over what names are exposed (and may be a good place to hide some boilerplate). – BoBTFish Jun 15 '17 at 13:41
  • @BoBTFish As in, include them in a separate implementation file and expose them through your own header? This is quite a hassle – Passer By Jun 15 '17 at 13:43

3 Answers3

4

Encapsulation to the rescue: Re-write your camera.h and light.h so that they do not include any headers of the respective library. That is an implementation detail, which should be hidden from users of those classes.

To achieve this you can either create real interfaces, use PIMPL or - if possible - just forward declare some things.

Marco
  • 41
  • 1
2

One solution is to adapt the libraries definition of BOOL, like this

#ifndef BOOL //if BOOL not defined yet
#define BOOL 

Other way is in your code, right after including both files, you could define your own BOOL that could cause no problems for both of them.

#include "uEye.h"
#undef BOOL
#include "ftdi2xx.h"

Or also

#include "uEye.h"
#undef BOOL
#include "ftdi2xx.h"
#undef BOOL
typedef unsigned int BOOL;
raullalves
  • 836
  • 8
  • 20
-2

You could try to include the two files in different namespaces:

namespace foo {
#include "uEye.h"
}

namespace bar {
#include "ftdi2xx.h"
}

Then you have to access with the respectively namespace e.g. foo::BOOL.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
  • 2
    This will fail miserably if the headers includes other libraries. Understandably, most c++ stuff includes the standard libraries, and this will break. Not to mention when you have a non-header only library. – Passer By Jun 15 '17 at 13:39