Skip to content

Instantly share code, notes, and snippets.

@cristicretu
Last active May 15, 2024 04:08
Show Gist options
  • Save cristicretu/ceceeff14ff6335959274dfe8b4e7061 to your computer and use it in GitHub Desktop.
Save cristicretu/ceceeff14ff6335959274dfe8b4e7061 to your computer and use it in GitHub Desktop.

Then install qt and cmake:

brew install qt
brew install cmake
  • Create a new CMake project, with this CMakeLists.txt:
cmake_minimum_required(VERSION 3.23)
project(myproject)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_PREFIX_PATH "/opt/homebrew/opt/qt")
# or /local/opt/qt if you have an intel mac

find_package(Qt6 COMPONENTS
        Core
        Gui
        Widgets
        REQUIRED)

# add here anything else you'd want
# this is manual ->add_executable(myproject main.cpp domain.h domain.cpp repository.cpp repository.h service.cpp service.h gui.cpp gui.h)

# this grabs all .h and .cpp files
include_directories(headers)

file(GLOB_RECURSE SOURCES "src/*.cpp")

file(GLOB_RECURSE HEADERS "headers/*.h")

add_executable(${PROJECT_NAME} main.cpp ${SOURCES} ${HEADERS})

target_link_libraries(${PROJECT_NAME}
        Qt::Core
        Qt::Gui
        Qt::Widgets
        )
VSCode Create the `build` folder, and configure CMake: ```bash mkdir build && cd build && cmake .. ```

Compile and run:

make && ./myproject

For getting intellisense in VSCode, configure the project with CMakeTools + hit ⌘ + Shift + p, type >C/C++: Edit Configurations (JSON), and inside the includePath, add this:

 "includePath": [
        "${workspaceFolder}/**",
        "/opt/homebrew/opt/qt/include",
        "/opt/homebrew/opt/qt/include/QtCore",
        "/opt/homebrew/opt/qt/include/QtGui",
        "/opt/homebrew/opt/qt/include/QtWidgets"
],
CLion
    - Clean the build directory:
            - Go to Build -> Clean
    - Manually delete the build directory (if necessary):
            - Quit CLion
            - Delete the `cmake-build-debug` (or equivalent) build directory from the project dir

Optionally, if something doesn't work, run this export Qt6_DIR="/opt/homebrew/opt/qt/lib/cmake/Qt6"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment