Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# Build directories
build/
build-*/
bin/
out/

# CodeQL analysis artifacts
_codeql_detected_source_root

# C++ objects and libs
*.slo
*.lo
Expand Down
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
17 changes: 17 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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}"
]
}
68 changes: 68 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
80 changes: 80 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
167 changes: 166 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,166 @@
# vscode-cmake-cpp-mingw-exe
# 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: `<Qt_Installation>/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)
Loading