Microsoft Visual Studio 2019 with and without CMake and the LLVM/Clang-Compiler

Installation of Clang

The OpenMP support of the Microsoft Visual C++ compiler is still limited to the OpenMP specification 2.0 in the year 2019 also the compiler recently added support for several C++20 features!

Hence, I wanted to try the workaround to employ the Clang-Toolchain which can be installed by the Visual Studio installer. They can be found in the category “Desktop Development with C++” under “Desktop and mobile devices”.

Visual Studio Installer and available packages

Add the end of the list you will find the Clang tools:

Clang packages for Microsoft Visual Studio 2019

Let the installer install those packages or modify your existing installation with the installer with these packages.

Using Clang within a Microsoft Visual Studio project

The main issue now was to really integrate and use Clang, including its OpenMP implementation, in an existing project that was configured for Visual Studio 2019 (v142) as shown in the following:

The Visual Studio Project with its initial configuration

What is very important and which costs me a lot of time to realize was that you have to deactivate the OpenMP support for the Visual Studio Compiler chain before switching. Otherwise, you will receive the error that Clang does not know the /openmp compiler option and terminates the compile operation. Hence, you have to switch off OpenMP support as shown in the following:

Switching off OpenMP support for the Visual C++ compiler

Afterwards, you can switch the compiler chain to LLVM/Clang:

Switching compiler chain to Clang

However, more steps are required in order to get Clang and OpenMP working. The first is to set the appropriate Clang compiler flags to employ OpenMP:

Setting Clang compiler flags for OpenMP usage

Finally, the linker-settings must also be adapted in order to allow you to use OpenMP with Clang. To that end, you must add the path to the Clang libraries to the additional library directories. The compiler is installed in the Microsoft Visual Studio folder. The following screenshot is based on an installation of Visual Studio in the default location that the installer proposes:

Adding the library direction

The respective libomp.lib must be added to the additional dependencies of the the linker as shown in the following picture:

Linking the libomp.lib

After that, my program compiled and I could employ more advanced OpenMP features.

However, to really execute the program without crash, I also had to modify the runtime environment in the Visual Studio in Configuration Properties/Debugging/Environment as shown in the following screenshot:

Resolving path to the DLL file.

Clang, CMake and Microsoft Visual Studio

Another time-consuming task was to get a CMake project within the Visual Studio running while employing its special Clang integration. The CMake project already contained two configurations, x64-Release and x64-Debug, using the Visual C++ toolchain. In a first step, I entered the configuration manager and cloned the existing release configuration:

Cloning the original configuration

Then, I changed the toolset to clang_cl_x64_x64:

Adapting the toolset for Clang

I extended the view to all advanced options and modified several CMake variables. The toolchain was automatically set correctly but several other options are missing.

The CMake option.
  • CMAKE_CXX_STANDARD_LIBRARIES: Add libomp.lib
  • CMAKE_CXX_FLAGS: Add -Xclang -fopenmp

I also changed the following settings but think that this does not has any effect:

Additional OpenMP options that can be modified

Furthermore, I had to add the line link_directories(PATH_TO_DLL ) to the CMakeLists.txt and to copy the DLL into the directory of my program executable.