How to link a library .a or dll in my project, with cmake in Qt and windows
Solved
General and Desktop
-
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.
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.
-
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}")
Now I get this error.
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})