Home > front end >  Qt: inject defines in a library
Qt: inject defines in a library

Time:10-30

I have a code like this:

myproject.pro

TEMPLATE = subdirs

DEFINES  = mydefine

SUBDIRS  = \
        libs/mylib \
        myapp

mylib.pro

contains(DEFINES, mydefine) {
  do something
}
else {
  do something else
}

Is there a way to inject the define "mydefine" to mylib? Thank you

CodePudding user response:

Unfortunately you can't do that.

Definitions are just compiler macros. Your IDE compiles your code by considering these macros. I mean:

If you write something like this:

int A = 5;
#define SOMETHING
#ifdef SOMETHIN //intentionally misspelled
A = 10;
#endif
std::cout << A ;

Your compiler will see :

int A = 5;
// compiler : Okayy.. I don't want to compile that
std::cout << A;

And it will generate an executable or library according to what it saw.

But what you actually can is compiling and generating two libraries and load one of them dynamically at run time. Or;

You again generate two different libraries called myLibwithDef, myLibwithoutDef and do something like this at where you want to use them:

#ifdef Something
LoadDynamicLibraryWithDef()
#else
LoadDynamicLibraryWithoutDef()
#endif
  • Related