Configuring CMake* and Microsoft Visual Studio* 2019 for Use with Intel Compilers

Published: 04/01/2021  

Last Updated: 03/28/2022

This article describes how to configure CMake* with an Intel compiler to build Microsoft Visual Studio* projects. Use this guide if you have source code and CMake* configs and want to create Visual Studio* solutions with an Intel compiler . 

Prerequisites

  • Microsoft Visual Studio* 2019 or better
  • at least one Intel® oneAPI Toolkit installed
  • CMake* 3.19

How to generate Microsoft Visual Studio* projects in a command-line with CMake*

1. Configure your environment:

Command line users

  • Open a command window.
  • Set system variables by running  setvars:

    "C:\Program Files (x86)\Intel\oneAPI\setvars.bat"

Windows PowerShell* users

  • Open a PowerShell window.
  • Set system variables by running setvars:

cmd.exe "/K" '"C:\Program Files (x86)\Intel\oneAPI\setvars.bat" && powershell'

For oneAPI Command Prompt Users

  • On your Windows Taskbar, click Start > Intel oneAPI 202x > Intel oneAPI Command Prompt or click Start and search for Intel oneAPI Command Prompt.
  • A command line window will open with the environment automatically set.

2. In the same command line window, navigate to your project folder, create a build folder and go into it.

3. Select your compiler:

  • Intel® C++\DPC++ Compilers are delivered as a toolset, so the common template for the command line is cmake -T "TOOLSET" -DCMAKE_CXX_COMPILER="COMPILER_EXE" ../ where :
COMPILER TOOLSET COMPILER EXE NAME
Intel® Classic Compiler Intel C++ Compiler 19.2 icl
Intel® NextGen Compiler Intel C++ Compiler 2021 icx
Intel® DPC++ Compiler Intel(R) oneAPI DPC++ Compiler dpcpp

 

The example below demonstrates how to build a project with dependencies on Intel® Threading Building Blocks (Intel® TBB) and Intel® RKCommon (see Intel® oneAPI Rendering Toolkit for more information). This example contains the application MyBenchmark and the library BenchA:

$ cmake -T "Intel(R) oneAPI DPC++ Compiler" -DCMAKE_CXX_COMPILER="dpcpp" -D TBB_ROOT="C:/Program Files (x86)/Intel/oneAPI/tbb/latest" -D TBB_INCLUDE_DIR="C:/Program Files (x86)/Intel/oneAPI/tbb/latest/include/oneapi" -DBENCH_A=ON ../
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.18363.
-- The CXX compiler identification is Clang 12.0.0 with MSVC-like command-line
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Intel/oneAPI/compiler/2021.1.1/windows/bin/intel64/icl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES)
-- Could NOT find OpenMP (missing: OpenMP_CXX_FOUND)
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - not found
-- Found Threads: TRUE
CMake Warning (dev) at C:/Program Files (x86)/Intel/oneAPI/rkcommon/latest/lib/cmake/rkcommon-1.5.0/rkcommon_macros.cmake:23 (find_package):
  Policy CMP0074 is not set: find_package uses <PackageName>_ROOT variables.
  Run "cmake --help-policy CMP0074" for policy details.  Use the cmake_policy
  command to set the policy and suppress this warning.

  CMake variable TBB_ROOT is set to:

    C:/Program Files (x86)/Intel/oneAPI/tbb/latest

  For compatibility, CMake is ignoring the variable.
Call Stack (most recent call first):
  C:/Program Files (x86)/Intel/oneAPI/rkcommon/latest/lib/cmake/rkcommon-1.5.0/rkcommon_macros.cmake:188 (find_dependency_39)
  C:/Program Files (x86)/Intel/oneAPI/rkcommon/latest/lib/cmake/rkcommon-1.5.0/rkcommonConfig.cmake:49 (rkcommon_create_tasking_target)
  CMakeLists.txt:46 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Looking for TBB components tbb;tbbmalloc; ()
-- Found TBB version 2021.1 at C:/Program Files (x86)/Intel/oneAPI/tbb/latest
-- BenchA.cpp
-- Configuring done
-- Generating done

 

  • Intel® Fortran Compiler Classic (ifort)  is supported as a compiler so the command line is cmake -DCMAKE_Fortran_COMPILER="ifort" ../
  • Intel® Fortran Compiler NextGen (ifx) is not supported in CMake* 3.19 

4. Find the Microsoft Visual Studio* solution (*.sln) generated in your build folder and open it.

5. Set your application's project as a Startup project.

6. Check the project settings:

  • For the Intel oneAPI DPC++/C++ Compiler:  Navigate Solution Explorer, Project Properties – General – Platform Toolset and make sure that the correct toolset is used in the 'Platform toolset' menu

  • For the Fortran Compiler: Solution Explorer, Project Properties – General – Use Compiler and make sure that the correct toolset is used in the 'Use Compiler' menu

 

