0

I an developing a lib for use on an embedded platform. I have code in a header that is part of the lib with typdef enum bool {false, true} bool;

If the lib user has already defined a type named bool, how can I code this so that my lib does not attempt to re-declare it?

Currently I have used #defines

#ifndef _BOOL 
#define _BOOL 
typedef enum bool{...
#endif

however this depends on a user that has bool defined also defining _BOOL

Is there a way of checking if types`with specific names already exist?

(Note this is a C Question, not C++, and neither I nor my assumed lib user is using stdbool, Ta)

Toby
  • 9,696
  • 16
  • 68
  • 132
  • If the platform is embedded, you should already know if `bool` exists, shouldn't you? – WhozCraig Apr 21 '13 at 19:46
  • Not if I create the lib and it is called by another author's application (on same platform) that may define bool itself?? – Toby Apr 22 '13 at 12:47

2 Answers2

5

You can't.

Your library probably should just define its own, distinct boolean type:

typedef enum { libname_false, libname_true } libname_bool;

Within your library implementation you could alias those to more convenient names as you desire.

Incidentally, you should not name preprocessor macros with leading underscores; those names are reserved for the compiler.

Community
  • 1
  • 1
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
1

I would go a way you going now, supplying some kind of your_lib_config.h with #defines like HAS_BOOL_DEFINED or anything similar which affects your library configuration. So user will be responsible to adjust settings in config header before using your library.

evilruff
  • 3,947
  • 1
  • 15
  • 27