编译源码 - 【OpenCV 3.x + TBB + IPP + OpenMP + CUDA 6.5(更新cuda 8.0)】- 【win7x64 + vs2012】

时间:2022-05-15 02:01:58

1、CMake

先安装好TBB和CUDA,到官网下载,配置系统环境PATH(添加bin目录);CUDA安装教程

第一次configure时会自动下载ippicv,这里需要耗费一点时间。因为3.0默认进行了ipp的优化。下载后的文件存放于“sources\3rdparty\ippicv”下

勾选“WITH_TBB”、“WITH_IPP”、“WITH_CUDA”、“WITH_OPENMP”,cuda下面还有一个“WITH_FAST_MATH”,是cuda的快速数学库,牺牲精度谋求速度,如果只要求单精度浮点可以选择该项

勾选”BUILD_opencv_world“,则最后生成的库将会合并为两项”opencv_world300.dll“或”opencv_world300d.dll“,以及”opencv_ffmpeg300.dll“,这样做的好处是只需要引用两个库,方便快捷,坏处是很多可能用不着的库,其实没必要引用。

一路configure,遇到TBB not found就设置相应的路径,最后generate

2、打开OpenCV.sln,选择debug或release,build其中的INSTALL项目,等待……

3、build完成后,生成的lib、dll放在install文件夹内,在项目中引用这些库就可以。需要注意的是,如果想debug调试时进入源码,需要在系统环境变量PATH添加OpenCV.sln所在目录的bin\Debug路径,里面含有跟踪源码所需的pdb文件。


调用opencv库的小技巧:


*通过 .props文件 配置项目属性,避免每次新建项目后手动添加路径

找到Microsoft.Cpp.Win32.user.props文件,可以通过explore搜索"Microsoft.Cpp.Win32.user.props",也可以通过”属性管理器-Debug | win32“的添加现有属性表自动定位到其目录

记事本打开Microsoft.Cpp.Win32.user.props,全部复制

新建一个.props文件,如v11_x86.props,粘贴刚刚复制的内容,然后把  <PropertyGroup>一栏修改为:

  <PropertyGroup>
<!--执行路径,即bin文件夹-->
<ExecutablePath>D:\opencv\v11_x86_cuda_ipp_tbb_openmp\bin;D:\Program Files\tbb44_20150728oss\bin\ia32\vc11;$(ExecutablePath)</ExecutablePath>
<!--包含路径-->
<IncludePath>D:\opencv\v11_x86_cuda_ipp_tbb_openmp\include;D:\Program Files\tbb44_20150728oss\include;$(IncludePath)</IncludePath>
<ReferencePath>$(ReferencePath)</ReferencePath>
<!--库路径-->
<LibraryPath>D:\opencv\v11_x86_cuda_ipp_tbb_openmp\lib;D:\Program Files\tbb44_20150728oss\lib\ia32\vc11;$(LibraryPath)</LibraryPath>
<SourcePath>$(SourcePath)</SourcePath>
<ExcludePath>$(ExcludePath)</ExcludePath>
</PropertyGroup>
这里设置了三种路径,每台电脑是不一样的
最后在VS中,”属性管理器“-”添加现有属性表“,找到v11_x86.props并添加

通过这样做,只需要写一次props,然后每个新项目都添加这个props,这样就不用手动添加路径了。

注意,新建props必须通过VS中的”属性管理器“-”添加新项目属性表“,不能通过新建txt改后缀的方法,会造成Unicode的问题


*引用库(附加依赖项)

在lib目录下新建一个 .bat,内容如下:

dir *300d.lib /ON /B > lib_debug.txt
这可以把lib的文件名输出到一个txt里面,release的如下:

dir *300.lib /ON /B > lib_release.txt

然后打开txt,通过“替换”把文件名修改为

#pragma comment( lib, "opencv_XXXX.lib")
的形式,XXXX为保留的内容,如:

将 

opencv_calib3d300.lib

 替换为 

#pragma comment( lib, "opencv_calib3d300.lib")

然后在头文件中引用需要的lib,只需要直接拷贝上面修改得到的代码

这里直接贴上opencv 3.0的库引用代码:

(自己编译的包括cuda、ipp、tbb、openmp的库)

debug

