Alternatives to MSVC71 in CMake for Modern Development


MSVC71 in CMake

  • Functionality
    CMake would set this variable to TRUE if it detected a Visual Studio installation with MSVC 7.1. This information could then be used in your CMakeLists.txt file to conditionally compile code or configure build settings specific to that compiler version.
  • Purpose
    In older versions of CMake (prior to around 3.8), MSVC71 was a boolean variable that indicated whether the compiler being used was Microsoft Visual C++ version 7.1.

Discouraged Usage

  • Limited Usefulness
    Even if you were working with a legacy project that used MSVC 7.1, the specific value of MSVC71 wouldn't provide much practical benefit in most cases. Modern CMake offers more robust ways to handle compiler version detection and configuration.
  • Outdated Compiler
    MSVC 7.1, released in 2002, is very outdated and not recommended for modern development. There have been significant advancements in compiler features, security, and performance since then.

Recommended Approach

  • CMake Features
    CMake offers several features for compiler detection and configuration, including:
    • find_package() modules for specific compilers or libraries
    • Conditional statements (e.g., if(), else(), endif()) based on detected variables
    • Preprocessor definitions (add_definitions()) to tailor code behavior for different compiler versions
  • MSVC_VERSION
    For cross-platform compatibility and future-proofness, it's strongly recommended to use the MSVC_VERSION variable instead. This variable provides a more detailed version number (e.g., 14.29.30133) that allows for more granular checks and configuration based on specific Visual Studio versions.
# Detect Visual Studio version
find_package(MSVC REQUIRED)

# Conditionally compile code based on version
if(MSVC_VERSION LESS 1900)  # Assuming specific version requirement
  message(WARNING "Compiling with an older MSVC version. Consider upgrading.")
  # Add or modify compiler flags here
else()
  # Use newer compiler features or settings
endif()


Simple Version Check and Message

cmake_minimum_required(VERSION 3.8)  # Requires CMake 3.8 or later for MSVC_VERSION

project(MyProject CXX)

find_package(MSVC REQUIRED)

message(STATUS "Detected MSVC version: ${MSVC_VERSION}")

if(MSVC_VERSION LESS 1910)  # Check for Visual Studio 2017 or later
  message(WARNING "Compiling with an older MSVC version. Consider upgrading for better performance and security.")
endif()

Conditional Compilation based on Version

cmake_minimum_required(VERSION 3.8)

project(MyProject CXX)

find_package(MSVC REQUIRED)

# Define for newer MSVC versions
if(MSVC_VERSION GREATER_EQUAL 1920)  # Check for Visual Studio 2019 or later
  add_definitions(-experimental:external-property-initializer)
endif()

# Source files
add_executable(my_executable main.cpp)

In this example, -experimental:external-property-initializer is a compiler flag only available in Visual Studio 2019 or later. The code conditionally adds this definition only if the detected MSVC_VERSION meets the criteria.

Customizing Warning Levels based on Version

cmake_minimum_required(VERSION 3.8)

project(MyProject CXX)

find_package(MSVC REQUIRED)

# Set warning level based on version
if(MSVC_VERSION LESS 1900)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")  # More warnings for older versions
else()
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")  # Even stricter warnings for newer versions
endif()

# Source files
add_executable(my_executable main.cpp)

This example adjusts the warning level based on the detected MSVC_VERSION. You can customize the warning levels (/W3, /W4) or add additional flags as needed.



    • This is the preferred method as it provides a more detailed version number for granular control based on specific Visual Studio versions.
    • Example:
    cmake_minimum_required(VERSION 3.8)  # Requires CMake 3.8 or later
    
    project(MyProject CXX)
    
    find_package(MSVC REQUIRED)
    
    message(STATUS "Detected MSVC version: ${MSVC_VERSION}")
    
    # Conditional logic based on MSVC_VERSION
    if(MSVC_VERSION LESS 1910)  # Example check for VS 2017 or later
      # Handle older versions
    else()
      # Handle newer versions
    endif()
    
  1. Compiler Detection with CMAKE_CXX_COMPILER

    • While not as detailed as MSVC_VERSION, this variable can identify the compiler being used (often MSVC on Windows).
    • Limited Usefulness
      This approach might not differentiate between specific MSVC versions.
    • Example:
    cmake_minimum_require(VERSION 3.0)
    
    project(MyProject CXX)
    
    if(CMAKE_CXX_COMPILER MATCHES "MSVC")
      message(STATUS "MSVC compiler detected")
      # Add compiler-specific flags here (be cautious of portability)
    endif()
    
  2. Using find_package() with Specific MSVC Versions

    • CMake provides find_package() modules for various compilers, including Visual Studio versions.
    • This can offer more tailored configuration options for specific MSVC versions.
    • Availability Note
      Not all MSVC versions might have corresponding modules.
    • Example (assuming a module exists for your desired MSVC version):
    cmake_minimum_required(VERSION 3.10)  # May require newer CMake for specific modules
    
    project(MyProject CXX)
    
    find_package(MSVC14 REQUIRED)  # Adjust the version number as needed
    
    # Use variables or functions provided by the specific find_package module
    message(STATUS "Using MSVC ${MSVC14_VERSION}")