How to Switch to Static Runtime and Enable C++ Exceptions?


I’m trying to build LLVM 10.0.0 with CMake on the command line (Developer Command Prompt for VS2019) and I need to know how to switch to a static runtime. I’ve tried setting LLVM’s variable for this, but it didn’t work. I still have a dynamic runtime. Along with that, I also need to know how to enable C++ exceptions.

One thing I tried was to do “-DCMAKE_CXX_FLAGS=”/std:c++17 /MT /EHsc /permissive-"" but this also didn’t work. Specifying these on the CXXFLAGS environment variable didn’t do anything either. I was using the same session of the Developer Command Prompt, but I made sure to run “refreshenv” to take the new environment from the registry before continuing.

I did try to do “-DLLVM_ENABLE_EH=On” but I still got a warning from CMake (from benchmarks/CMakeLists.txt, if I recall correctly) saying that using std::regex with exceptions disabled isn’t fully supported.

Any help is appreciated. Thanks in advance (I also sent an email on the CMake mailing list, by the way).

I ran this command “cmake -S…/llvm -G"Visual Studio 16 2019” -Thost=x64 -DCMAKE_INSTALL_PREFIX=…/install -DCMAKE_BUILD_TYPE=Release -DCMAKE_MSVC_RUNTIME_LIBRARY=MT -DCMAKE_CXX_FLAGS="/permissive- /EHsc /std:c++17 /O2 /MT /D_SILENCE_ALL_CXX_DEPRECATION_WARNINGS" -DLLVM_ENABLE_RTTI=On -DLLVM_ENABLE_EH=ON -DCMAKE_CXX_STANDARD=17 -DPYTHON_HOME=C:/Users/Osman/AppData/Local/Programs/Python/Python38 -DLLVM_ENABLE_PROJECTS=“clang;clang-tools-extra;lld;lldb;libcxx;libcxxabi”". But I still get “Using Release VC++ CRT: MD”, and also the CMake warning: “CMake Warning at utils/benchmark/CMakeLists.txt:244 (message):
Using std::regex with exceptions disabled is not fully supported”.

Please help.

Hi Osman,

You can’t enable the static CRT through the command-line. You would need to apply this patch:
(although it doesn’t work with lldb)


Okay, thanks. So how do I apply the patch?

I would really like to know about how to apply the patch for making MSVC-built LLVM executables lighter. But aside from that, I also want to know what LLVM does to CMake’s default setting of /EHsc. I want to keep that setting on, but I can’t figure out how. Any help is appreciated. Thanks in advance. P.S. The CMAKE_CXX_FLAGS variable isn’t helping at all, otherwise I wouldn’t be asking this.

Try setting LLVM_ENABLE_EH to ON on the CMake command line.

It sounds like you probably need to dig into the CMake logic a little bit and find out where the _HAS_EXCEPTIONS=0 define is coming from. Not many people (maybe nobody) uses this configuration, so it’s not surprising that it may have some issues.

Are you sure? I would expect cmake -DLLVM_USE_CRT_RELEASE=MT to use the static CRT.

I think Zach is correct, Osman can use -DLLVM_ENABLE_EH=ON, and it will enable exceptions.

You’re still getting a warning from the benchmark library because LLVM’s cmake forcibly disables benchmark library exceptions here:
That’s probably a bug, it should probably be dependent on LLVM_ENABLE_EH.

Yes you’re right, -DLLVM_USE_CRT_RELEASE=MT works indeed! Although the “Visual Studio” cmake generators are multi-config, so you end up with -DCMAKE_BUILD_TYPE=Release being ignored, which means that if you want to make things right, you would need to also set the other –DLLVM_USE_CRT_XXX values as well, to avoid surprises when switching targets inside VS. For those multi-config generators, reintroducing -DLLVM_USE_CRT would be nice, maybe.

Envoyé : October 28, 2019 4:13 PM

I’ll try to do it all from within the IDE. I noticed that the /EHsc flag is already there (makes me wonder why CMake still gave that warning). But I also noticed something else. What’s the reason for this in the Post-Build Event command line for INSTALL.vcxproj?


“C:\Program Files\CMake\bin\cmake.exe” -DBUILD_TYPE=$(Configuration) -P cmake_install.cmake​
if %errorlevel% neq 0 goto :cmEnd​
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone​
exit /b %1​
if %errorlevel% neq 0 goto :VCEnd

Also, libclang.vcxproj has a static runtime set but it was also set to build a DLL instead of a static .lib library. I changed it to have it build a static library. I wonder if that’s a good idea.

The warning is probably because of /D_HAS_EXCEPTIONS=0