ElevenJune
ElevenJune

Reputation: 457

Use qmake variable in #include preprocessor command

I have a project coreApp using two libraries lib1 and lib2.

Each lib has a file Version.h specifying the lib version, each in the library namespace, example :

namespace lib1{
    static const QString Version = "1.2";
}

In my coreApp .pro I have :

LIB1_PATH_VAR = $$(LIB1_PATH) (LIB1_PATH is an environment variable storing the path to the root directory of lib1)
LIBS += -L$$LIB1_PATH_VAR/bin/ -l1
LIB2_PATH_VAR = $$(LIB2_PATH)
LIBS += -L$$LIB2_PATH_VAR/bin/ -l2

I want to do something like this in my main.cpp of coreApp :

#include "$$LIB1_PATH_VAR/Version.h"
#include "$$LIB2_PATH_VAR/Version.h"

std::cout<<lib1::Version<<std::endl;
std::cout<<lib2::Version<<std::endl;

It would allow me to be independent from the library path (in the code) while using classes that have the same name in both libraries (here Version.h).

Would it be possible ?

Current Solution :

INCLUDEPATH += LIB1_PATH/..

to my coreApp.pro So that my main.cpp can look like this :

#include "path/to/lib1/Version.h"
#include "path/to/lib2/Version.h"

std::cout<<lib1::Version<<std::endl;
std::cout<<lib2::Version<<std::endl;

The problem is that the library path in written in the code, if I move the library to another location I have to change all my #include in the code

Upvotes: 0

Views: 196

Answers (1)

Matt
Matt

Reputation: 15196

Would it be possible ?

Yes, qmake supports that kind of preprocessing using QMAKE_SUBSTITUTES. Some silly example code:

my.pro

mysubst.input = myfile.cpp.in
mysubst.output = myfile.cpp
QMAKE_SUBSTITUTES += mysubst

myfile.cpp.in

// all $$<expr> stuff is properly evaluated and substituted;
// !!IF / !!ELIF / !!ELSE / !!ENDIF processed as expected;
// quotes and backslashes must be escaped;
// anything else is passed through

#include <iostream>
!!IF !isEmpty(some_var)
#include \"$${some_var}\"
!!ENDIF
// more c++ code...

Upvotes: 1

Related Questions