概述
记录使用VSCODE中的CMAKE拓展构建项目时出现的报错
CMakePresets.json
:
{"version": 6,"configurePresets": [{"name": "x64-debug","displayName": "x64-debug","cmakeExecutable": "D:/Program Files/Microsoft Visual Studio/2022/Enterprise/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe","generator": "Ninja","binaryDir": "${workspaceFolder}/build/${presetName}","installDir": "${workspaceFolder}/install/${presetName}","cacheVariables": {"CMAKE_BUILD_TYPE": "Debug","CMAKE_C_COMPILER": "D:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe","CMAKE_CXX_COMPILER": "D:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe","CMAKE_MAKE_PROGRAM": "D:/Program Files/Microsoft Visual Studio/2022/Enterprise/Common7/IDE/CommonExtensions/Microsoft/CMake/Ninja/ninja.exe"},"condition": {"type": "equals","lhs": "${hostSystemName}","rhs": "Windows"}}]
}
CMakeLists.txt
:
cmake_minimum_required (VERSION 3.28)project("THREAD_YOLO_RT_VSCODE")add_executable(THREAD_YOLO_RT_VSCODE main.cpp main.h)if (CMAKE_VERSION VERSION_GREATER 3.12)set_property(TARGET THREAD_YOLO_RT_VSCODE PROPERTY CXX_STANDARD 20)
endif()
报错1:
The C++ compiler amd64cl.exe is not able to compile a simple test program
解决方法1:
按照The C++ compiler amd64cl.exe is not able to compile a simple test program的方法配置,无法解决。
这里直接找到所使用的cmake.exe
所对应的CMakeTestCCompiler.cmake
和CMakeTestCXXCompiler.cmake
然后分别修改:
set(CMAKE_C_COMPILER_WORKS TRUE) # 添加这一行
if(NOT CMAKE_C_COMPILER_WORKS)PrintTestCompilerStatus("C")...
set(CMAKE_CXX_COMPILER_WORKS TRUE) # 添加这一行
if(NOT CMAKE_CXX_COMPILER_WORKS)PrintTestCompilerStatus("CXX")...
然后,保存重新调试即可。
接下来继续写CMakeLists.txt
cmake_minimum_required (VERSION 3.28)# 如果支持,请为 MSVC 编译器启用热重载。
if (POLICY CMP0141)cmake_policy(SET CMP0141 NEW)set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()if(POLICY CMP0146)cmake_policy(SET CMP0146 NEW)
endif()project("THREAD_YOLO_RT_VSCODE")# 检查是否使用 MSVC 作为编译器
if (MSVC)# 如果是 MSVC,设置 OpenCV_DIR 为 MSVC 版本set(OpenCV_DIR "D:/program/opencv/build/x64/vc16/lib")find_package(OpenCV 4.10 REQUIRED)
else()# 如果不是 MSVC,设置 OpenCV_DIR 为 GCC 版本set(OpenCV_DIR "D:/program/Opencv411")find_package(OpenCV 4.1.1 REQUIRED)
endif()if(OpenCV_FOUND)message(STATUS "OpenCV library found at ${OpenCV_INCLUDE_DIRS}")
else()message(FATAL_ERROR "Cannot find OpenCV in the specified directory.")
endif()set(CUDA_HOST_COMPILER ${CMAKE_CXX_COMPILER})
set(CUDA_DIR "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2")find_package(CUDA REQUIRED)
if(CUDA_FOUND)message(STATUS "CUDA library is found!")
else()message(FATAL_ERROR "CUDA library not found!")
endif()# 设置CUDA NVCC编译器标志,指定优化级别和计算能力
set(CUDA_NVCC_FLAGS${CUDA_NVCC_FLAGS};-O3 # 优化级别-gencode arch=compute_50,code=sm_50 # 指定GPU架构和计算能力,这里是针对sm_61
)# 设置TensorRT的根目录并查找TensorRT头文件和库
set(TENSORRT_ROOT "D:/program/TensorRT-8.5.1.7")
find_path(TENSORRT_INCLUDE_DIR NvInfer.h HINTS ${TENSORRT_ROOT} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES include)
MESSAGE(STATUS "Found TensorRT headers at ${TENSORRT_INCLUDE_DIR}")
find_library(TENSORRT_LIBRARY_INFER nvinfer HINTS ${TENSORRT_ROOT} ${TENSORRT_BUILD} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib lib64 lib/x64)
find_library(TENSORRT_LIBRARY_INFER_PLUGIN nvinfer_plugin HINTS ${TENSORRT_ROOT} ${TENSORRT_BUILD} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib lib64 lib/x64)
find_library(TENSORRT_LIBRARY_NVONNXPARSER nvonnxparser HINTS ${TENSORRT_ROOT} ${TENSORRT_BUILD} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib lib64 lib/x64)
find_library(TENSORRT_LIBRARY_NVPARSERS nvparsers HINTS ${TENSORRT_ROOT} ${TENSORRT_BUILD} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib lib64 lib/x64)
set(TENSORRT_LIBRARY ${TENSORRT_LIBRARY_INFER} ${TENSORRT_LIBRARY_INFER_PLUGIN} ${TENSORRT_LIBRARY_NVONNXPARSER} ${TENSORRT_LIBRARY_NVPARSERS})
MESSAGE(STATUS "Find TensorRT libs at ${TENSORRT_LIBRARY}")
# 处理标准库查找结果
find_package_handle_standard_args(TENSORRT DEFAULT_MSG TENSORRT_INCLUDE_DIR TENSORRT_LIBRARY)
# 如果没有找到TensorRT库,输出错误信息
if(NOT TENSORRT_FOUND)message(ERROR "Cannot find TensorRT library.")
endif()aux_source_directory("src" SRC_LIST)add_executable(THREAD_YOLO_RT_VSCODE main.cpp main.h)
target_include_directories(THREAD_YOLO_RT_VSCODE PRIVATE ${OpenCV_INCLUDE_DIRS} ${TENSORRT_INCLUDE_DIR} ${CUDA_INCLUDE_DIRS} "include")
target_link_libraries(THREAD_YOLO_RT_VSCODE PRIVATE ${OpenCV_LIBS} ${CUDA_LIBRARIES} ${CUDA_CUBLAS_LIBRARIES} ${CUDA_cudart_static_LIBRARY} ${TENSORRT_LIBRARY})if (CMAKE_VERSION VERSION_GREATER 3.12)set_property(TARGET THREAD_YOLO_RT_VSCODE PROPERTY CXX_STANDARD 20)
endif()# TODO: 如有需要,请添加测试并安装目标。
报错2
[cmake] CMake Error at CMakeLists.txt:36 (find_package):
[cmake] By not providing "FindCUDA.cmake" in CMAKE_MODULE_PATH this project has
[cmake] asked CMake to find a package configuration file provided by "CUDA", but
[cmake] CMake did not find one.
[cmake]
[cmake] Could not find a package configuration file provided by "CUDA" with any of
[cmake] the following names:
[cmake]
[cmake] CUDAConfig.cmake
[cmake] cuda-config.cmake
解决方法2
Unknown CMake command “cuda_add_library“.
c++ - 如何使用 CMake 3.15 查找和链接 CUDA 库?
【已解决】cmake报告找不到CUDA环境@Windows VC2022
如何让cmake的CUDA找到(How to let cmake find CUDA)如何解决Specify CUDA_TOOLKIT_ROOT_DIR(未尝试)
CMake does not properly find CUDA library
最后修改CMakeLists.txt
# 将
set(CUDA_DIR "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2")
find_package(CUDA REQUIRED)
if(CUDA_FOUND)message(STATUS "CUDA library is found!")
else()message(FATAL_ERROR "CUDA library not found!")
endif()# 修改为
set(CUDA_TOOLKIT_ROOT_DIR "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2")
find_package(CUDAToolkit REQUIRED)
if(CUDAToolkit_FOUND)message(STATUS "CUDA library is found!")
else()message(FATAL_ERROR "CUDA library not found!")
endif()
继续写CMakeLists.txt
cmake_minimum_required (VERSION 3.28)# 如果支持,请为 MSVC 编译器启用热重载。
if (POLICY CMP0141)cmake_policy(SET CMP0141 NEW)set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()if(POLICY CMP0146)cmake_policy(SET CMP0146 NEW)
endif()project("THREAD_YOLO_RT_VSCODE")# 检查是否使用 MSVC 作为编译器
if (MSVC)# 如果是 MSVC,设置 OpenCV_DIR 为 MSVC 版本set(OpenCV_DIR "D:/program/opencv/build/x64/vc16/lib")find_package(OpenCV 4.10 REQUIRED)
else()# 如果不是 MSVC,设置 OpenCV_DIR 为 GCC 版本set(OpenCV_DIR "D:/program/Opencv411")find_package(OpenCV 4.1.1 REQUIRED)
endif()if(OpenCV_FOUND)message(STATUS "OpenCV library found at ${OpenCV_INCLUDE_DIRS}")
else()message(FATAL_ERROR "Cannot find OpenCV in the specified directory.")
endif()# 设置CUDA
set(CUDA_HOST_COMPILER ${CMAKE_CXX_COMPILER})
set(CUDA_TOOLKIT_ROOT_DIR "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2")
find_package(CUDAToolkit REQUIRED)
if(CUDAToolkit_FOUND)message(STATUS "CUDA library is found!")
else()message(FATAL_ERROR "CUDA library not found!")
endif()# 设置CUDA NVCC编译器标志,指定优化级别和计算能力
set(CUDA_NVCC_FLAGS${CUDA_NVCC_FLAGS};-O3 # 优化级别-gencode arch=compute_50,code=sm_50 # 指定GPU架构和计算能力,这里是针对sm_61
)# 设置TensorRT的根目录并查找TensorRT头文件和库
set(TENSORRT_ROOT "D:/program/TensorRT-8.5.1.7")
find_path(TENSORRT_INCLUDE_DIR NvInfer.h HINTS ${TENSORRT_ROOT} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES include)
MESSAGE(STATUS "Found TensorRT headers at ${TENSORRT_INCLUDE_DIR}")
find_library(TENSORRT_LIBRARY_INFER nvinfer HINTS ${TENSORRT_ROOT} ${TENSORRT_BUILD} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib lib64 lib/x64)
find_library(TENSORRT_LIBRARY_INFER_PLUGIN nvinfer_plugin HINTS ${TENSORRT_ROOT} ${TENSORRT_BUILD} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib lib64 lib/x64)
find_library(TENSORRT_LIBRARY_NVONNXPARSER nvonnxparser HINTS ${TENSORRT_ROOT} ${TENSORRT_BUILD} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib lib64 lib/x64)
find_library(TENSORRT_LIBRARY_NVPARSERS nvparsers HINTS ${TENSORRT_ROOT} ${TENSORRT_BUILD} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib lib64 lib/x64)
set(TENSORRT_LIBRARY ${TENSORRT_LIBRARY_INFER} ${TENSORRT_LIBRARY_INFER_PLUGIN} ${TENSORRT_LIBRARY_NVONNXPARSER} ${TENSORRT_LIBRARY_NVPARSERS})
MESSAGE(STATUS "Find TensorRT libs at ${TENSORRT_LIBRARY}")
# 处理标准库查找结果
find_package_handle_standard_args(TENSORRT DEFAULT_MSG TENSORRT_INCLUDE_DIR TENSORRT_LIBRARY)
# 如果没有找到TensorRT库,输出错误信息
if(NOT TENSORRT_FOUND)message(ERROR "Cannot find TensorRT library.")
endif()aux_source_directory("src" SRC_LIST)
add_executable(THREAD_YOLO_RT_VSCODE main.cpp main.h)
target_sources(THREAD_YOLO_RT_VSCODE PRIVATE ${SRC_LIST})
target_include_directories(THREAD_YOLO_RT_VSCODE PRIVATE ${OpenCV_INCLUDE_DIRS} ${TENSORRT_INCLUDE_DIR} ${CUDAToolkit_INCLUDE_DIRS} "include")
target_link_libraries(THREAD_YOLO_RT_VSCODE PRIVATE ${OpenCV_LIBS} ${CUDA_cublas_LIBRARY} ${CUDA_cudart_static_LIBRARY} ${TENSORRT_LIBRARY})
# target_link_directories(THREAD_YOLO_RT_VSCODE PRIVATE ${CUDAToolkit_LIBRARY_DIR})if (CMAKE_VERSION VERSION_GREATER 3.12)set_property(TARGET THREAD_YOLO_RT_VSCODE PROPERTY CXX_STANDARD 20)
endif()# TODO: 如有需要,请添加测试并安装目标。
错误3:
[build] 'DOSKEY' is not recognized as an internal or external command,
[build] operable program or batch file.
[build] RC Pass 1: command "rc /fo CMakeFiles\THREAD_YOLO_RT_VSCODE.dir/manifest.res CMakeFiles\THREAD_YOLO_RT_VSCODE.dir/manifest.rc" failed (exit code 0) with the following output:
[build] 系统找不到指定的文件。
[build] ninja: build stopped: subcommand failed.
解决方法3:
vs编译cmake报错RC Pass 1: command “rc /foCMakeFiles\cmTC_0cba6.dir/manifest.res CMakeFiles\cmTC_0cba6.di
在VScode中出现:‘DOSKEY‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
在VS中CMake生成出现报错 RC Pass 1: command “rc /foCMakeFiles\cmTC_2347.dir/manifest.res CMakeFiles\cmTC_2347
审核了环境变量,增加了C:\Windows\SysWOW64
项。
重新生成,出现报错:
MT: command "CMAKE_MT-NOTFOUND /nologo /manifest CMakeFiles\THREAD_YOLO_RT_VSCODE.dir/intermediate.manifest /out:CMakeFiles\THREAD_YOLO_RT_VSCODE.dir/embed.manifest /notify_update" failed (exit code 0x0) with the following output:
系统找不到指定的文件。
CMake设置MSVC工程MT/MTd/MD/MDd
运行时库 /MT /MTD /MD /MDD
Windows 下基于 Visual Studio Code 使用 CMake + MinGW 配置 C++ 开发环境
尝试参考方法无效。最后尝试删除缓存并重新配置:
再重新生成,即可生成THREAD_YOLO_RT_VSCODE.exe
,调试运行有效。