Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Conditional #include depending on OS
QtWS25 Last Chance

Conditional #include depending on OS

Scheduled Pinned Locked Moved Solved General and Desktop
headerinclude
4 Posts 4 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jars121
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      }
      

      ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      3
      • SGaistS SGaist

        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
        }
        

        ?

        J Offline
        J Offline
        jars121
        wrote on last edited by
        #3

        @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 :)

        aha_1980A 1 Reply Last reply
        1
        • J jars121

          @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 :)

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt has to stay free or it will die.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved