Qt CLI 01

A project will be created using cmake. You can see the most relevant captures:

CMakeLists.txt

# indicates that the cmake version must be >= 3.16

cmake_minimum_required(VERSION 3.16)

# indicates the name of the project and that it will be used c++

project(aprendiendoCLists01 LANGUAGES CXX)

# Enables automatic file processing ui files

set(CMAKE_AUTOUIC ON)

#Enables automatic file processing moc files (MetaObjectCompiler)

set(CMAKE_AUTOMOC ON)

# Enables automatic file processing resources files .qrc

set(CMAKE_AUTORCC ON)

# c++ version 17

set(CMAKE_CXX_STANDARD 17)

# mandatory use of C++17

set(CMAKE_CXX_STANDARD_REQUIRED ON)

#it is required (REQUIRED) and requires the Core module

#find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)

find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)

# indicates sources and executables

add_executable(aprendiendoCLists01

  main.cpp

)

# links the executable to the Core library

target_link_libraries(aprendiendoCLists01 Qt${QT_VERSION_MAJOR}::Core)

# includes the specified module that defines a set of variables,

# among them ${CMAKE_INSTALL_BINDIR}: Directory of binaries and

# ${CMAKE_INSTALL_LIBDIR}: Library directory.

include(GNUInstallDirs)

# TARGETS: directory install the generated executable.

# LIBRARY: For shared libraries

# RUNTIME: directory where the executable will be installed

install(TARGETS aprendiendoCLists01

    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}

    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}

)

main.cpp

#include <QCoreApplication>

#include <QDebug>

int main(int argc, char *argv[])

{

    /*

    Manages the event loop for CLI applications

    It is the core of any Qt application which does not require

    graphic interface (instead of QApplication,

    used for graphic applications

    */

    QCoreApplication a(argc, argv);

    qDebug() << «Hello world»;

    // starts the event loop and returns the result value

    return a.exec();

}

Qt CLI 02 IP

This project was created to obtain basic data from your computer’s network cards. Two things to keep in mind:
1) Your computer may have more than one network card.
2) There are two versions of IP addresses.

You need to include the Network library, so you’ll see some small changes you’ll need to make in CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)

project(QtCLI02_IP 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 Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Network)

add_executable(QtCLI02_IP
main.cpp
)
target_link_libraries(QtCLI02_IP Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Network)

include(GNUInstallDirs)
install(TARGETS QtCLI02_IP
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

The following modules will be included in main.cpp for later use:

#include <QCoreApplication>



#include <QHostAddress>
#include <QNetworkInterface>
#include <QAbstractSocket>


#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //A list will be created with the selected information
    //Each node in the list will be a network card
    //The QHostAddress and QNetworkInterface modules are used here
    QList<QHostAddress> list = QNetworkInterface::allAddresses();

    for(int i=0;i< list.count();i++){

        QHostAddress nic = list.at(i);

        //Gets the data from the network card "i"
        qInfo() << nic.toString();

        //Check if it is loopback
        if (nic.isLoopback())
            qInfo() << "\tLoopback" ;

        //Check if it is multicast
        if (nic.isMulticast())
            qInfo() << "\tMulticast";

        //Now the protocol used will be checked
            switch(nic.protocol()){

            case QAbstractSocket::UnknownNetworkLayerProtocol:
                qInfo() << "\tProtocol:Unknown";
                break;
            case QAbstractSocket::AnyIPProtocol:
                qInfo() << "\tProtocol: Any";
                break;
            case QAbstractSocket::IPv4Protocol:
                qInfo() << "\tProtocol: IPv4";
                break;
            case QAbstractSocket::IPv6Protocol:
                qInfo() << "\tProtocol:IPv6";
                break;
            }
    }

    return a.exec();
}

and that’s all folks!!!

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();

}