记录一次环境配置

OpenCV的环境配置

OpenCV下载安装

前往官网选择适合的OpenCV版本下载,并安装到合适的位置。

官网下载OpenCV

我的OpenCV安装在:D:\opencv4.5.2

添加OpenCV到系统Path变量

打开编辑系统环境变量,点击右下方的环境变量,在系统变量中找到PATH项,点击编辑添加OpenCV目录下的bin文件夹。

添加OpenCV至PATH

至于添加的是vc14还是vc15,参照下表

VS版本 VC
VS2015 VC14
VS2017及以上 VC15

添加OpenCV环境到VS项目

右键项目,选择最下方属性,打开属性配置窗口,选择是Debug模式或Release模式配置。

VS项目配置窗口

VC++目录配置OpenCV

  1. 点击包含目录,在包含目录中把OpenCV目录下的include文件夹添加。

此处我的添加是:

1
2
D:\opencv4.5.2\build\include\opencv2
D:\opencv4.5.2\build\include
  1. 点击库目录,在库目录中把OpenCV的lib文件添加,此处路径藏得比较深,参考我的添加。

此处我的添加是:

1
D:\opencv4.5.2\build\x64\vc15\lib

链接器配置OpenCV

  1. 点击输入,再点击附加依赖项,将OpenCV的lib文件添加,lib文件存放在上面库目录的文件夹中。
1
2
opencv_world452.lib
opencv_world452d.lib

注意,此处文件名带d的为Debug版本的依赖项,不带d的为Release版本的依赖项。
在配置Debug版本时把带d的文件放在不带d的文件之上,
在配置Release版本时把不带d的文件放在带d的文件之上。

如果显示找不到opencv_worldxxx.dll ,请把opencv_worldxxx.dll的路径添加到系统环境变量。

CUDA环境配置

CUDA下载安装

参考此条博客

我的CUDA目录在:G:\NVIDIA_GPU_Computing_Toolkit_CUDA_v12.0

添加CUDA环境到VS项目

配置为x64平台。

右键项目→生成依赖项→生成自定义→勾选“CUDA xxx”。

生成依赖项

VC++目录配置CUDA

  1. 点击包含目录,在包含目录中把OpenCV目录下的include文件夹添加。

此处我的添加是:

1
G:\NVIDIA_GPU_Computing_Toolkit_CUDA_v12.0\include
  1. 点击库目录,在库目录中把OpenCV的lib文件夹添加。

此处我的添加是:

1
G:\NVIDIA_GPU_Computing_Toolkit_CUDA_v12.0\lib\x64

链接器配置CUDA

  1. 点击常规,再点击附加库目录,添加下面的目录。似乎不配置这一条也可以
1
$(CUDA_PATH_V12_0)\lib\$(Platform)

1
G:\NVIDIA_GPU_Computing_Toolkit_CUDA_v12.0\lib\x64
  1. 点击输入,再点击附加依赖项,将CUDA的lib文件添加,输入下面的内容。
1
G:\NVIDIA_GPU_Computing_Toolkit_CUDA_v12.0\lib\x64\*.lib

配置源码文件风格

右键源文件,可以添加新建项中选择“CUDA C/C++ File”。
右键“xxx.cu”源文件,点击属性,选择配置属性中的常规,从项类型中找到“CUDA C/C++”。

测试CUDA代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>

cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);

__global__ void addKernel(int *c, const int *a, const int *b)
{
int i = threadIdx.x;
c[i] = a[i] + b[i];
}

int main()
{
const int arraySize = 5;
const int a[arraySize] = { 1, 2, 3, 4, 5 };
const int b[arraySize] = { 10, 20, 30, 40, 50 };
int c[arraySize] = { 0 };

// Add vectors in parallel.
cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addWithCuda failed!");
return 1;
}

printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
c[0], c[1], c[2], c[3], c[4]);

// cudaDeviceReset must be called before exiting in order for profiling and
// tracing tools such as Nsight and Visual Profiler to show complete traces.
cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceReset failed!");
return 1;
}

return 0;
}

// Helper function for using CUDA to add vectors in parallel.
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size)
{
int *dev_a = 0;
int *dev_b = 0;
int *dev_c = 0;
cudaError_t cudaStatus;

// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}

// Allocate GPU buffers for three vectors (two input, one output) .
cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}

cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}

cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}

// Copy input vectors from host memory to GPU buffers.
cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}

cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}

// Launch a kernel on the GPU with one thread for each element.
addKernel<<<1, size>>>(dev_c, dev_a, dev_b);

// Check for any errors launching the kernel
cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
goto Error;
}

// cudaDeviceSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
goto Error;
}

// Copy output vector from GPU buffer to host memory.
cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}

Error:
cudaFree(dev_c);
cudaFree(dev_a);
cudaFree(dev_b);

return cudaStatus;
}

运行结果如下图:

测试结果

CUDA环境的一些碰壁

  1. 我的CUDA安装在一个可移动固态硬盘(G盘)中,可能导致了我的CUDA项目只能位于G盘使用,复制粘贴到电脑的盘会出现问题。

  2. 如果出现了大段指令无法运行,像下面的错误显示,可以复制命令(引号部分)到cmd运行,查看更详细的报错原因。

1
2
3
4
5
6
// VS报错如下
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 MSB3721 命令“"G:\NVIDIA_GPU_Computing_Toolkit_CUDA_v12.0\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "E:\Microsoft_Visual_Studio\VS\VC\Tools\MSVC\14.33.31629\bin\HostX64\x64" -x cu -IG:\NVIDIA_GPU_Computing_Toolkit_CUDA_v12.0\include -IG:\NVIDIA_GPU_Computing_Toolkit_CUDA_v12.0\include -G --keep-dir x64\Debug -maxrregcount=0 --machine 64 --compile -cudart static -g -D_DEBUG -D_CONSOLE -D_UNICODE -DUNICODE -Xcompiler "/EHsc /W3 /nologo /Od /FdG:\Data\Coding_Master\yolov5-trt\bin\immediate\x64\Debug\vc143.pdb /FS /Zi /RTC1 /MDd " -o G:\Data\Coding_Master\yolov5-trt\bin\x64\File.cu.obj "G:\Data\Coding_Master\yolov5-trt\File.cu"”已退出,返回代码为 1。 yolov5-trt E:\Microsoft_Visual_Studio\VS\MSBuild\Microsoft\VC\v170\BuildCustomizations\CUDA 12.0.targets 794

// 复制命令部分,即下面部分到cmd输入
"G:\NVIDIA_GPU_Computing_Toolkit_CUDA_v12.0\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "E:\Microsoft_Visual_Studio\VS\VC\Tools\MSVC\14.33.31629\bin\HostX64\x64" -x cu -IG:\NVIDIA_GPU_Computing_Toolkit_CUDA_v12.0\include -IG:\NVIDIA_GPU_Computing_Toolkit_CUDA_v12.0\include -G --keep-dir x64\Debug -maxrregcount=0 --machine 64 --compile -cudart static -g -D_DEBUG -D_CONSOLE -D_UNICODE -DUNICODE -Xcompiler "/EHsc /W3 /nologo /Od /FdG:\Data\Coding_Master\yolov5-trt\bin\immediate\x64\Debug\vc143.pdb /FS /Zi /RTC1 /MDd " -o G:\Data\Coding_Master\yolov5-trt\bin\x64\File.cu.obj "G:\Data\Coding_Master\yolov5-trt\File.cu"
  1. 还遇到一些其他的坑可以自行必应。