#pragma comment( lib, "opencv_calib3d300d.lib")
#pragma comment( lib, "opencv_core300d.lib")
#pragma comment( lib, "opencv_cudaarithm300d.lib")
#pragma comment( lib, "opencv_cudabgsegm300d.lib")
#pragma comment( lib, "opencv_cudacodec300d.lib")
#pragma comment( lib, "opencv_cudafeatures2d300d.lib")
#pragma comment( lib, "opencv_cudafilters300d.lib")
#pragma comment( lib, "opencv_cudaimgproc300d.lib")
#pragma comment( lib, "opencv_cudalegacy300d.lib")
#pragma comment( lib, "opencv_cudaobjdetect300d.lib")
#pragma comment( lib, "opencv_cudaoptflow300d.lib")
#pragma comment( lib, "opencv_cudastereo300d.lib")
#pragma comment( lib, "opencv_cudawarping300d.lib")
#pragma comment( lib, "opencv_cudev300d.lib")
#pragma comment( lib, "opencv_features2d300d.lib")
#pragma comment( lib, "opencv_flann300d.lib")
#pragma comment( lib, "opencv_hal300d.lib")
#pragma comment( lib, "opencv_highgui300d.lib")
#pragma comment( lib, "opencv_imgcodecs300d.lib")
#pragma comment( lib, "opencv_imgproc300d.lib")
#pragma comment( lib, "opencv_ml300d.lib")
#pragma comment( lib, "opencv_objdetect300d.lib")
#pragma comment( lib, "opencv_photo300d.lib")
#pragma comment( lib, "opencv_shape300d.lib")
#pragma comment( lib, "opencv_stitching300d.lib")
#pragma comment( lib, "opencv_superres300d.lib")
#pragma comment( lib, "opencv_ts300d.lib")
#pragma comment( lib, "opencv_video300d.lib")
#pragma comment( lib, "opencv_videoio300d.lib")
#pragma comment( lib, "opencv_videostab300d.lib")
release
#pragma comment( lib, "opencv_calib3d300.lib")
#pragma comment( lib, "opencv_core300.lib")
#pragma comment( lib, "opencv_cudaarithm300.lib")
#pragma comment( lib, "opencv_cudabgsegm300.lib")
#pragma comment( lib, "opencv_cudacodec300.lib")
#pragma comment( lib, "opencv_cudafeatures2d300.lib")
#pragma comment( lib, "opencv_cudafilters300.lib")
#pragma comment( lib, "opencv_cudaimgproc300.lib")
#pragma comment( lib, "opencv_cudalegacy300.lib")
#pragma comment( lib, "opencv_cudaobjdetect300.lib")
#pragma comment( lib, "opencv_cudaoptflow300.lib")
#pragma comment( lib, "opencv_cudastereo300.lib")
#pragma comment( lib, "opencv_cudawarping300.lib")
#pragma comment( lib, "opencv_cudev300.lib")
#pragma comment( lib, "opencv_features2d300.lib")
#pragma comment( lib, "opencv_flann300.lib")
#pragma comment( lib, "opencv_hal300.lib")
#pragma comment( lib, "opencv_highgui300.lib")
#pragma comment( lib, "opencv_imgcodecs300.lib")
#pragma comment( lib, "opencv_imgproc300.lib")
#pragma comment( lib, "opencv_ml300.lib")
#pragma comment( lib, "opencv_objdetect300.lib")
#pragma comment( lib, "opencv_photo300.lib")
#pragma comment( lib, "opencv_shape300.lib")
#pragma comment( lib, "opencv_stitching300.lib")
#pragma comment( lib, "opencv_superres300.lib")
#pragma comment( lib, "opencv_ts300.lib")
#pragma comment( lib, "opencv_video300.lib")
#pragma comment( lib, "opencv_videoio300.lib")
#pragma comment( lib, "opencv_videostab300.lib")


--------------


2016 更新:

编译 opencv 3.x + CUDA 8.0 问题


错误

