Qt CLI 03 DLL SO
A dynamic link library will be created
Within the folder you have assigned to your Qt projects, create the following folder structure.
Inside the folder “sharedLibrary” create a file called CMakelists.txt with the following content:
cmake_minimum_required(VERSION 3.14)
add_subdirectory(myApp)
add_subdirectory(myLibrary)
Create a Library Project by specifying the myLibrary folder as the project folder.
Create a CLI project indicating the project folder as “myapp”
File myLibrary-CMakeLists:
cmake_minimum_required(VERSION 3.16)
project(myLibrary LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
add_library(myLibrary SHARED
myLibrary_global.h
mylibrary.cpp
mylibrary.h
)
target_link_libraries(myLibrary PRIVATE Qt${QT_VERSION_MAJOR}::Core)
target_compile_definitions(myLibrary PRIVATE MYLIBRARY_LIBRARY)
File myLibrary-mylibrary.h:
#ifndef MYLIBRARY_H
#define MYLIBRARY_H
#include «myLibrary_global.h»
#include <QDebug>
class MYLIBRARY_EXPORT MyLibrary
{
public:
MyLibrary();
void test();
};
#endif // MYLIBRARY_H
File myLibrary-mylibrary.cpp:
#include «mylibrary.h»
MyLibrary::MyLibrary() {}
void MyLibrary::test()
{
qInfo() << «Hola desde mi librería»;
}
File myApp-CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(myApp LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
add_executable(myApp
main.cpp
)
target_link_libraries(myApp Qt${QT_VERSION_MAJOR}::Core)
include(GNUInstallDirs)
install(TARGETS myApp myLibrary
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
File myApp-main.cpp:
#include <QCoreApplication>
#include «../myLibrary/myLIbrary_global.h»
#include «../myLibrary/mylibrary.h»
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyLibrary lib;
lib.test();
return a.exec();
}
