Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve conflicting declaration of two header files of two libraries?

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.

like image 874
Hans Avatar asked Jun 15 '17 13:06

Hans


2 Answers

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.

like image 176
Marco Avatar answered Sep 27 '22 21:09

Marco


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;
like image 42
raullalves Avatar answered Sep 27 '22 21:09

raullalves