This guide walks through setting up a complete development environment on Windows using Visual Studio Code, MinGW, and CMake to build C/C++ projects from scratch.
- Required Software Downloads
1.1 VSCode
Download from the official site: https://code.visualstudio.com/
Choose the appropriate architecture (64-bits standard for most modern Windows machines). Run the installer and follow the prompts.
1.2 CMake
Available at: https://cmake.org/download/
Select the Windows installer package (.msi) suitable for your system architecture.
1.3 MinGW
Download from: https://osdn.net/projects/mingw/releases/
Alternatively, MSYS2 provides a more complete development environment with package management capabilities.
- Environment Configuration
2.1 VSCode Installation
Execute the downloaded installer. Modify the installation path if desired, but default settings work well for most users. After installation, launch VSCode.
2.2 CMake Path Configuration
To make CMake accessible from any command prompt location:
- Right-click "This PC" and select Properties
- Click "Advanced system settings"
- Navigate to the Advanced tab and click "Environment Variables"
- Locate the Path variable and click Edit
- Add the path to CMake's bin directory (e.g., C:\cmake\bin)
- Confirm all dialogs by clicking OK
2.3 MinGW Path Configuration
Follow the same procedure as CMake configuration, adding MinGW's bin directory to the system Path variable.
2.4 System Restart
Important: Restart your computer after configuring environment variables. In some cases, CMake configuration fails until a system restart has been performed.
Verify successful installation by running the following commands in a new command prompt window:
gcc --version
cmake --version
Correct installation displays version information for each tool.
- Creating a Demo Project
3.1 Project Structure
Create an empty directory with the following layout:
project_root/
├── CMakeLists.txt
├── main.c
├── include/
│ ├── Common.h
│ └── MathOps.h
├── src/
│ ├── CMakeLists.txt
│ ├── Common.c
│ └── MathOps.c
└── bin/
3.2 Opening in VSCode
Launch VSCode and open the project root folder using File → Open Folder.
3.3 CMakeLists.txt Configuration
Root-level CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(MathDemo)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
add_subdirectory(src)
include_directories(${CMAKE_SOURCE_DIR}/include)
add_executable(MathDemo main.c)
target_link_libraries(MathDemo src)
The include_directories() directive is essential for allowing source files to locate header files via #include directives.
src/CMakeLists.txt
set(lib_name src)
aux_source_directory(. source_files)
include_directories(${CMAKE_SOURCE_DIR}/include)
add_library(${lib_name} ${source_files})
The aux_source_directory() command automatically collects all .c files in the current directory into the specified variable.
- Building and Execution
4.1 CMake Configuration
Press F1 in VSCode, type "cmake configure", and select CMake: Configure from the dropdown menu. This generates the build system files based on your CMakeLists.txt configuration.
4.2 Compilation
Press F1, type "cmake build", and select CMake: Build. This compiles all source files and produces the executable in the bin directory.
4.3 Running the Application
Open a terminal in VSCode (Terminal → New Terminal), navigate to the bin directory, and execute:
./MathDemo.exe
- Complete Source Code
5.1 Root Directory Files
# Root CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(MathDemo)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
add_subdirectory(src)
include_directories(${CMAKE_SOURCE_DIR}/include)
add_executable(MathDemo main.c)
target_link_libraries(MathDemo src)
/* main.c */
#include <stdio.h>
#include "Common.h"
#include "MathOps.h"
int main()
{
printf("Math Demo Application\n");
int val1 = 50;
int val2 = 8;
printf("Add(%d, %d) = %d\n", val1, val2, Add(val1, val2));
printf("Subtract(%d, %d) = %d\n", val1, val2, Subtract(val1, val2));
printf("Multiply(%d, %d) = %d\n", val1, val2, Multiply(val1, val2));
printf("Divide(%d, %d) = %d\n", val1, val2, Divide(val1, val2));
(void)Swap(&val1, &val2);
printf("After Swap: val1=%d, val2=%d\n", val1, val2);
return 0;
}
5.2 src/ Directory Files
# src/CMakeLists.txt
set(lib_name src)
aux_source_directory(. source_files)
include_directories(${CMAKE_SOURCE_DIR}/include)
add_library(${lib_name} ${source_files})
/* src/Common.c */
#include "Common.h"
int Compare(const void *left, const void *right)
{
return *(int *)left - *(int *)right;
}
int Swap(int *const a, int *const b)
{
int temp = *b;
*b = *a;
*a = temp;
return 0;
}
/* src/MathOps.c */
#include "MathOps.h"
#include <stdio.h>
int Add(const int a, const int b)
{
return a + b;
}
int Subtract(const int a, const int b)
{
return a - b;
}
int Multiply(const int a, const int b)
{
return a * b;
}
int Divide(const int a, const int b)
{
return a / b;
}
5.3 include/ Directory Files
/* include/Common.h */
#ifndef COMMON_H
#define COMMON_H
int Compare(const void *left, const void *right);
int Swap(int *const a, int *const b);
#endif /* COMMON_H */
/* include/MathOps.h */
#ifndef MATHOPS_H
#define MATHOPS_H
int Add(const int a, const int b);
int Subtract(const int a, const int b);
int Multiply(const int a, const int b);
int Divide(const int a, const int b);
#endif /* MATHOPS_H */