Skip to content

Instantly share code, notes, and snippets.

@jabarragann
Last active October 1, 2023 16:23
Show Gist options
  • Save jabarragann/91b549a810a6d69a0a1d61d8635249eb to your computer and use it in GitHub Desktop.
Save jabarragann/91b549a810a6d69a0a1d61d8635249eb to your computer and use it in GitHub Desktop.
QT minimal example with both make and cmake

QT minimal example

Minimal QT example tested on Windows WSL2 Ubuntu 20.04.

Requirements

sudo apt update
sudo apt install qt5-default cmake
sudo apt install qtbase5-dev

Building options

The project provides instructions to build the GuiApp using make or cmake. All instructions assume you start at the project's root.

Cmake

mkdir build
cd build
cmake ..
make
./build/MinimalQt5App

Make

make
./build_make/MinimalQt5App

If header files are not found, you can use the following command to find the correct path:

find /usr/include/ -type f -name "*.h" -path "*/qt5/*"

Resources

cmake_minimum_required(VERSION 3.5)
project(MinimalQt5App)
# Find the required Qt5 components
find_package(Qt5 REQUIRED Widgets)
# Add the executable
add_executable(${PROJECT_NAME} main.cpp)
# Link the Qt5 libraries to your application
target_link_libraries(${PROJECT_NAME} Qt5::Widgets)
#include <QApplication>
#include <QWidget>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// Create a main window
QWidget window;
// Create a push button with text
QPushButton button("Click me!", &window);
// Set the size of the window
window.resize(300, 200);
// Show the window
window.show();
return app.exec();
}
# Makefile for MinimalQt5App
# Compiler
CXX = g++
# Compiler flags
CXXFLAGS = -std=c++11
# Qt5 libraries and includes
QT_LIBS = -L/usr/lib/x86_64-linux-gnu -lQt5Quick -lQt5PrintSupport -lQt5Qml -lQt5Network -lQt5Widgets -lQt5Gui -lQt5Core
QT_INCLUDES = -I/usr/include/x86_64-linux-gnu/qt5/ -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets/ -I/usr/include/x86_64-linux-gnu/qt5/QtCore
# Output directory for object files and executable
BUILD_DIR = build_make
# Source files
SRCS = main.cpp
# Object files
OBJS = $(addprefix $(BUILD_DIR)/,$(SRCS:.cpp=.o))
# Executable name
TARGET = MinimalQt5App
# Make rules
all: $(TARGET)
$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $(BUILD_DIR)/$(TARGET) $(OBJS) $(QT_LIBS)
$(BUILD_DIR)/%.o: %.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -c $< -o $@ $(QT_INCLUDES) -fPIC
clean:
rm -rf $(BUILD_DIR)
.PHONY: all clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment