Solved

error: conflicting types for 'BOOL'

Posted on 2014-02-06
6
921 Views
Last Modified: 2014-02-25
Hi Experts,

I am using two different libraries and they both have BOOL defined.  I am getting multiple definition(actually conflicting type) errors.  How can I fix this without changing the library code?

Thank you.
0
Comment
Question by:ambuli
6 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Any chance to separate the code that uses each of them into different source files? That would solve the problem in a simple way.

Also: How do both 'typdef' their 'BOOL' types? If both are somewhat compatible (i.e. the types' sizes match), there might be a workaround.
0
 

Author Comment

by:ambuli
Comment Utility
Yes, I am using one of the library in only one file.  I was able to do away with the compile error
using :
#ifdef BOOL
#undef BOOL
#include "newlibrary.h"
#endif

However, I am not sure if this would be okay.

They are typedef like below.
typedef unsigned char BOOL;
typedef int BOOL;
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>However, I am not sure if this would be okay.

'#undef' won't take care of a user-defined 'typedef', only expressions created with a '#define' would be affected.

And 'int' and 'unsigned char' aren't really compatible. If passed to one of the libs' functions, there easily might be a problem. Without recompoiling one of the libs, I can only think of strictly separating their use into different compilation units i.g. source files. If they aren't used together in one unit, they'll not cause trouble.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 167 total points
Comment Utility
You absolutely will have to modify the library headers if any source file needs to use both of them. So you might as well do a decent job:
Both libraries have bad definitions for BOOL. For C programs you want
typedef enum
{
  false,
  true
} BOOL;

Open in new window

(this assumes you assign values false or true to BOOL variables). The benefit you get is that when running gdb to debug the program, it will shiow the values of BOOL variables as "true" or "false" (rather than "1" or "0" if they were integer).
Also, you only ever want to typedef it once. To achieve that as well
#ifndef BOOL_TYPEDEF_DONE
$define BOOL_TYPEDEF_DONE
typedef enum
{
  false,
  true
} BOOL;
#endif

Open in new window

Change both library header files as above. Remove #define lines for true and false. If using something else (e.g. TRUE & FALSE), remove those lines and change the enum to use whatever identifiers are expected.
You need to recompile the libraries afterwards
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 166 total points
Comment Utility
If you can't modify and rebuild either of these libraries the simplest solution would be to write an abstraction layer (a proxy) for each and build them a shared libraries. The proxy will need to marshal whatever types your using to represent BOOL and true/false to whatever type/value is being used by the library. Your main code base will then need to interface to these libraries as DSOs (Dynamic Shared Objects) via your abstraction (proxy) interface.

If you need to link both of them as static libraries you are going to have to change at least one of them. If you are going to change the libraries and you are building with C99 support you'd be better off just typedef'ing BOOL to _Bool, which is a proper boolean type in C99. Unfortunately, ANSI C doesn't have real boolean types so, in that case, what Duncan suggests is probably your best bet.
0
 
LVL 12

Accepted Solution

by:
satsumo earned 167 total points
Comment Utility
This is a problem with BOOL, unless the language has its own definition you get endless conflicting versions of how it should be defined. I've seen TRUE defined as 1, 1 as an enum (as above), -1, 0xFFFFFFFF, !FALSE, !0, !NULL. I don't define BOOL, TRUE or FALSE directly, I regard it is an unnecessary complication that cause issues like this. In my book something is zero or it isn't zero, you don't need a define or a header and zero is 100% portable. That's how the processor evaluates booleans now matter what people call them.

In this case you have two definitions of a macro with the same name but used in different contexts. You cant reliably make them the same because the two contexts might define the macro as different values. I would rename at one of those definitions. If that second, less used bool is from a library called foo.lib with foo.h I would call it FOO_BOOL and change all the uses in its header file to match.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now