- 首先还是安装好对应的插件,本文,使用
cmake
来管理整个cpp
项目。
- 关于
cmake
管理工程, 使用CMakeLists.txt
与 CMakePresets.json
来搭配使用。 cmake version > 3.27
。本文使用CMake Tools
与 搭配 CMakePresets.json
来使用。通过配置来完成传cmake变量,以及控制多线程编译,以及执行target等配置。
-
CMakePresets.json
内容如下。
{
"version": 3,
"configurePresets": [
{
"name": "base",
"hidden": true,
// "generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}"
},
{
"name": "x64-debug",
"displayName": "x64 Debug",
"inherits": "base",
"hidden": true,
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "x64-debug",
"hidden": true,
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "cuda-debug",
"displayName": "Cuda x64 Debug",
"inherits": "x64-debug",
"cacheVariables": {
"BACKEND": "ort-cuda"
}
},
{
"name": "cuda-release",
"displayName": "Cuda x64 Release",
"inherits": "x64-release",
"cacheVariables": {
"BACKEND": "ort-cuda"
}
},
}
]
"buildPresets": [
{
"name": "cuda-release",
"description": "",
"displayName": "",
"jobs": 16,
"verbose": false,
"configuration": "Release",
"targets": [
"install"
],
"configurePreset": "cuda-release"
},
{
"name": "trt-release",
"description": "",
"displayName": "",
"jobs": 16,
"verbose": false,
"configuration": "Release",
"targets": [
"install"
],
"configurePreset": "trt-release"
}
]
- 在
cmake tool
中选择对应的目标,进行预配置,编译以及执行。
当没有传参时,Debug
- 当没有参数传递的时候,debug,直接使用
cmake tool
中的调试开启调试,或者执行 也同理
当存在参数传递时。
- 配置
.vscode
中的 launch.json
文件。
{
"version": "0.2.0",
"configurations": [
{
"name": "(msvc) Launch",
"type": "cppvsdbg",
"request": "launch",
// Resolved by CMake Tools:
"program": "${command:cmake.launchTargetPath}",
"args": ["${workspaceFolder}\\config.txt, "${workspaceFolder}\\model.onnx"],
"stopAtEntry": false,
"cwd": "${command:cmake.launchTargetDirectory}",
"environment": [
{
// add the directory where our target was built to the PATHs
// it gets resolved by CMake Tools:
"name": "PATH",
"value": "${env:PATH}:${command:cmake.getLaunchTargetDirectory}"
}
],
"externalConsole": true
}
]
}