错误37140error C2059: 语法错误:“newline”D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp135
错误37137error C2061: 语法错误: 标识符“NppiGraphcutState”D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp120
错误37152error C2065: “nppiGraphcut8InitAlloc”: 未声明的标识符D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp254
错误37148error C2065: “nppiGraphcutInitAlloc”: 未声明的标识符D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp182
错误37144error C2065: “pState”: 未声明的标识符D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp127
错误37145error C2065: “pState”: 未声明的标识符D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp132
错误37142error C2143: 语法错误 : 缺少“;”(在“*”的前面)D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp141
错误37141error C2334: “{”的前面有意外标记;跳过明显的函数体D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp136
错误37139error C2833: “operator NppiGraphcutState”不是可识别的运算符或类型D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp135
错误37150error C3861: “nppiGraphcut_32f8u”: 找不到标识符D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp196
错误37149error C3861: “nppiGraphcut_32s8u”: 找不到标识符D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp191
错误37154error C3861: “nppiGraphcut8_32f8u”: 找不到标识符D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp274
错误37153error C3861: “nppiGraphcut8_32s8u”: 找不到标识符D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp267
错误37151error C3861: “nppiGraphcut8GetSize”: 找不到标识符D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp246
错误37146error C3861: “nppiGraphcutFree”: 找不到标识符D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp132
错误37147error C3861: “nppiGraphcutGetSize”: 找不到标识符D:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp174
错误37143error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 intD:\opencv\310\opencv\sources\modules\cudalegacy\src\graphcuts.cpp141
错误37212error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_cudalegacy310d.lib”D:\opencv\310\mybuild\modules\cudalegacy\LINK
错误37223error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_cudalegacy310d.lib”D:\opencv\310\mybuild\modules\cudalegacy\LINK
错误37440error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_cudalegacy310d.lib”D:\opencv\310\mybuild\modules\stitching\LINK
错误38012error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_cudalegacy310d.lib”D:\opencv\310\mybuild\modules\cudaobjdetect\LINK
错误38833error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_cudalegacy310d.lib”D:\opencv\310\mybuild\modules\cudaoptflow\LINK
错误39744error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_cudaobjdetect310d.lib”D:\opencv\310\mybuild\modules\cudaobjdetect\LINK
错误39746error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_cudaobjdetect310d.lib”D:\opencv\310\mybuild\modules\cudaobjdetect\LINK
错误39177error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_cudaoptflow310d.lib”D:\opencv\310\mybuild\modules\cudaoptflow\LINK
错误39179error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_cudaoptflow310d.lib”D:\opencv\310\mybuild\modules\cudaoptflow\LINK
错误39754error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_cudaoptflow310d.lib”D:\opencv\310\mybuild\modules\superres\LINK
错误39756error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_cudaoptflow310d.lib”D:\opencv\310\mybuild\modules\videostab\LINK
错误37450error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_stitching310d.lib”D:\opencv\310\mybuild\modules\stitching\LINK
错误37456error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_stitching310d.lib”D:\opencv\310\mybuild\modules\stitching\LINK
错误39766error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_superres310d.lib”D:\opencv\310\mybuild\modules\superres\LINK
错误39768error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_superres310d.lib”D:\opencv\310\mybuild\modules\superres\LINK

这是因为opencv 3.x不兼容CUDA 8.0,opencv 3.1是2015年底发布的,而cuda 8.0则是2016年发布的,opencv还来不及适配,只能等待新版本解决该问题?处女座选择继续捣鼓

cuda 8.0中缺少了NppiGraphcutState的声明,而在cuda 6.5中是有该结构体的声明,具体查看 NVIDIA GPU Computing Toolkit\CUDA\v6.5\include\nppi_computer_vision.h, cuda 8.0中也有该文件,但里面是空的

#ifndef NV_NPPI_COMPUTER_VISION_H
#define NV_NPPI_COMPUTER_VISION_H

/**
* \file nppi_computer_vision.h
* NPP Image Processing Functionality.
*/

#include "nppdefs.h"


#ifdef __cplusplus
extern "C" {
#endif


#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /* NV_NPPI_COMPUTER_VISION_H */

CUDA doc里面有说明:the NPP graphcut APIs, which have been deprecated in CUDA 7.5,也就是说不仅是cuda 8.0,连cuda 7.5也存在以上问题。所以,需要将opencv源码中调用了cuda的graphcut api的部分去掉。opencv 2.4.13是2016年中发布,其中就兼容了CUDA 8.0,查看了 opencv 2.4.13的graphcuts.cpp,果真就是这样做的

// GraphCut has been removed in NPP 8.0
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)
所以,只要将opencv 3.x的graphcuts.cpp的宏

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
替换为

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)


修改后,编译成功


--------------

尝试编译opencv 2.4.13+CUDA 8.0,报错

错误23716error C1076: 编译器限制 : 达到内部堆限制;使用 /Zm 指定更高的限制C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0605
错误23715error C3859: 超过了 PCH 的虚拟内存范围;请使用“-Zm115”或更大的命令行选项重新编译C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0605
错误23718error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_stitching2413d.lib”D:\opencv\2413\mybuild\modules\stitching\LINK
错误23722error LNK1104: 无法打开文件“..\..\lib\Debug\opencv_stitching2413d.lib”D:\opencv\2413\mybuild\modules\stitching\LINK
错误23723error MSB3073: 命令“setlocal
"C:\Program Files\CMake\bin\cmake.exe" -DBUILD_TYPE=Debug -P cmake_install.cmake
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
:VCEnd”已退出,代码为 1。C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.CppCommon.targets134
解决方法:opencv_stitching项目属性-C/C++-命令行-添加/Zm115 ,再build一次INSTALL,注意不要选择rebuild


小问题:CMake configure的时候warning

CMake Warning at cmake/OpenCVPackaging.cmake:23 (message):
CPACK_PACKAGE_VERSION does not match version provided by version.hpp
header!
Call Stack (most recent call first):
CMakeLists.txt:1105 (include)

分别试了CMake 2.8.12、3.7.1、3.4.3,均弹出warning,无解,不管



----END-----