7. Build and run the solution to check the configuration.

 

How to generate Microsoft Visual Studio* projects from CMake* using Visual Studio*

1. Create a 'build' folder inside the root of your project.

2. Open the project root in Visual Studio* (Open local folder).

2. Generator will detect your CMakeSettings.json. Wait for the generator to finish the configuration.

  • If generation has not started, select Project -> Generate Cache for your project
  • If you changed a CMakeSettings.json, you may need to first clean up your cache: Project -> Cmake Cache for … -> delete cache

The example output indicating that generation is successfully completed:

1> CMake generation started for configuration: 'x64-Debug (default)'.
1> Command line: "cmd.exe" /c ""C:\Program Files\CMake\bin\cmake.exe"  -G "Visual Studio 16 2019" -A x64  -DCMAKE_CONFIGURATION_TYPES:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\Downloads\cmake\dpcpp_project\out" -DCMAKE_CXX_COMPILER:FILEPATH="C:/Program Files (x86)/Intel/oneAPI/compiler/latest/windows/bin/dpcpp.exe" -DCMAKE_C_COMPILER:FILEPATH="C:/Program Files (x86)/Intel/oneAPI/compiler/latest/windows/bin/dpcpp.exe" -Drkcommon_DIR:FILEPATH="C:/Program Files (x86)/Intel/oneAPI/rkcommon/latest/lib/cmake/rkcommon-1.5.0" -DBENCH_A:BOOL="True" -T "Intel(R) oneAPI DPC++ Compiler"  -DTBB_ROOT="C:/Program Files (x86)/Intel/oneAPI/tbb/latest" -DTBB_INCLUDE_DIR="C:/Program Files (x86)/Intel/oneAPI/tbb/latest/include/oneapi" "C:\Users\Downloads\cmake\dpcpp_project" 2>&1"
1> Working directory: C:\Users\Downloads\cmake\dpcpp_project\out
1> [CMake] -- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.18363.
1> [CMake] -- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES) 
1> [CMake] -- Could NOT find OpenMP (missing: OpenMP_CXX_FOUND) 
1> [CMake] CMake Warning (dev) at C:/Program Files (x86)/Intel/oneAPI/rkcommon/latest/lib/cmake/rkcommon-1.5.0/rkcommon_macros.cmake:23 (find_package):
1> [CMake]   Policy CMP0074 is not set: find_package uses <PackageName>_ROOT variables.
1> [CMake]   Run "cmake --help-policy CMP0074" for policy details.  Use the cmake_policy
1> [CMake]   command to set the policy and suppress this warning.
1> [CMake] 
1> [CMake]   CMake variable TBB_ROOT is set to:
1> [CMake] 
1> [CMake]     C:/Program Files (x86)/Intel/oneAPI/tbb/latest
1> [CMake] 
1> [CMake]   For compatibility, CMake is ignoring the variable.
1> [CMake] Call Stack (most recent call first):
1> [CMake]   C:/Program Files (x86)/Intel/oneAPI/rkcommon/latest/lib/cmake/rkcommon-1.5.0/rkcommon_macros.cmake:188 (find_dependency_39)
1> [CMake]   C:/Program Files (x86)/Intel/oneAPI/rkcommon/latest/lib/cmake/rkcommon-1.5.0/rkcommonConfig.cmake:49 (rkcommon_create_tasking_target)
1> [CMake]   CMakeLists.txt:46 (find_package)
1> [CMake] This warning is for project developers.  Use -Wno-dev to suppress it.
1> [CMake] -- Looking for TBB components tbb;tbbmalloc; ()
1> [CMake] -- Found TBB version 2021.1 at C:/Program Files (x86)/Intel/oneAPI/tbb/latest
1> [CMake] -- BenchA.cpp
1> [CMake] -- Configuring done
1> [CMake] -- Generating done
1> [CMake] -- Build files have been written to: C:/Users/Downloads/cmake/dpcpp_project/out
1> Extracted CMake variables.
1> Extracted source files and headers.
1> Extracted code model.
1> Extracted includes paths.
1> CMake generation finished.

3. Click Build All.

5. Navigate your build folder and open a generated solution.

6. Set your application's project as a Startup project.

7. Navigate Solution Explorer, Project Properties – General – Platform Toolset and make sure that the correct toolset is used in the 'Platform toolset' menu.

8. Check the project compiler:

  • For the Intel DPC++/C++ Compiler:  Navigate Solution Explorer, Project Properties – General – Platform Toolset and make sure that the correct toolset is used in the 'Platform toolset' menu.

  • For Fortran Compiler: Solution Explorer, Project Properties – General – Use Compiler and make sure that the correct toolset is used in the 'Use Compiler' menu

 

9. Build and run the solution to check the configuration.

 

 

Product and Performance Information

1

Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.