Important: Please read the Qt Code of Conduct -

reading #define from external header into .pro file

  • I have a project file in which I need to be able to specify whether to include certain source and header files depending on the build configuration. Normally, I'd use a combination of CONFIG and DEFINES to do this. However, my problem is that the value of the configuration needs to come from a #define which is specified in a common header file which is shared across projects, some of which do not use qt. I've tried using infile & fromfile, but neither is returning what I expect - I'm pretty sure they're parsing with the expectation of DEFINES, not #define.

    Does anyone know if what I'm trying to do is possible? (Or can point me towards what I'm doing wrong?)

    ps - I haven't mucked about with .pro files very much, so please forgive any stupid or obvious mistakes.

    here's an example of what I'm trying
    #define CONFIG_A 0
    #define CONFIG_B 1

    #define MODEL CONFIG_A
    QT += core gui widgets

    TARGET = MyProject
    TEMPLATE = app

    QMAKE_CXXFLAGS += -std=c++11 -Wall
    test = fromfile(defines.h,MODEL)
    message(Build configuration detected: $$test)

    the build output I see (which is what makes me suspect it's not parsing the preprocessor #define the way I want it to) is:
    defines.h(4): Extra characters after test expression.
    defines.h(6): Assignment needs exactly one word on the left hand side.

  • Lifetime Qt Champion

    Hi and welcome to the forums
    Im not 100% sure but i think $$fromfile() only likes .pro file not a (c++) .h
    and hence it do not understand it.

  • Lifetime Qt Champion

    @nishajones171 It sounds strange if your build configuration is coming from a header file. Usually it is other way around - build system defines variables which then are used in the code.

  • Agreed, it's a strange way to do it - it's done this way because it's a multiprocessor environment, and the other processor requires a completely separate IDE, and even within Eclipse, we have one QT project and one non-QT project, so using native CONFIG-type stuff won't work for the non-QT project.

  • @mrjj - yes, that's my understanding as well. And thanks :)

  • Lifetime Qt Champion

    Hi @nishajones171,

    If you could use environment variables: you can evalute them inside the .pro file by $$(VARIABLE)

    It should be possible to use environment variables in other build systems also.

  • @nishajones171 yes, as @aha_1980 mentioned, if you could use environment variables that could simplify things. The Qt project and the non-Qt project both will read and evaluate such variables. Just in case, see this entry for Qt qmake and this entry for Eclipse CDT (I assume you're using it as you mentioned "even Eclipse"

  • @aha_1980 That might work - I'll have to run it by the rest of the team, as it's more of a process change, but it does seem like a potential solution. Thanks!

  • @Pablo-J.-Rogina Appreciate the pointer towards how to do this in Eclipse - very helpful :) I looked around in the other IDE we use (an ancient flavor of IAR Embedded Workbench), and haven't found anything that'd read an environment variable, but it's possible they use a different name for it. In any case, thanks!

Log in to reply