Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preprocessor definition duplication

I have two libraries and unfortunately they define two identical preprocessor definitions (which I need to use):

lib1.h

#define MYINT 1

lib2.h

#define MYINT 2

In my program I need to use both of them:

#include <Lib1.h>
#include <lib2.h>
...
int myint = MYINT;

And here I have error that MYINT can't be resolved.

How can I solve that when I cannot modify the lib files?

like image 765
DMan Avatar asked Jan 10 '19 11:01

DMan


2 Answers

You might #undef MYINT before to include the header as workaround.

#undef MYINT
#include <Lib1.h>
const int myint_lib1 = MYINT; // 1

#undef MYINT
#include <lib2.h>
const int myint_lib2 = MYINT; // 2
like image 95
Jarod42 Avatar answered Sep 28 '22 18:09

Jarod42


Get the MYINT value of the first library before the second one replaces it.

#include <Lib1.h>
int myInt1 = MYINT;
#undef MYINT
#include <lib2.h>
int myInt2 = MYINT;
#undef MYINT

Of course, that doesn't work if MYINT is something dynamic and you need to keep its actual content around.

Edited by handy999: no semicolon at the end of preprocessor statements.

like image 22
Blaze Avatar answered Sep 28 '22 19:09

Blaze