I have subdir project :
//exaple.pro
TEMPLATE = subdirs
SUBDIRS += \
myLib \
executable_one \
executable_two
myLib
is static library with one function in it:
//MyLib.h
void foo();
//MyLib.cpp
#include "MyLib.h"
#include <iostream>
void foo()
{
# ifdef MY_DEFINE
std::cout << "MY_DEFINE is defined\n";
# else
std::cout << "MY_DEFINE is undefined\n";
# endif
}
And two other projects which are linking to this library executable_one
and executable_two
. In one I want to define MY_DEFINE
, in another one don't. So I've tried to add DEFINES += MY_DEFINE
in the executable_one.pro
file, but in both cases the output is "MY_DEFINE is undefined". I understand that myLib
was compiled without this flag before executable and then was just linked into it without changing, but is there any way to make the library be built twice and for one of the executables pass this compiler option to the library?
No way you can achieve this without compiling myLib
twice.
You must build the library twice (easiest is to have two .pro file for the library), one with DEFINES += MY_DEFINE
one without. Then, the two .pro files must define a different target (one could be myLib_my_define
, the other one, myLib_no_my_define
).
Later executable_one
will link with myLib_my_define
, and executable_two
will link with myLib_no_my_define
.
Obviously, an alternative is toi replace your pre-processing variable by a regular variable (have foo
take a boolean variable for the switch), but I suppose it's not possible to do that if you asked the question the way you asked it.