Solved

error: conflicting types for 'BOOL'

Posted on 2014-02-06
6
967 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
ID: 39839491
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
ID: 39839688
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
ID: 39839721
>>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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 167 total points
ID: 39840256
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
ID: 39840748
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
ID: 39864772
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
smtp c source code 7 47
LINUX, CPANEL & WHM 5 44
How to find Linux Server's last patch date 9 37
AWS ELB 5 17
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

863 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

23 Experts available now in Live!

Get 1:1 Help Now