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. How to link a library .a or dll in my project, with cmake in Qt and windows
QtWS25 Last Chance

How to link a library .a or dll in my project, with cmake in Qt and windows

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 9.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.
  • L Offline
    L Offline
    lincoln
    wrote on 26 Aug 2021, 19:39 last edited by lincoln
    #1

    Hello, I have the following problem, when including an external library to my project, in Qt, when using qmake, it was only necessary to do:

    INCLUDEPATH + = "$$ PWD / mylib / include"
    LIBS + = -L "$$ PWD / mylib / lib"
    LIBS + = -lthislib -lotherlib
    

    but in cmake I have no idea how to do it.

    I have the following:

    cmake_minimum_required(VERSION 3.5)
    
    project(untitled2 VERSION 0.1 LANGUAGES CXX)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    # QtCreator supports the following variables for Android, which are identical to qmake Android variables.
    # Check https://doc.qt.io/qt/deployment-android.html for more information.
    # They need to be set before the find_package( ...) calls below.
    
    #if(ANDROID)
    #    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    #    if (ANDROID_ABI STREQUAL "armeabi-v7a")
    #        set(ANDROID_EXTRA_LIBS
    #            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
    #            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
    #    endif()
    #endif()
    
    find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
    find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
    
    set(PROJECT_SOURCES
            main.cpp
            widget.cpp
            widget.h
            widget.ui
    )
    
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib/include)
    add_library(MYLIB STATIC IMPORTED)
    
    
    if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
        qt_add_executable(untitled2
            MANUAL_FINALIZATION
            ${PROJECT_SOURCES}
        )
    else()
        if(ANDROID)
            add_library(untitled2 SHARED
                ${PROJECT_SOURCES}
            )
        else()
            add_executable(untitled2
                ${PROJECT_SOURCES}
            )
        endif()
    endif()
    
    target_link_libraries(untitled2 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
    target_link_libraries(untitled2 PRIVATE  ${MYLIB})
    
    set_target_properties(untitled2 PROPERTIES
        MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
        MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
        MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    )
    
    if(QT_VERSION_MAJOR EQUAL 6)
        qt_finalize_executable(untitled2)
    endif()
    

    I use include_directories, to add the .h files from my library and add_library to add the static library, and at the end target_link_libraries (untitled2 PRIVATE $ {MYLIB}),

    but I get this error when I run the program.

    5683c7fc-cf8c-4296-aab4-a014071159a4-image.png

    code of my main form:

    #include "widget.h"
    #include "./ui_widget.h"
    #include <QMessageBox>
    #include "xlsxdocument.h"
    #include "xlsxchartsheet.h"
    #include "xlsxcellrange.h"
    #include "xlsxchart.h"
    #include "xlsxrichstring.h"
    #include "xlsxworkbook.h"
    
    using namespace QXlsx;
    
    Widget::Widget(QWidget *parent)
      : QWidget(parent)
      , ui(new Ui::Widget)
    {
      ui->setupUi(this);
      QXlsx::Document document;
      document.write("A1","Hello Qt!");
      if(!document.saveAs("D:\\ejmplo.xlsx")){
        QMessageBox::critical(this,qApp->applicationName(),"Error al guardar el archivo.\n");
        return;
      }
      QMessageBox::information(this,qApp->applicationName(),"Archivo guardado.");
    }
    
    Widget::~Widget()
    {
      delete ui;
    }
    

    Any suggestions as to why this error appears, or what is the correct way to include third party libraries in cmake, thanks in advance.

    Solitary wolf

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JoeCFD
      wrote on 26 Aug 2021, 20:27 last edited by JoeCFD
      #2
      unset(MY_LIB CACHE)
      FIND_LIBRARY( MY_LIB NAMES libthislib.so
                                      PATHS ${PWD}/mylib <===check PWD usage here
                                      DOC "MY_LIB library to link with"
                                      NO_SYSTEM_ENVIRONMENT_PATH)
      target_link_libraries(${PROJECT_NAME} "${MY_LIB}")
      
      L 1 Reply Last reply 26 Aug 2021, 21:30
      0
      • J JoeCFD
        26 Aug 2021, 20:27
        unset(MY_LIB CACHE)
        FIND_LIBRARY( MY_LIB NAMES libthislib.so
                                        PATHS ${PWD}/mylib <===check PWD usage here
                                        DOC "MY_LIB library to link with"
                                        NO_SYSTEM_ENVIRONMENT_PATH)
        target_link_libraries(${PROJECT_NAME} "${MY_LIB}")
        
        L Offline
        L Offline
        lincoln
        wrote on 26 Aug 2021, 21:30 last edited by lincoln
        #3

        @JoeCFD

        Now I get this error.

        0ae471ad-c967-4426-98c3-45e42504512a-image.png

        I take it back so I stay at the end and if it worked, thanks man.

        include_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib/include)
        unset(MYLIB CACHE)
        find_library(MYLIB NAMES libQXlsx.a PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib NO_SYSTEM_ENVIROMENT_PATH)
        target_link_libraries(untitled2 PRIVATE  ${MYLIB})
        

        Solitary wolf

        1 Reply Last reply
        0

        2/3

        26 Aug 2021, 20:27

        • Login

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