Cpp qt: cmake

From wikinotes

CMake is the defacto cross platform build-management tool. It's very complicated to learn, and makes me a bit uneasy when it comes to maintaining it.


WARNING:

I haven't tested this on windows or OSX yet (only linux)

Project Structure:

project/
    include/
        mybutton.h
    src/
        mybutton.cpp
        main.cpp
    CMakeLists.txt
CMakeLists.txt
# https://www.kdab.com/using-cmake-with-qt-5/

# ================
# Project settings
# ================
CMAKE_MINIMUM_REQUIRED(VERSION 3.12)
PROJECT(qt_cmake_example)

# ============
# Qt5 settings
# ============
set(CMAKE_AUTOMOC ON)                  # handle `moc` for Qt targets
set(CMAKE_INCLUDE_CURRENT_DIR ON)      # include `CMAKE_CURRENT_SOURCE_DIR`, `CMAKE_CURRENT_BINARY_DIR`
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)  # export compile.json (used by linter)
find_package(Qt5Widgets REQUIRED)      # may list `COMPONENTS Core Gui ...`. (See /usr/lib/cmake)

# ================
# Compile Settings
# ================
include_directories(include)
add_executable(main src/main.cpp src/mybutton.cpp)

# compile `main` using libraries in variable ${Qt5Widgets_LIBRARIES}. 
# defined during `find_package(Qt5Widgets)` /usr/lib/cmake/Qt5Widgets/Qt5WidgetsConfig.cmake
target_link_libraries(main ${Qt5Widgets_LIBRARIES})
How to Build
cd project/
mkdir build
cmake ..
make