Skip to content

A sample repository and guide for setting up a multi-compiler (MSVC, MinGW, Intel) C/Fortran environment on Windows.

License

Notifications You must be signed in to change notification settings

Foadsf/win-multi-compiler-setup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multi-Compiler C & Fortran Environment for Windows

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Creative Commons 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.

Project Structure
├── 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 via pacman.

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.

  1. Install MSYS2:

    winget install --id=MSYS2.MSYS2
  2. 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.

  1. 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 --includeRecommended flag is essential as it installs the Windows SDK and CMake integration, which are required by the Intel compilers.

This single toolkit contains both the C/C++ (icx) and Fortran (ifx) compilers based on LLVM technology.

  1. 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:

  1. Open Windows Terminal

  2. Press Ctrl+, to open settings

  3. Click "Open JSON file" in the bottom left

  4. 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 "defaultProfile" setting in the Windows Terminal configuration.

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 (INTEL_LLVM_COMPILER, MSC_VER, clang_) for compatibility. Most specific macros must be checked first.

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 /fpp (Intel) or -cpp (GNU) flags, unlike C where preprocessing is enabled by default.

Due to fundamental syntax differences, separate Makefiles are required:

GNU Make (Makefile)
# 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)
NMAKE (Makefile.nmake)
# 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)
CMake Advantages:
  • 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: cmake .. -G Ninja -DCOMPILER_CHOICE=intel

Visual Studio generators don’t support Intel compiler overrides.

  1. Clone or download this repository

  2. Open Windows Terminal

  3. Select the appropriate profile for your desired toolchain

  4. Navigate to test_c or test_fortran directory

  5. Follow the build instructions in each project’s README

Table 1. C Project Build Commands
Environment Build Command Expected Output

MSYS2 UCRT64

make

"Hello from C, compiled with the GNU C Compiler (gcc)!"

Windows CMD (MSVC)

nmake /f Makefile.nmake msvc

"Hello from C, compiled with the Microsoft C Compiler (cl)!"

Windows CMD (Intel)

nmake /f Makefile.nmake intel

"Hello from C, compiled with the Intel oneAPI C Compiler (icx)!"

CMake (Any)

cmake .. && cmake --build .

Depends on detected/selected compiler

Table 2. Fortran Project Build Commands
Environment Build Command Expected Output

MSYS2 UCRT64

make

"Hello from Fortran, compiled with the GNU Fortran Compiler (gfortran)!"

Windows CMD (Intel)

nmake /f Makefile.nmake intel

"Hello from Fortran, compiled with the Intel Fortran Compiler (ifx)!"

CMake (Intel)

cmake .. -G Ninja -DCOMPILER_CHOICE=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 setx command. It has a character limit and can corrupt long PATH variables. Always use the GUI System Properties → Environment Variables interface.

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:

  1. Intel icx/ifx: Fastest compilation, best optimization

  2. MSVC cl: Good compilation speed, excellent Windows integration

  3. 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:

MSYS2 UCRT64
  • PATH: Includes /ucrt64/bin for GNU tools

  • CC: Set to gcc

  • FC: Set to gfortran

MSVC
  • VSINSTALLDIR: Visual Studio installation directory

  • VCINSTALLDIR: VC++ tools directory

  • WindowsSdkDir: Windows SDK path

  • LIB, INCLUDE: Library and header search paths

Intel oneAPI
  • 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:

  1. Test any changes across all three toolchains

  2. Update documentation for new features

  3. Follow the existing code style and structure

  4. 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.

About

A sample repository and guide for setting up a multi-compiler (MSVC, MinGW, Intel) C/Fortran environment on Windows.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published