- 1. License
- 2. Overview
- 3. Environment Setup Philosophy
- 4. Toolchain Installation
- 5. Windows Terminal Configuration
- 6. Compiler Detection & Build Systems
- 7. Usage Examples
- 8. Lessons Learned & Troubleshooting
- 9. Advanced Configuration
- 10. Testing and Validation
- 11. Contributing
- 12. References and Further Reading
- 13. Conclusion
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
You are free to:
-
Share — copy and redistribute the material in any medium or format
-
Adapt — remix, transform, and build upon the material for any purpose, even commercially
Under the following terms:
-
Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made
-
ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original
This repository serves as a comprehensive guide and test suite for setting up a robust C and Fortran development environment on Windows. It covers the installation and configuration of three distinct toolchains: MinGW-w64 (via MSYS2), Microsoft Visual C++ (MSVC), and Intel oneAPI.
The subdirectories test_c
and test_fortran
contain sample projects with Makefiles
, Makefile.nmake
, and CMakeLists.txt
files to verify that each toolchain is working correctly. This main README
documents the lessons learned during the setup process.
├── test_c/ # C compiler test project
│ ├── main.c # C source with compiler detection
│ ├── Makefile # GNU Make (MSYS2/Linux)
│ ├── Makefile.nmake # NMAKE (Windows)
│ ├── CMakeLists.txt # CMake cross-platform
│ └── README.md # C project documentation
├── test_fortran/ # Fortran compiler test project
│ ├── main.f90 # Fortran source with compiler detection
│ ├── Makefile # GNU Make (MSYS2/Linux)
│ ├── Makefile.nmake # NMAKE (Windows)
│ ├── CMakeLists.txt # CMake cross-platform
│ └── README.md # Fortran project documentation
└── README.adoc # This file
The goal is to have three independent, powerful compiler toolchains that can be accessed on demand from a modern terminal like Windows Terminal. This setup avoids global PATH
conflicts by using dedicated terminal profiles to initialize each environment separately.
- MSYS2/MinGW
-
Provides the GNU toolchain (
gcc
,gfortran
,make
) in a Unix-like shell environment with package management viapacman
. - MSVC Build Tools
-
Provides the Microsoft C/C++ compiler (
cl.exe
), linker (link.exe
), and Windows SDK for native Windows development. - Intel oneAPI
-
Provides high-performance C++ (
icx
) and Fortran (ifx
) compilers that integrate with the MSVC toolchain for linking and utilize LLVM backends.
ℹ️
|
This approach ensures that each toolchain operates in isolation, preventing version conflicts and allowing developers to switch between environments seamlessly. |
All tools should be installed using a package manager from an elevated PowerShell terminal for simplicity and reproducibility. Winget proved to be the most effective for installing the MSVC and Intel toolchains, while MSYS2’s pacman
manages the GNU toolchain.
The MSYS2 environment provides a complete Unix-like development environment on Windows with the GNU compiler collection.
-
Install MSYS2:
winget install --id=MSYS2.MSYS2
-
Install Compilers: After installing MSYS2, open the MSYS2 UCRT64 terminal from the Start Menu and install the complete toolchain:
# Update package database pacman -Syu # Install C/C++ toolchain and basic development tools pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain # Install Fortran compiler pacman -S mingw-w64-ucrt-x86_64-gcc-fortran # Install additional development tools pacman -S mingw-w64-ucrt-x86_64-cmake pacman -S mingw-w64-ucrt-x86_64-ninja
-
GCC 15.2.0: GNU C Compiler
-
*G*: GNU C Compiler
-
gfortran: GNU Fortran Compiler
-
make: GNU Make build system
-
cmake: Cross-platform build system generator
-
ninja: Fast build system
-
pacman: Package manager for additional tools
The Intel compilers depend on the MSVC linker and the Windows SDK. The VS Build Tools provide these components without requiring the full Visual Studio IDE.
-
Install VS 2022 Build Tools: The crucial step is to include the "Desktop development with C++" workload using the
--add
argument:winget install --id Microsoft.VisualStudio.2022.BuildTools --override "--add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"
-
MSVC 19.29: Microsoft C/C++ Optimizing Compiler
-
link.exe: Microsoft Incremental Linker
-
nmake.exe: Microsoft Program Maintenance Utility
-
Windows SDK: Headers and libraries for Windows development
-
MSBuild: Microsoft Build Engine
❗
|
The |
This single toolkit contains both the C/C++ (icx
) and Fortran (ifx
) compilers based on LLVM technology.
-
Install the HPC Toolkit: This is a very large installation (several GB) and will take significant time:
winget install --id Intel.OneAPI.HPCToolkit
-
icx: Intel C++ Compiler based on LLVM
-
ifx: Intel Fortran Compiler
-
Intel MKL: Math Kernel Library for optimized mathematical operations
-
Intel MPI: Message Passing Interface for parallel computing
-
Intel VTune Profiler: Performance analysis tools
ℹ️
|
Intel compilers are designed for high-performance computing and provide superior optimization for Intel processors while maintaining compatibility with industry standards. |
The key to managing these environments without conflict is to create dedicated profiles in Windows Terminal that automatically run the correct initialization scripts.
To configure:
-
Open Windows Terminal
-
Press
Ctrl+,
to open settings -
Click "Open JSON file" in the bottom left
-
Add the following profiles to the
"profiles"
→"list"
array
This profile launches the bash shell for the UCRT64 (MinGW) environment with full Unix compatibility.
{
"guid": "{a718a3d5-9e77-4d0d-b7b6-69ec3d190206}",
"name": "MSYS2 UCRT64",
"commandline": "C:/msys64/msys2_shell.cmd -defterm -here -no-start -ucrt64",
"startingDirectory": "%USERPROFILE%",
"icon": "C:/msys64/ucrt64.ico",
"colorScheme": "One Half Dark"
}
This profile starts a Command Prompt and runs the vcvarsall.bat
script to configure the MSVC environment.
{
"guid": "{07446546-a4f5-4943-83ad-27f950a719f7}",
"name": "MSVC x64",
"commandline": "cmd.exe /k \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Auxiliary\\Build\\vcvarsall.bat\" x64",
"startingDirectory": "%USERPROFILE%",
"colorScheme": "Campbell Powershell"
}
This profile starts a Command Prompt and runs the Intel setvars.bat
script, which configures both the Intel tools and the underlying MSVC environment they depend on.
{
"guid": "{2b0a995e-399a-48d8-9159-d8df2e8e6e06}",
"name": "Intel oneAPI Compilers",
"commandline": "cmd.exe /k \"C:\\Program Files (x86)\\Intel\\oneAPI\\setvars.bat\"",
"startingDirectory": "%USERPROFILE%",
"colorScheme": "Campbell"
}
💡
|
You can set any of these profiles as your default by copying its GUID to the |
Both test projects include sophisticated compiler detection logic that identifies which compiler was used at runtime.
The C project uses preprocessor macros to detect compilers in order of specificity:
#if defined(__INTEL_LLVM_COMPILER)
printf("Hello from C, compiled with the Intel oneAPI C Compiler (icx)!\n");
#elif defined(__INTEL_COMPILER)
printf("Hello from C, compiled with the Intel C Compiler (icc)!\n");
#elif defined(_MSC_VER)
printf("Hello from C, compiled with the Microsoft C Compiler (cl)!\n");
#elif defined(__GNUC__)
printf("Hello from C, compiled with the GNU C Compiler (gcc)!\n");
#elif defined(__clang__)
printf("Hello from C, compiled with the Clang C Compiler!\n");
#else
printf("Hello from C, compiled with an unknown compiler!\n");
#endif
❗
|
The order of detection is crucial because Intel oneAPI compiler defines multiple macros ( |
The Fortran project uses preprocessor directives with explicit preprocessing enabled:
#if defined(__INTEL_LLVM_COMPILER)
print *, "Hello from Fortran, compiled with the Intel Fortran Compiler (ifx)!"
#elif defined(__INTEL_COMPILER)
print *, "Hello from Fortran, compiled with the Intel Fortran Compiler (ifort)!"
#elif defined(__GFORTRAN__)
print *, "Hello from Fortran, compiled with the GNU Fortran Compiler (gfortran)!"
#elif defined(__FLANG__)
print *, "Hello from Fortran, compiled with the LLVM Flang Compiler!"
#else
print *, "Hello from Fortran, compiled with an unknown compiler!"
#endif
ℹ️
|
Fortran requires explicit preprocessor activation using |
Due to fundamental syntax differences, separate Makefiles are required:
# Unix-style paths with forward slashes
CC = gcc
TARGET = test_c.exe
BUILD_DIR = build
all: $(BUILD_DIR)/$(TARGET)
$(BUILD_DIR)/$(TARGET): main.c
@mkdir -p $(BUILD_DIR)
$(CC) main.c -o $(BUILD_DIR)/$(TARGET)
clean:
@rm -rf $(BUILD_DIR)
# Windows-style commands and paths
BUILD_DIR = build
TARGET = test_c.exe
msvc:
@if not exist $(BUILD_DIR) mkdir $(BUILD_DIR)
cl main.c /Fe:$(BUILD_DIR)\$(TARGET)
intel:
@if not exist $(BUILD_DIR) mkdir $(BUILD_DIR)
icx main.c /Fe:$(BUILD_DIR)\$(TARGET)
clean:
@if exist $(BUILD_DIR) rmdir /s /q $(BUILD_DIR)
CMake provides true cross-platform, cross-compiler builds with intelligent environment detection:
cmake_minimum_required(VERSION 3.15)
# Compiler selection before project() call
if(COMPILER_CHOICE STREQUAL "intel")
find_program(ICX_COMPILER icx)
if(ICX_COMPILER)
set(CMAKE_C_COMPILER "${ICX_COMPILER}")
endif()
endif()
project(TestC C)
add_executable(test_c main.c)
-
Automatic compiler detection
-
Generator selection (Ninja, NMake, Visual Studio)
-
Cross-platform compatibility
-
Advanced dependency management
-
Integration with IDEs
💡
|
For Intel compilers with CMake, always use Ninja or NMake generators:
Visual Studio generators don’t support Intel compiler overrides. |
-
Clone or download this repository
-
Open Windows Terminal
-
Select the appropriate profile for your desired toolchain
-
Navigate to
test_c
ortest_fortran
directory -
Follow the build instructions in each project’s README
Environment | Build Command | Expected Output |
---|---|---|
MSYS2 UCRT64 |
|
"Hello from C, compiled with the GNU C Compiler (gcc)!" |
Windows CMD (MSVC) |
|
"Hello from C, compiled with the Microsoft C Compiler (cl)!" |
Windows CMD (Intel) |
|
"Hello from C, compiled with the Intel oneAPI C Compiler (icx)!" |
CMake (Any) |
|
Depends on detected/selected compiler |
Environment | Build Command | Expected Output |
---|---|---|
MSYS2 UCRT64 |
|
"Hello from Fortran, compiled with the GNU Fortran Compiler (gfortran)!" |
Windows CMD (Intel) |
|
"Hello from Fortran, compiled with the Intel Fortran Compiler (ifx)!" |
CMake (Intel) |
|
"Hello from Fortran, compiled with the Intel Fortran Compiler (ifx)!" |
Problem: Multiple versions of tools like cmake.exe
in the system PATH
causing unexpected behavior.
Diagnosis:
where cmake
where cl
where gcc
Solution: Edit System Environment Variables via Windows Control Panel to remove conflicting directories from Path
. Never use setx
for this as it can truncate and corrupt your PATH
.
|
Do not attempt to modify the system PATH using |
Problem: Intel setvars.bat
fails with "WARNING: Visual Studio was not found".
Diagnosis: Intel compilers require MSVC toolchain for linking and Windows SDK for headers.
Solution: Ensure VS Build Tools are installed with the C++ workload as shown in the installation section.
Problem: Intel compilers not working with Visual Studio generator in CMake.
Explanation: Visual Studio generators are tied to the MSVC toolchain and ignore CMAKE_C_COMPILER
settings.
Solution: Use Ninja or NMake Makefiles generators for Intel compilers:
cmake .. -G Ninja -DCOMPILER_CHOICE=intel
cmake .. -G "NMake Makefiles" -DCOMPILER_CHOICE=intel
Problem: Fortran compiler detection not working due to preprocessor directives being ignored.
Root Cause: Fortran compilers don’t enable preprocessing by default, unlike C compilers.
Solution: Explicitly enable preprocessing:
* Intel: Add /fpp
flag
* GNU: Add -cpp
flag
# GNU Fortran
gfortran -cpp main.f90 -o test.exe
# Intel Fortran
ifx main.f90 /fpp /Fe:test.exe
Problem: CMake not switching compilers when reusing build directory.
Root Cause: CMake caches compiler selection in build directory.
Solution: Always clean build directory when switching compilers:
# Windows
rmdir /s /q build
# MSYS2/Linux
rm -rf build
Based on testing with the sample projects:
-
Intel icx/ifx: Fastest compilation, best optimization
-
MSVC cl: Good compilation speed, excellent Windows integration
-
GCC: Slower compilation but excellent standards compliance
Intel compilers typically produce the fastest executables on Intel hardware, especially for mathematical computations. MSVC produces well-optimized code for general Windows applications. GCC provides good performance with excellent portability.
If tools are installed in non-standard locations, update the terminal profile paths accordingly:
{
"name": "Custom Intel oneAPI",
"commandline": "cmd.exe /k \"D:\\Intel\\oneAPI\\setvars.bat\"",
"startingDirectory": "%USERPROFILE%"
}
Key environment variables set by each toolchain:
-
PATH
: Includes/ucrt64/bin
for GNU tools -
CC
: Set togcc
-
FC
: Set togfortran
-
VSINSTALLDIR
: Visual Studio installation directory -
VCINSTALLDIR
: VC++ tools directory -
WindowsSdkDir
: Windows SDK path -
LIB
,INCLUDE
: Library and header search paths
-
INTEL_ONEAPI_ROOT
: Base installation directory -
SETVARS_COMPLETED
: Indicates successful initialization -
Inherits all MSVC variables plus Intel-specific paths
Recommended extensions for each toolchain:
-
C/C++ (Microsoft): IntelliSense and debugging
-
CMake Tools: CMake integration
-
Fortran (Modern Fortran): Fortran language support
Configure compiler paths in .vscode/c_cpp_properties.json
:
{
"configurations": [
{
"name": "MSVC",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe"
},
{
"name": "Intel",
"compilerPath": "C:/Program Files (x86)/Intel/oneAPI/compiler/latest/bin/icx.exe"
},
{
"name": "GCC",
"compilerPath": "C:/msys64/ucrt64/bin/gcc.exe"
}
]
}
CLion automatically detects toolchains when they’re properly configured in Windows Terminal. Use the "Toolchains" settings to configure each environment.
The included test projects validate that your environment is correctly configured. Each project tests:
-
Compiler detection and identification
-
Preprocessor functionality
-
Build system integration (Make, NMAKE, CMake)
-
Cross-platform compatibility
-
Proper flag handling (
/Fe:
vs-o
,/fpp
vs-cpp
)
Run the tests after installation to ensure everything works correctly.
This project welcomes contributions! Please:
-
Test any changes across all three toolchains
-
Update documentation for new features
-
Follow the existing code style and structure
-
Add test cases for new functionality
This multi-compiler environment provides a robust foundation for C and Fortran development on Windows, supporting everything from simple command-line programs to complex high-performance computing applications. The combination of GNU tools for open-source compatibility, MSVC for Windows integration, and Intel compilers for performance creates a comprehensive development platform.
The key to success is proper environment isolation through Windows Terminal profiles and understanding the unique characteristics and requirements of each toolchain.