diff --git a/.gitignore b/.gitignore index 7f4826b..3da1f63 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,12 @@ +# Build directories +build/ +build-*/ +bin/ +out/ + +# CodeQL analysis artifacts +_codeql_detected_source_root + # C++ objects and libs *.slo *.lo diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e012a83 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C++ Launch (GDB/MinGW)", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/bin/QtMinGWExample.exe", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "gdb.exe", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "preLaunchTask": "CMake Build" + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6c0a7b1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,17 @@ +{ + "cmake.configureOnOpen": true, + "cmake.buildDirectory": "${workspaceFolder}/build", + "cmake.generator": "MinGW Makefiles", + "cmake.preferredGenerators": [ + "MinGW Makefiles", + "Ninja" + ], + "files.associations": { + "*.h": "cpp", + "*.cpp": "cpp" + }, + "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools", + "cmake.configureArgs": [ + "-DCMAKE_PREFIX_PATH=${env:Qt6_DIR};${env:Qt5_DIR}" + ] +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..89275ae --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,68 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "CMake Configure", + "type": "shell", + "command": "cmake", + "args": [ + "-B", + "${workspaceFolder}/build", + "-G", + "MinGW Makefiles", + "-DCMAKE_BUILD_TYPE=Debug" + ], + "group": { + "kind": "build", + "isDefault": false + }, + "presentation": { + "reveal": "always", + "panel": "shared" + }, + "problemMatcher": [] + }, + { + "label": "CMake Build", + "type": "shell", + "command": "cmake", + "args": [ + "--build", + "${workspaceFolder}/build", + "--config", + "Debug" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "presentation": { + "reveal": "always", + "panel": "shared" + }, + "problemMatcher": [ + "$gcc" + ], + "dependsOn": [ + "CMake Configure" + ] + }, + { + "label": "CMake Clean", + "type": "shell", + "command": "cmake", + "args": [ + "--build", + "${workspaceFolder}/build", + "--target", + "clean" + ], + "group": "build", + "presentation": { + "reveal": "always", + "panel": "shared" + }, + "problemMatcher": [] + } + ] +} diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d3e0807 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,80 @@ +cmake_minimum_required(VERSION 3.16) + +project(QtMinGWExample VERSION 1.0.0 LANGUAGES CXX) + +# Set C++ standard +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Qt configuration +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +# Find Qt packages +find_package(Qt6 COMPONENTS Core Widgets QUIET) +if (NOT Qt6_FOUND) + find_package(Qt5 COMPONENTS Core Widgets REQUIRED) + set(QT_VERSION_MAJOR 5) +else() + set(QT_VERSION_MAJOR 6) +endif() + +# Include directories +include_directories(${CMAKE_SOURCE_DIR}/include) + +# Source files +set(SOURCES + src/main.cpp + src/mainwindow.cpp +) + +# Header files +set(HEADERS + include/mainwindow.h +) + +# UI files +set(UI_FILES + src/mainwindow.ui +) + +# Create executable +if(QT_VERSION_MAJOR EQUAL 6) + qt_add_executable(${PROJECT_NAME} + MANUAL_FINALIZATION + ${SOURCES} + ${HEADERS} + ${UI_FILES} + ) +else() + add_executable(${PROJECT_NAME} + ${SOURCES} + ${HEADERS} + ${UI_FILES} + ) +endif() + +# Link Qt libraries +if(QT_VERSION_MAJOR EQUAL 6) + target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core Qt6::Widgets) +else() + target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Widgets) +endif() + +# Finalize executable for Qt6 +if(QT_VERSION_MAJOR EQUAL 6) + qt_finalize_executable(${PROJECT_NAME}) +endif() + +# Set output directory +set_target_properties(${PROJECT_NAME} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin +) + +# Platform specific settings for MinGW +if(MINGW) + message(STATUS "Building with MinGW") + # Add MinGW specific flags if needed + target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra) +endif() diff --git a/README.md b/README.md index 3b96a52..2656fab 100644 --- a/README.md +++ b/README.md @@ -1 +1,166 @@ -# vscode-cmake-cpp-mingw-exe \ No newline at end of file +# VSCode CMake C++ Qt MinGW Template + +A template project for developing Qt C++ applications using VSCode, CMake, and MinGW compiler. + +## Prerequisites + +### Required Tools + +1. **Visual Studio Code** + - Download from: https://code.visualstudio.com/ + +2. **VSCode Extensions** + - C/C++ Extension Pack (ms-vscode.cpptools) + - CMake Tools (ms-vscode.cmake-tools) + - CMake Language Support (twxs.cmake) + +3. **MinGW-w64** + - Download from: https://www.mingw-w64.org/ + - Or use MSYS2: https://www.msys2.org/ + - Make sure `gcc`, `g++`, and `mingw32-make` are in your PATH + +4. **CMake** + - Download from: https://cmake.org/download/ + - Version 3.16 or higher + - Add to system PATH + +5. **Qt Framework** + - Download Qt 5.15+ or Qt 6.x from: https://www.qt.io/download + - Make sure to install MinGW-compatible version + - Set environment variable: `Qt5_DIR` or `Qt6_DIR` pointing to Qt CMake modules + - Example: `C:\Qt\6.5.0\mingw_64\lib\cmake\Qt6` + +## Project Structure + +``` +vscode-cmake-cpp-mingw-exe/ +├── .vscode/ # VSCode configuration +│ ├── settings.json # Editor and CMake settings +│ ├── tasks.json # Build tasks +│ └── launch.json # Debug configuration +├── include/ # Header files +│ └── mainwindow.h +├── src/ # Source files +│ ├── main.cpp +│ ├── mainwindow.cpp +│ └── mainwindow.ui # Qt UI file +├── CMakeLists.txt # CMake build configuration +└── README.md +``` + +## Setup Instructions + +### 1. Clone the Repository + +```bash +git clone https://github.com/JayTwoLab/vscode-cmake-cpp-mingw-exe.git +cd vscode-cmake-cpp-mingw-exe +``` + +### 2. Configure Environment Variables + +Set the Qt installation path: + +**Windows (PowerShell):** +```powershell +$env:Qt6_DIR="C:\Qt\6.5.0\mingw_64\lib\cmake\Qt6" +# or for Qt5 +$env:Qt5_DIR="C:\Qt\5.15.2\mingw81_64\lib\cmake\Qt5" +``` + +**Windows (Command Prompt):** +```cmd +set Qt6_DIR=C:\Qt\6.5.0\mingw_64\lib\cmake\Qt6 +``` + +### 3. Open in VSCode + +```bash +code . +``` + +### 4. Configure CMake + +- VSCode will automatically prompt to configure the project +- Or press `Ctrl+Shift+P` and run "CMake: Configure" +- Select MinGW compiler kit when prompted + +### 5. Build the Project + +**Option A: Using VSCode** +- Press `Ctrl+Shift+B` to build +- Or press `F7` + +**Option B: Using Command Line** +```bash +mkdir build +cd build +cmake -G "MinGW Makefiles" .. +cmake --build . +``` + +### 6. Run the Application + +**Option A: Using VSCode** +- Press `F5` to build and debug +- Or press `Ctrl+F5` to run without debugging + +**Option B: Using Command Line** +```bash +./build/bin/QtMinGWExample.exe +``` + +## Building for Release + +```bash +mkdir build-release +cd build-release +cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release .. +cmake --build . +``` + +## Troubleshooting + +### CMake Cannot Find Qt + +If CMake cannot find Qt, ensure: +1. Qt is installed with MinGW support +2. `Qt5_DIR` or `Qt6_DIR` environment variable is set correctly +3. The path points to the CMake modules directory: `/lib/cmake/Qt[5|6]` + +### MinGW Not Found + +Make sure MinGW bin directory is in your PATH: +```powershell +$env:PATH += ";C:\Qt\Tools\mingw810_64\bin" +``` + +### Qt DLLs Not Found at Runtime + +Copy the required Qt DLLs to the executable directory or add Qt bin directory to PATH: +```powershell +$env:PATH += ";C:\Qt\6.5.0\mingw_64\bin" +``` + +## Features + +- ✅ CMake-based build system +- ✅ Qt 5 and Qt 6 support +- ✅ MinGW compiler support +- ✅ VSCode integrated development +- ✅ Debug configuration +- ✅ Simple Qt Widgets UI example + +## License + +This project is licensed under the MIT License - see the LICENSE file for details. + +## Contributing + +Feel free to submit issues and pull requests. + +## References + +- [Qt Documentation](https://doc.qt.io/) +- [CMake Documentation](https://cmake.org/documentation/) +- [VSCode C++ Documentation](https://code.visualstudio.com/docs/languages/cpp) \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..62ba33a --- /dev/null +++ b/build.bat @@ -0,0 +1,37 @@ +@echo off +REM Build script for Windows with MinGW + +echo Building Qt MinGW CMake Example... +echo. + +REM Create build directory if it doesn't exist +if not exist build mkdir build + +REM Navigate to build directory +cd build + +REM Configure with CMake +echo Configuring with CMake... +cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug .. +if errorlevel 1 ( + echo Configuration failed! + cd .. + exit /b 1 +) + +echo. +echo Building project... +cmake --build . +if errorlevel 1 ( + echo Build failed! + cd .. + exit /b 1 +) + +cd .. +echo. +echo Build successful! +echo Executable location: build\bin\QtMinGWExample.exe +echo. +echo To run the application, execute: +echo build\bin\QtMinGWExample.exe diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..6d005b6 --- /dev/null +++ b/build.sh @@ -0,0 +1,37 @@ +#!/bin/bash +# Build script for Linux/Mac + +echo "Building Qt CMake Example..." +echo "" + +# Create build directory if it doesn't exist +mkdir -p build + +# Navigate to build directory +cd build + +# Configure with CMake +echo "Configuring with CMake..." +cmake -DCMAKE_BUILD_TYPE=Debug .. +if [ $? -ne 0 ]; then + echo "Configuration failed!" + cd .. + exit 1 +fi + +echo "" +echo "Building project..." +cmake --build . +if [ $? -ne 0 ]; then + echo "Build failed!" + cd .. + exit 1 +fi + +cd .. +echo "" +echo "Build successful!" +echo "Executable location: build/bin/QtMinGWExample" +echo "" +echo "To run the application, execute:" +echo " ./build/bin/QtMinGWExample" diff --git a/include/mainwindow.h b/include/mainwindow.h new file mode 100644 index 0000000..28d6dde --- /dev/null +++ b/include/mainwindow.h @@ -0,0 +1,25 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private slots: + void onButtonClicked(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..da1768b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,15 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + MainWindow window; + window.setWindowTitle("Qt MinGW CMake Example"); + window.resize(400, 300); + window.show(); + + return app.exec(); +} diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp new file mode 100644 index 0000000..f036e09 --- /dev/null +++ b/src/mainwindow.cpp @@ -0,0 +1,23 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + // Connect button click signal to slot + connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::onButtonClicked); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::onButtonClicked() +{ + QMessageBox::information(this, "Hello", "Hello from Qt with MinGW!"); +} diff --git a/src/mainwindow.ui b/src/mainwindow.ui new file mode 100644 index 0000000..37b98b4 --- /dev/null +++ b/src/mainwindow.ui @@ -0,0 +1,77 @@ + + + MainWindow + + + + 0 + 0 + 400 + 300 + + + + MainWindow + + + + + + + Qt Application with CMake and MinGW + + + Qt::AlignCenter + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Click Me! + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + 0 + 0 + 400 + 21 + + + + + + + +