为什么在OpenCV中使用静态库而不是动态库?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (58)

我在VisualStudio 2012中有一个项目,它使用OpenCV动态库。它汇编、连接和运作良好。

我想改变这个项目,所以它使用静态库而不是动态库。

我将项目VC+目录中的库目录更改为

  C:\thirdparty\opencv\build\x86\vc11\lib

致:C:\第三方\OpenCV\build\x86\vc 11\staticlib

但是,当我想要构建这个项目时,我会得到很多链接器错误,例如:

 Error  110 error LNK2001: unresolved external symbol _TIFFWriteScanline    myproject\opencv_highgui245.lib(grfmt_tiff.obj) 

更重要的是,有很多错误,如:

 Error  1   error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' in myproject.obj    myproject\opencv_core245.lib(system.obj)    

对于使用动态库使用静态库的项目,我应该做哪些其他更改?

在更改/md到/mt并将一些新库添加到输入库列表之后:

opencv_calib3d245.lib
opencv_contrib245.lib
opencv_core245.lib
opencv_features2d245.lib
opencv_flann245.lib
libtiff.lib
libpng.lib
libjpeg.lib
libjasper.lib
IlmImf.lib
zlib.lib
opencv_gpu245.lib
opencv_haartraining_engine.lib
opencv_highgui245.lib
opencv_imgproc245.lib
opencv_legacy245.lib
opencv_ml245.lib
opencv_nonfree245.lib
opencv_objdetect245.lib
opencv_photo245.lib
opencv_stitching245.lib
opencv_ts245.lib
opencv_video245.lib
opencv_videostab245.lib

我发现了一些新的错误:

Error   9   error LNK2001: unresolved external symbol _AVIFileCreateStreamA@12  myproject\opencv_highgui245.lib(cap_vfw.obj)    
Error   8   error LNK2001: unresolved external symbol _AVIFileGetStream@16  myproject\opencv_highgui245.lib(cap_vfw.obj)    
Error   5   error LNK2001: unresolved external symbol _AVIFileInit@0    myproject\opencv_highgui245.lib(cap_vfw.obj)    
Error   7   error LNK2001: unresolved external symbol _AVIFileOpenA@16  myproject\opencv_highgui245.lib(cap_vfw.obj)    

显然库不见了,但是是哪一个?

需要添加更多的库来列出。库的完整清单如下:

opencv_calib3d245.lib
opencv_contrib245.lib
opencv_core245.lib
opencv_features2d245.lib
opencv_flann245.lib
libtiff.lib
libpng.lib
libjpeg.lib
libjasper.lib
IlmImf.lib
zlib.lib
opencv_gpu245.lib
opencv_haartraining_engine.lib
opencv_highgui245.lib
opencv_imgproc245.lib
opencv_legacy245.lib
opencv_ml245.lib
opencv_nonfree245.lib
opencv_objdetect245.lib
opencv_photo245.lib
opencv_stitching245.lib
opencv_ts245.lib
opencv_video245.lib
opencv_videostab245.lib
Vfw32.Lib
comctl32.lib

这解决了问题。

提问于
用户回答回答于

通过将项目的运行时库更改为/MTD,我能够让静态库在VS 2013中工作

然后包括这些链接器>输入>附加依赖项:

opencv_core248d.lib
opencv_imgproc248d.lib
opencv_highgui248d.lib
opencv_ml248d.lib
opencv_video248d.lib
opencv_features2d248d.lib
opencv_calib3d248d.lib
opencv_objdetect248d.lib
opencv_contrib248d.lib
opencv_legacy248d.lib
opencv_flann248d.lib
libpngd.lib
libtiffd.lib
zlibd.lib
IlmImfd.lib
libjasperd.lib
libjpegd.lib
comctl32.lib
gdi32.lib
vfw32.lib
用户回答回答于

对于使用OpenCV 3.0.0的Visual Studio 2012,这些问题仍然适用,并且此线程中的解决方案是相关的。这是我的设置让它起作用:

Windows的系统环境变量

在Windows系统环境变量中设置:(OPENCV_DIR = D:\OpenCV\build\x64\v11替换D:\OpenCV\为opencv的路径,64位机器为x64,32位机器为x86)。

将AdditionalLibraryDirectories使用staticlib

将其他库目录(视图>属性页>配置属性>链接器>常规>附加库目录)设置为: $(OPENCV_DIR)\staticlib;%(AdditionalLibraryDirectories)

运行时库

将代码生成>运行时库更改为Multi-threaded Debug (/MTd)每个用户的答案,否则会得到这种错误:

Error   1   error LNK2038: mismatch detected for 'RuntimeLibrary': value
'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in Source.obj
C:\Users\...\documents\visual studio 2012\Projects\OpenCVTest2\OpenCVTest2\opencv_core300d.lib(alloc.obj)   OpenCVTest2

最后,附加依赖关系列表

我的附加依赖项必须包含staticlib目录中的所有库名称。注意版本号 ; 因为我使用OpenCV 3.0.0,文件名以* 300d.lib结尾。我相信comctl32.libvfw32.lib不在staticlib中,但我添加它们以防万一(视图>属性页>配置属性>链接器>输入>附加依赖项):

opencv_calib3d300d.lib
opencv_core300d.lib
opencv_features2d300d.lib
opencv_flann300d.lib
opencv_hal300d.lib
opencv_highgui300d.lib
opencv_imgcodecs300d.lib
opencv_imgproc300d.lib
opencv_ml300d.lib
opencv_objdetect300d.lib
opencv_photo300d.lib
opencv_shape300d.lib
opencv_stitching300d.lib
opencv_superres300d.lib
opencv_ts300d.lib
opencv_video300d.lib
opencv_videostab300d.lib
libtiffd.lib
libpngd.lib
libjpegd.lib
libjasperd.lib
IlmImfd.lib
libwebpd.lib
ippicvmt.lib
zlibd.lib
comctl32.lib
vfw32.lib

扫码关注云+社区

领取腾讯云代金券