How to build a simple C++ QT6 program.

A cmake configuration file "CMakeLists.txt" :
Code:
cmake_minimum_required(VERSION 3.16)
project(LabelExample VERSION 1.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find the Qt 6 Widgets module
find_package(Qt6 REQUIRED COMPONENTS Widgets)

qt_add_executable(LabelExample main.cpp)

# Link against the Qt 6 Widgets library
target_link_libraries(LabelExample PRIVATE Qt6::Widgets)

# Recommended: specific to a target
target_include_directories(LabelExample PUBLIC /usr/local/include)

# Alternative: global for all targets in this directory
include_directories(/usr/local/include)

The application "main.cpp" :
Code:
#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // Create a label with your desired text
    QLabel label("Hello from Qt 6!");
    
    // Optional: resize the label window
    label.resize(300, 100);
    label.setAlignment(Qt::AlignCenter);
    
    label.show();

    return app.exec();
}

A shell script to build & run :
Code:
rm CMakeCache.txt
rm -rf CMakeCache.txt CMakeFiles/
rm Makefile
rm -vfR ./build
mkdir   ./build
cd      ./build
# specify source and target directory
cmake -S .. -B ./build/ ..
cmake -DCMAKE_CXX_COMPILER=clang++20 -DCMAKE_C_COMPILER=clang20 ..
cmake --build .
./LabelExample
 
Back
Top