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”.
Add the end of the list you will find the Clang tools:
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:
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:
Afterwards, you can switch the compiler chain to LLVM/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:
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:
The respective libomp.lib must be added to the additional dependencies of the the linker as shown in the following picture:
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:
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:
Then, I changed the toolset to clang_cl_x64_x64:
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.
- 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:
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.