Conditional #include depending on OS



  • G'day,

    I'm developing a Qt-based application for an embedded Linux target device, cross-compiled from my primary development machine which runs Ubuntu. I also have a Macbook Air laptop, which I'd like to use to continue development when commuting and out of the office. Up until recently this has worked nicely, as all the functions I'd built were accessible under both Linux and Mac OSX. I'm now at the point where I'm interfacing with Linux kernel, GPIO, etc. elements, which aren't available on Mac OSX, so I need to be able to conditionally/selective include certain classes when compiling.

    I have the following setup in my main.cpp file:

    #include <iostream>
    //several other #include statements
    #ifdef Q_OS_LINUX
        #include "LinuxClass.h"
    #endif
    
    int main(int argc, char *argv[])
    {
    
    QQmlApplicationEngine engine;
    
    #ifdef Q_OS_LINUX
        engine.rootContext()->setContextProperty(QStringLiteral("LinuxClass"), new LinuxClass());
    #endif
    
    }
    

    Now, if my understanding is correct, when compiling under Mac OSX, both the header #ifdef and main() #ifdef statements will be false, so LinuxClass.h will not be included, nor will the LinuxClass rootContext property be set. However, when compiling, I receive compilation errors from within LinuxClass, which suggests that it has been included? I've checked all my other classes, and can confirm there are no other instances of including LinuxClass.

    Am I missing something simple here?


  • Lifetime Qt Champion

    Hi,

    Did you also put the corresponding files in a scope in your .pro file ?

    Something like:

    unix:!macx{
        HEADERS += LinuxClass.h
        SOURCES += LinuxClass.cpp
    }
    

    ?


  • Banned

    This post is deleted!


  • @SGaist said in Conditional #include depending on OS:

    Hi,

    Did you also put the corresponding files in a scope in your .pro file ?

    Something like:

    unix:!macx{
        HEADERS += LinuxClass.h
        SOURCES += LinuxClass.cpp
    }
    

    ?

    Perfect, thank you! I had poured over every single class and #include, and completely overlooked the .pro file :)


  • Qt Champions 2018

    @jars121 So please mark this thread as SOLVED now. Thanks.


 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.