C++ 在opencv中使用静态库而不是动态库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16830842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:41:05  来源:igfitidea点击:

using static libraries instead of dynamic libraries in opencv

c++visual-studioopencv

提问by mans

I have a project in visual studio 2012 which uses opencv dynamic libraries. It compiled, linked and worked well.

我在 Visual Studio 2012 中有一个使用 opencv 动态库的项目。它编译、链接并运行良好。

I want to change the project so it uses static libraries instead of dynamic libraries.

我想更改项目,使其使用静态库而不是动态库。

I changed the library directories in project VC++ directory from

我将项目 VC++ 目录中的库目录从

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

to: C:\thirdparty\opencv\build\x86\vc11\staticlib

到:C:\第三方\opencv\build\x86\vc11\staticlib

but when I want to build the project, I am getting a lot of linker error such as:

但是当我想构建项目时,我收到了很多链接器错误,例如:

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

and more importantly a lot of error such as this:

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

 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)    

What other changes should I do to convert a project which uses dynamic libraries to use static libraries?

要将使用动态库的项目转换为使用静态库,我还应该做哪些其他更改?

Edit 1

编辑 1

After change /md to /mt and adding some new libraries to the list of input libraries:

将 /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

I am getting some new errors:

我收到了一些新错误:

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)    

Apparently some library is missing, but which one?

显然缺少一些图书馆,但哪个图书馆?

Edit 2need to add more library to list. Full list of library is as follow:

编辑 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

This solved the problem.

这解决了问题。

采纳答案by Jason Kleban

I am able to get the static libraries working in VS 2013 by changing the project's Runtime Library to /MTd

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

enter image description here

在此处输入图片说明

and then including these Linker >> Input >> Additional Dependencies:

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

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

回答by G Huxley

If you are building using CMake then it is very simple because it is one of the OpenCV CMake options; just set BUILD_WITH_STATIC_CRT to off. Eg. on the CMake command-line

如果您使用 CMake 进行构建,那么它非常简单,因为它是 OpenCV CMake 选项之一;只需将 BUILD_WITH_STATIC_CRT 设置为关闭。例如。在 CMake 命令行上

-DBUILD_WITH_STATIC_CRT=OFF

回答by mathiass

For Visual Studio 2012 with OpenCV 3.0.0, these problems still apply, and the solutions in this thread are relevant. Here's my setup to get it to work:

对于带有 OpenCV 3.0.0 的 Visual Studio 2012,这些问题仍然存在,并且该线程中的解决方案是相关的。这是我让它工作的设置:

Windows' System Environment Variables

Windows 的系统环境变量

Set in Windows' System Environment Variables: OPENCV_DIR = D:\OpenCV\build\x64\v11(replace D:\OpenCV\with whatever your path to opencv is. Also, x64 for 64-bit machines, x86 for 32-bit machines).

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

Use staticlib for AdditionalLibraryDirectories

将 staticlib 用于 AdditionalLibraryDirectories

Set the Additional Library Directories (View > Property Pages > Configuration Properties > Linker > General > Additional Library Directories) to: $(OPENCV_DIR)\staticlib;%(AdditionalLibraryDirectories)

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

Runtime Library

运行时库

Change the Code Generation > Runtime Library to Multi-threaded Debug (/MTd)per uos??'s answer, otherwise you'll get this kind of error:

将 Code Generation > Runtime Library 更改为Multi-threaded Debug (/MTd)per uos??'s answer,否则你会得到这样的错误:

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

Finally, the Additional Dependencies list

最后,附加依赖项列表

My Additional Dependencies must include all the library names in the staticlibdirectory. Mind the version numbers; since I'm using OpenCV 3.0.0, the filenames ends with *300d.lib. I believe the comctl32.liband vfw32.libare not in the staticlib, but I added them just in case (View > Property Pages > Configuration Properties > Linker > Input > Additional Dependencies):

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

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

x86 vs x64

x86 与 x64

I also ran into this issue that VS2012 claims the target machine does not match the module machine type like this guy. The solution is given here.

我也遇到过这个问题,VS2012 声称目标机器与这个家伙的模块机器类型不匹配。此处给出解决方案。

回答by mathiass

Dude, let me tell you... been there done that.. I tried the static lib thing (a couple of times..) It's evil.

伙计,让我告诉你......在那里做过......我尝试了静态库的东西(几次......)它是邪恶的。

  1. If you are using Visual Studio, you better degrade back to VS2010. VS2012 and up wasn't used to build these libraries. And that is true at least for version 244. You might just be able to link but you will get crashes with very basic functions.. So spare yourself.
  2. If you use GCC you might just find that the .a libs are evil indeed.. They were cross-compiled in windows for LINUX. Trying to link with cygwin, MinGw won't do the job.
  1. 如果您使用的是 Visual Studio,则最好降级回 VS2010。VS2012 及更高版本未用于构建这些库。至少对于版本 244 而言确实如此。您可能只能链接,但使用非常基本的功能会导致崩溃.. 所以请放过自己。
  2. 如果您使用 GCC,您可能会发现 .a 库确实是邪恶的。它们是在 Windows 中为 LINUX 交叉编译的。试图与 cygwin 链接,MinGw 不会完成这项工作。

After trying for 2 days I decided that it was enough time wasted, and retreated back to the DLL's, which work ok if you use VS2010.

尝试了 2 天后,我认为浪费了足够的时间,然后退回到 DLL 中,如果您使用 VS2010,它可以正常工作。

Let it not be a turnoff.. If you did manage to link, please share with us all how :)

让它不是一个关闭..如果你确实设法链接,请与我们分享如何:)

回答by dajuric

I have build an application which depends on OpenCV 2.4.9 with static linking.

我已经构建了一个应用程序,它依赖于具有静态链接的 OpenCV 2.4.9。

1) I have just added linker additional dependencies:

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

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) Linker => General => Additional Library Directories => changed $(OPENCV_DIR)\x86\vc12\lib to $(OPENCV_DIR)\x86\vc12\staticlib where OPENCV_DIR is environment variable set to: C:\OpenCV 2.4.9\opencv\build

2) 链接器 => 常规 => 附加库目录 => 将 $(OPENCV_DIR)\x86\vc12\lib 更改为 $(OPENCV_DIR)\x86\vc12\staticlib,其中 OPENCV_DIR 是环境变量设置为:C:\OpenCV 2.4.9 \opencv\build

3) and changed C/C++ => Code Generation => Multi-threaded debug DLL (MD) to Multi-threaded debug (MTd)

3) 并将 C/C++ => 代码生成 => 多线程调试 DLL (MD) 更改为多线程调试 (MTd)

and enjoyed the successful build.

并享受成功的构建。

the same thing works for release mode (of course link against non-debug libs)

同样的事情适用于发布模式(当然链接到非调试库)