I have a project in visual studio 2012 which uses opencv dynamic libraries. It compiled, linked and worked well.
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
C:\thirdparty\opencv\build\x86\vc11\lib
to: C:\thirdparty\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
After change /md to /mt and adding some new libraries to the list of input libraries:
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 2 need to add more library to list. Full list of library is as follow:
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.
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
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:
Windows' System Environment Variables
Set in Windows' System Environment Variables:
OPENCV_DIR = D:\OpenCV\build\x64\v11
(replaceD:\OpenCV\
with whatever your path to opencv is. Also, x64 for 64-bit machines, x86 for 32-bit machines).Use staticlib for AdditionalLibraryDirectories
Set the Additional Library Directories (View > Property Pages > Configuration Properties > Linker > General > Additional Library Directories) to:
$(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:Finally, the Additional Dependencies list
My Additional Dependencies must include all the library names in the
staticlib
directory. Mind the version numbers; since I'm using OpenCV 3.0.0, the filenames ends with *300d.lib. I believe thecomctl32.lib
andvfw32.lib
are not in the staticlib, but I added them just in case (View > Property Pages > Configuration Properties > Linker > Input > Additional Dependencies):x86 vs 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.
Dude, let me tell you... been there done that.. I tried the static lib thing (a couple of times..) It's evil.
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.
Let it not be a turnoff.. If you did manage to link, please share with us all how :)
I am able to get the static libraries working in VS 2013 by changing the project's Runtime Library to /MTd
and then including these Linker >> Input >> Additional Dependencies:
I have build an application which depends on OpenCV 2.4.9 with static linking.
1) I have just added linker additional dependencies:
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
3) and changed C/C++ => Code Generation => Multi-threaded debug DLL (MD) to Multi-threaded debug (MTd)
and enjoyed the successful build.
the same thing works for release mode (of course link against non-debug libs)