swaechter
swaechter

Reputation: 1439

Use library and include base directory of a Qt5 installation with CMake

I migrated my project from Qt4 to Qt5. In Qt4 in combination with CMake there were the variables QT_INCLUDE_DIR and QT_LIBRARY_DIR. They are missing in Qt5. What I need is a replacement for these two variables because I have a few headers/libraries located in the Qt installation (Like QScintilla2)

I use Qt5Widgets as module and my first idea was to use this path and use a .. to get the parent path - but a Qt5Widgets_LIBRARIES returns the library (name) and not the path (Cut off the library ?). Also the return value of Qt5Widgets_INCLUDE_DIRS looks like one huge (appended) path (without whitespaces between different include directories)

So - any ideas or solutions ? The solution should be compatible for Unix/Linux, OS X and Windows

Upvotes: 0

Views: 3152

Answers (1)

swaechter
swaechter

Reputation: 1439

After playing around with CMake I came to the follwing solution. I search for includes and after I found them I use the library path of Qt5Widgets. In this case this works without a problem beaucause QScintilla installs itself in the Qt directory (and in the lib dir under Linux):

# Module file for QScintilla - compiled with Qt5. These variables are available:
# QSCINTILLA2_FOUND = Status of QScintilla
# QSCINTILLA2_INCLUDE_DIR = QScintilla include dir
# QSCINTILLA2_LIBRARY = QScintilla library

# Check
if(${Qt5Widgets_FOUND})

    # Set as not found
    set(QSCINTILLA2_FOUND false)

    # Iterate over the include list of the Qt5Widgets module
    foreach(TEMPPATH in ${Qt5Widgets_INCLUDE_DIRS})

        # Check for a Qsci directory
        find_path(QSCINTILLA2_INCLUDE_DIR qsciglobal.h ${TEMPPATH}/Qsci)

        # Found - break loop
        if(QSCINTILLA2_INCLUDE_DIR)
            break()
        endif()

    endforeach()

    # Check
    if(QSCINTILLA2_INCLUDE_DIR)

        # Get Qt5Widgets library and cut off the library name
        get_target_property(QT5_WIDGETSLIBRARY Qt5::Widgets LOCATION)
        get_filename_component(QT5_WIDGETSLIBRARYPATH ${QT5_WIDGETSLIBRARY} PATH)

        # Add library
        set(LIBRARYPATH ${QT5_WIDGETSLIBRARYPATH} "/usr/lib/" "/usr/local/lib")
        find_library(QSCINTILLA2_LIBRARY NAMES libqscintilla2.a qscintilla2.lib PATHS ${LIBRARYPATH})

        # Check
        if(QSCINTILLA2_LIBRARY)
            # Enable library
            set(QSCINTILLA2_FOUND true)
            mark_as_advanced(QSCINTILLA2_INCLUDE_DIR QSCINTILLA2_LIBRARY)
        else()
            message(FATAL_ERROR "QScintilla2 library not found")
        endif()

    else()
        message(FATAL_ERROR "Cannot find QScintilla2 header")
    endif()

else()
    message(FATAL_ERROR "Qt5Widgets module not found")
endif()

Upvotes: 3

Related Questions