win10_opencv4.2_cuda11_vs2019 编译

时间:2022-07-24
本文章向大家介绍win10_opencv4.2_cuda11_vs2019 编译,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

查看cuda11支持的vs版本:https://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/index.html

1,安装 visual studio 2019 community 免费版

2,下载安装CUDA Toolkit,https://developer.nvidia.com/cuda-downloads?target_os=Windows&target_arch=x86_64&target_version=10,一种是网络下载安装,一种是独立安装包,建议第二个,可以反复装,而且不会有奇怪错误。

3,下载opencv4.2 source包和扩展包

https://github.com/opencv/opencv/releases/download/4.2.0/opencv-4.2.0-vc14_vc15.exe
https://github.com/opencv/opencv_contrib/archive/4.2.0.zip

4,下载cmake最新版本

cmake-3.18.2-win64-x64

5,解压后,目录有如下:

-D:software
    -build_opencv                    //存放cmake目标文件
    -cmake-3.18.2-win64-x64          //cmake安装包位置
    -opencv-4.2.0                    //opencv4.2源码包
       -opencv_contrib-4.2.0         //上面的扩展包 opencv_contrib 加压后放在这里

————第1阶段:配置cmake

6,双击 cmake-3.18.2-win64-x64bincmake-gui.exe 打开cmake界面

目录填写参考上面的

7,点击【configure】目标,弹出对话框选择如下:

8,然后点击【Finish】完成config之后,然后找到 OPENCV_EXTRA_MODULES_PATH 设置扩展模块的源代码路径【注意:是下级modules 目录!!!】 ,如下:

9,再configure

10,在搜索框中 WITH_CUDABUILD_CUDA_STUBS,都打勾,点击【configure】按钮。

11,在搜索框中 CUDA_FAST_MATH,打勾,再configure。没有错误,完成。

12,点击【generate】按钮,生成项目。

两个done

————第2阶段:使用vs生成lib库

这个时候去 build_opencv 这里目录下,双击打开 OpenCV.sln(VS2019工程文件),

默认debug版本,生成的lib和dll带有字母d的后缀

Debug 版本,点击顶部生成菜单,点击重新生成解决方案。

很多失败的。。。

The problem is in the configuration step. Before pressing generate button, be sure that you specified the appropriate configurations. In this link , you can find an example of configuration to build opencv. Especially, take attention to 'WITH' segments.

重新来一遍 cmake 。。。

关闭vs,删掉build_opencv内容,重新打开cmake

依旧大量错误:

error MSB6006: “cmd.exe”已退出,代码为 1。
LINK : fatal error LNK1104: 无法打开文件“....libDebugopencv_gapi420d.lib”
...

再次搜问题,找到opencv issue里,https://github.com/opencv/opencv/issues/9908,解释如下:

I have encountered this problem today, using VS 2017 and CUDA 9.0.
The actual problem is the first error MSB6006 which leads to the other LNK1104 errors. I searched in google and find this solution:
https://stackoverflow.com/questions/45918446/compiling-opencv-3-3-with-cuda-9-0rc
When using CMAKE to generate the project, change the CUDA_HOST_COMPILER parameter to:
C:Program Files (x86)Microsoft Visual Studio2017CommunityVCToolsMSVC14.10.25017binHostX64x64cl.exe
The path of Visual Studio has changed in VS 2017 which leads to the errors.

This can solve most of problems you list, but some errors like: C2610, C2535, .etc still exists, but it seems affect only the performance tests. All OpenCV libraries are generated successfully and the INSTALL procedure also works fine.

好,再来一遍,我默认安装的vs2019 cl.exe路径为:

C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.26.28801binHostx64x64cl.exe

新增 cmake配置项为:CUDA_HOST_COMPILER ,(输入后,手动再选择一次,路径是斜杠,不是反斜杠)

依旧是如上错误,不得解。。。。。

头疼。

继续搜发现,

这个配置项 Build_CUDA_STUBS 一定不要 ✔ !!!!

接下来总下总结一下:

1. 换成了opencv4.4版本

2.配置了 OPENCV_EXTRA_MODULES_PATH,WITH_CUDA,CUDA_FAST_MATH,ENABLE_FAST_MATCH,BUILD_opencv_world。

编译时间非常长,大约在2--4个小时时间 编译好后应该不会有什么错。如果有一两个Matlab啊Python啊之类的错误请无视之。如果几十个几百个错可能就会很大程度上影响使用了。

编译好后,找到解决方案目录里的 [CMakeTargets] 项展开的 [INSTALL] 项,右键 -> [Project Only(仅项目)] -> [Build Only INSTALL (仅生成INSTALL)]。 就可以关闭vs2019了

1.2G巨大的debug版dll

接下来可以继续生成 release 版本的。

靠谱文章:https://blog.csdn.net/sinat_38679789/article/details/73466834

靠谱文章:https://kazusa.im/2020/08/08/compilingopencv4-4-0withcudaandopencv-python/

参考:https://cloud.tencent.com/developer/article/1523416

OPENCV_DNN_CUDA 这个配置项如果没有自己下载扩展包,就不要勾选,cmake会报错。