Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

qt6 and cmake equivalent of qt5 and qmake



  • Hey!
    I have made three libraries in Qt that can be used in different programs. One is to check for updates, one is able to update (Linux only) and the third is a homemade variant of QFontDialog.

    With qmake in Qt 5 it worked like this to include the libraries (*.pro file)
    unix:LIBS += "-L../lib"
    unix:LIBS += -lcheckupdate
    unix:LIBS += -lupdateappimage
    unix:LIBS += -lselectfont

    win32:CONFIG(release, debug|release): LIBS += -L../lib/ -lcheckupdate # Release
    else:win32:CONFIG(debug, debug|release): LIBS += -L../lib/ -lcheckupdated # Debug
    win32:CONFIG(release, debug|release): LIBS += -L../lib/ -lselectfont # Release
    else:win32:CONFIG(debug, debug|release): LIBS += -L../lib/ -lselectfont # Debug

    INCLUDEPATH += "../include"
    DEPENDPATH += "../include"

    How to do with cmake? Have tried with Qt Creator. But the option to add a library is disabled.



  • target_link_libraries and target_include_directories is what you are looking for but, since you are the owner of the libraries, the better practice is to export the targets of your library so that users of your library can consume it easily with CMake.

    If you take this example and run (I assume linux here)

    mkdir build
    cd build
    cmake -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=RELEASE  ../
    cmake --build . --target install
    

    Then you can use the library in your application with the familiar find_package(MyLib)/target_link_libraries(MyApp MyLib::MyLib)



  • Thanks so much!
    I have downloaded your example and started exploring how it works. I made it work by starting from a "CMakeLists.txt" created by Qt Creator. It seems to work. But I need to learn cmake, so I thank you for your example. After 30 years of programming with qmake, it takes some time to learn new things.

    Create a *.so or a *.dll file
    Qt5
        else()
             # add_executable(myProgram
    		 # Changed to
             add_library(myLib SHARED
                ${PROJECT_SOURCES}
            )
    		
    Qt6
    
    if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
         # qt_add_executable(myProgram
    	 # Changed to
         add_library(myLib SHARED
            MANUAL_FINALIZATION
            ${PROJECT_SOURCES}	
    		
    		
    Use the library file (Only tested with Qt6 and Windows)
    
    target_link_libraries (myTestProgram PRIVATE $ {PROJECT_SOURCE_DIR} /lib/myLib.dll)
    
    
    Or include the library file in a project that uses qmake
    
    win32:CONFIG(release, debug|release): LIBS += -L../lib/ -ldownloadunpack  # Release
    else:win32:CONFIG(debug, debug|release): LIBS += -L../lib/ -ldownloadunpackd # Debug
    unix:LIBS += -ldownloadunpack
    

Log in to reply