环境
- Intel One API 2023.2
- CMake 3.27.7
- Visual Studio 2022 Community (with C++ desktop)
样例
程序代码
1 #include <iostream> 2 3 int main() 4 { 5 std::cout << "Hello, CMake!" << std::endl; 6 std::cin.get(); 7 return 0; 8 }HelloCMake.cpp
MakeList
1 cmake_minimum_required(VERSION 3.23) 2 3 # Heroius: set() function should be called before project() to take effect, otherwise default compilers will be used. 4 5 # specify Intel oneAPI compiler 6 # Heroius: use '/' instead of '\\' in file paths to avoid incorrect mutiple letter escaping. 7 # Heroius: however these compiler flags seem only placeholders and not used at all. 8 set(CMAKE_C_COMPILER "C:/Program Files (x86)/Intel/oneAPI/compiler/latest/windows/bin/dpcpp.exe" CACHE STRING "" FORCE) 9 set(CMAKE_CXX_COMPILER "C:/Program Files (x86)/Intel/oneAPI/compiler/latest/windows/bin/dpcpp.exe" CACHE STRING "" FORCE) 10 11 # Heroius: the toolchain string determines used c++ compiler, but leaves c compiler unset. 12 set(CMAKE_GENERATOR_TOOLSET "Intel(R) oneAPI DPC++ Compiler 2023") 13 14 # Heroius: skip c compiler checking, since c compiler is just a mock. 15 set(CMAKE_C_COMPILER_WORKS TRUE) 16 17 # Set the project name and version 18 # Heroius: move project() instruction here to make flags take effect. 19 project(HelloCMake) 20 21 # look for Intel oneAPI CMake toolchain files 22 # Heroius: strange error occurs if c compiler checking is not skipped. 23 find_package(IntelSYCL REQUIRED) 24 25 # Add the executable 26 add_executable(HelloCMake HelloCMake.cpp) 27 28 # Heroius: there should be a way that can include sycl libs to generation path, so that the app can run itself. 29 # add_sycl_to_target(TARGET HelloCMake SOURCES HelloCMake.cpp) 30 31 set_target_properties(HelloCMake PROPERTIES 32 RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin 33 RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin 34 # ... add other configurations if needed 35 ) 36 37 # Set executable path 38 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)CMakeLists.txt
Configure脚本
1 :: 设置 OPENCV_ROOT 环境变量 2 Set OPENCV_ROOT=C:\OpenCV\Build 3 :: 如果存在 Build,删除 4 if exist Build rd /S /Q Build 5 :: 如果不存在 Build,创建 6 if not exist Build md Build 7 8 Call "C:\Program Files (x86)\Intel\oneAPI\setvars.bat" intel64 vs2022 9 10 :: 当前文件夹为 Source,子文件夹 .\Build 为构建目录 11 cmake -S . -B .\BuildConfigure.bat
编译脚本
1 :: 进入构建目录 2 cd Build 3 :: 构建,项目文件在当前目录 4 cmake --build . 5 pauseBuild_CMake.bat
运行脚本
1 @ECHO OFF 2 3 :: 加载ONEAPI环境,使得引用库在运行环境可见;将加载过程的输出重定向隐藏 4 Call "C:\Program Files (x86)\Intel\oneAPI\setvars.bat" intel64 vs2022 >NUL 2>&1 5 6 cd ./build/bin/ 7 8 hellocmake.exeRun_App.bat
注意事项
- 要使用Intel One API,需要CMake版本不低于3.23
- CMakeList中,在调用project()之前设置好编译器参数
- Intel One API 2023.2 未提供专用于C的编译器,因此须跳过CMake中的C编译器检查
- 实际确定编译器的是CMAKE_GENERATOR_TOOLSET变量,但是各编译器变量的设置也不能跳过
- CMakeList中的路径变量,使用反斜杠作为路径分隔符,以避免多次转义导致的错误
- 使用find_package(IntelSYCL REQUIRED)添加对Intel One API的引用
- 生成的程序需要在One API环境运行,因在生成目录中没有复制必要的库文件(此问题或有其他解决方案,欢迎留言指正)