我在visual studio 2012中有一个使用opencv动态库的项目。它汇编,链接和运作良好。
我想更改项目,因此它使用静态库而不是动态库。
我从
更改了项目VC ++目录中的库目录 C:\thirdparty\opencv\build\x86\vc11\lib
于: C:\ thirdparty \ opencv \ build \ x86 \ vc11 \ 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)
我应该做哪些其他更改来转换使用动态库的项目来使用静态库?
修改1
将/ 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)
显然有些图书馆遗失了,但是哪一个?
编辑2 需要添加更多库到列表。图书馆的完整清单如下:
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
这解决了这个问题。
答案 0 :(得分:11)
我可以通过将项目的运行时库更改为/ 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
答案 1 :(得分:7)
如果您正在使用CMake进行构建,那么它非常简单,因为它是OpenCV CMake选项之一;只需将BUILD_WITH_STATIC_CRT设置为关闭。例如。在CMake命令行上
-DBUILD_WITH_STATIC_CRT=OFF
答案 2 :(得分:4)
对于使用OpenCV 3.0.0的Visual Studio 2012,这些问题仍然适用,并且此线程中的解决方案是相关的。这是我的设置,让它工作:
在Windows的系统环境变量中设置:OPENCV_DIR = D:\OpenCV\build\x64\v11
(将D:\OpenCV\
替换为opencv的路径。此外,x64用于64位计算机,x86用于32位计算机)。
将附加库目录(查看>属性页>配置属性>链接器>常规>其他库目录)设置为:$(OPENCV_DIR)\staticlib;%(AdditionalLibraryDirectories)
更改代码生成>每个uosɐſ's answer运行时库到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.lib
和vfw32.lib
不在staticlib中,但我添加它们以防万一(查看&gt;属性页&gt;配置属性&gt;链接器&gt;输入&gt;其他依赖项):< / p>
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
答案 3 :(得分:3)
伙计,我告诉你......去过那里做过...... 我尝试了静态的lib(几次......)这很邪恶。
尝试了2天之后我决定浪费了足够的时间,然后退回到DLL,如果使用VS2010则可以正常工作。
让它不是一个岔道..如果您确实设法链接,请与我们分享如何:)
答案 4 :(得分:1)
我已经构建了一个依赖OpenCV 2.4.9和静态链接的应用程序。
1) 我刚刚添加了链接器附加依赖项:
opencv_core249d.lib opencv_imgproc249d.lib opencv_highgui249d.lib opencv_ml249d.lib opencv_video249d.lib opencv_features2d249d.lib opencv_calib3d249d.lib opencv_objdetect249d.lib opencv_contrib249d.lib opencv_legacy249d.lib opencv_flann249d.lib libpngd.lib libtiffd.lib zlibd.lib IlmImfd.lib libjasperd.lib libjpegd.lib comctl32.lib GDI32.LIB vfw32.lib
2) 链接器=&gt;一般=&gt;其他图书馆馆长=&gt;将$(OPENCV_DIR)\ x86 \ vc12 \ lib更改为$(OPENCV_DIR)\ x86 \ vc12 \ staticlib,其中OPENCV_DIR是环境变量设置为: C:\ OpenCV 2.4.9 \ opencv \ build
3) 并改变了C / C ++ =&gt;代码生成=&gt;多线程调试DLL(MD)到多线程调试(MTd)
并享受成功的建设。
同样适用于发布模式(当然是针对非调试库的链接)