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

I am creating a GUI for managing equipment with Qt Creator on Ubuntu 14.04. I have a class for controlling a camera ( camera.h ) and a class for controlling a light source that is connected to a USB to RS232 serial converter ( light.h ). Two header files of this class include headers provided by the manufacturer: uEye.h and ftdi2xx.h for the camera and serial converter, respectively. Both libraries work fine if I use them separately. However, when I try to include them in my mainwindow.h , I get the following error messages (about 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; 

etc. As I understood from other posts, in C ++, there seems to be no easy solution. Any suggestions for resolving them (with the exception of using other equipment or two separate programs)?

Update:

Finally, I found a workaround, although this is still not an exact answer to my question. I did the following: I went to the ftdi2xx.h file and commented on the problem that caused #include WinTypes.h . In light.h , I first included uEye.h (I think this header also includes some WinTypes.h ). Then I needed to add some missing typedef declarations that were not hidden by uEye.h before including ftdi2xx.h . It works, however, it is not a very clean and pleasant solution, because it involves a mess with third-party things.

+5
source share
3 answers

Encapsulation for help: Rewrite your cameras .h and light.h so that they do not include the headers of the corresponding library. This is an implementation detail that should be hidden from users of these classes.

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

+3
source

One solution is to adapt the definition of BOOL in libraries, for example

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

Another way is your code, immediately after including both files you can define your own BOOL, which will not cause 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; 
+1
source

You can try to include these two files in different namespaces:

 namespace foo { #include "uEye.h" } namespace bar { #include "ftdi2xx.h" } 

Then you need to access with the appropriate namespace, for example. foo::BOOL .

-2
source

Source: https://habr.com/ru/post/1268871/


All Articles