Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CMAKE_MSVC_RUNTIME_LIBRARY to consistently handle linking mode for MSVC runtime #687

Merged
merged 2 commits into from
Jun 14, 2024

Conversation

hcho3
Copy link
Contributor

@hcho3 hcho3 commented Jun 14, 2024

On the Windows platform, the MSVC (Microsoft Visual C++) runtime can be linked either statically or dynamically. Currently, DMLC-Core makes this choice by manually editing raw build flags:

dmlc-core/CMakeLists.txt

Lines 200 to 211 in 1ef6649

if(MSVC)
target_compile_definitions(dmlc PUBLIC -DDMLC_USE_CXX11=1)
if(NOT BUILD_SHARED_LIBS AND NOT DMLC_FORCE_SHARED_CRT)
foreach(flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
if(${flag_var} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif(${flag_var} MATCHES "/MD")
endforeach(flag_var)
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")

This approach is brittle and prone to errors.

A more robust method is to use the built-in flag CMAKE_MSVC_RUNTIME_LIBRARY to control the linking mode for the MSVC runtime.

@hcho3 hcho3 merged commit 1334185 into dmlc:main Jun 14, 2024
0 of 7 checks passed
@hcho3 hcho3 deleted the msvc_runtime branch June 14, 2024 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant