How to embedd openCV Dll's in Executable
I wrote an image matching tool (console application, without gui or windows), using openCV. I want to port my EXE file to another computer but it asks for opencv dlls (opencv_core220.dll, opencv_highgui220.dll, ...)
My question is how to do it. I see two ways any of them is good:
- Recompile opencv as static library (.lib instead of .dll). That didnt work. I get 10 linker errors regarding cap_vfw.obj
- If po开发者_开发知识库ssible, how to merge/embedd DLL's into exe file.
I tried to use ILMerge, but it doesnt work (Error: coul'd not load assembly from a.exe file) since it is designed for .Net only
P.S. - I am using visual studio 2005, on windows, c++ compiler, openCV 2.2
I found an answer. You must open the original openCV project and recompile all the relevant parts in static library mode.
- Do it for each project starting from
libjasper
and alphabetically untilopencv_video
. Also do the same forzlib
project - For each project, go to project properties\configuration properties\general\configuration type and set it to static library
- Now recompile those projects. They will create many large lib files (of up to 10MB) each. Those files are located mainly in
modules
directory of opencv. You must find them and copy to the directory when you keep your regular lib files when compiling against dll's. Now is a tricky part - You have to include in your code more libraries, than when you compile against dll's
- Here is an example what your code should look like this. Sorry that it is not formatted well. Probably html bugs of this page:
#include "cv.h"
#include "highgui.h"
using namespace std;
using namespace cv;
// Directives to linker to include openCV lib files.
#ifndef STATIC_LIBRARY_LINK
// Linking against DLL. For each 'lib' file that appears below, final EXE will need a DLL.
// Core of openCV
#pragma comment(lib, "opencv_core220.lib")
#pragma comment(lib, "opencv_highgui220.lib")
#pragma comment(lib, "opencv_imgproc220.lib")
// Calibration and image matching
#pragma comment(lib, "opencv_flann220.lib")
#pragma comment(lib, "opencv_features2d220.lib")
#pragma comment(lib, "opencv_calib3d220.lib")
// Other libs that might be needed
/*#pragma comment(lib, "opencv_gpu220.lib")
#pragma comment(lib, "opencv_video220.lib")
#pragma comment(lib, "opencv_legacy220.lib")
#pragma comment(lib, "opencv_ml220.lib")
#pragma comment(lib, "opencv_objdetect220.lib")
#pragma comment(lib, "opencv_ffmpeg220.lib")
#pragma comment(lib, "opencv_contrib220.lib") */
#else
// Static linking. No DLL's would be required but EXE file will be bigger
// and linking in debug mode might produce many warnings since *.pdb are not always
// present with the lib files
// Core of openCV. Must be compiled as lib and not as dll's
#pragma comment(lib, "opencv_core.lib")
#pragma comment(lib, "opencv_highgui.lib")
#pragma comment(lib, "opencv_imgproc.lib")
// Calibration and image matching. Must be compiled as lib and not as dll's
#pragma comment(lib, "opencv_flann.lib")
#pragma comment(lib, "opencv_features2d.lib")
#pragma comment(lib, "opencv_calib3d.lib")
// Image I/O auxillary libraries. Must be compiled as lib and not as dll's
#pragma comment(lib, "libtiff.lib")
#pragma comment(lib, "libpng.lib")
#pragma comment(lib, "zlib.lib")
#pragma comment(lib, "libjasper.lib")
#pragma comment(lib, "libjpeg.lib")
// OpenCV linear algebra methods. Must be compiled as lib and not as dll's
#pragma comment(lib, "opencv_lapack.lib")
// Auxillary libs, found in visual studio microsoft sdk
#pragma comment(lib, "vfw32.lib")
#pragma comment( lib, "comctl32.lib" )
//#pragma comment(lib, "window_w32.lib") // Not needed
#endif
int main(void){
// Your code here
return 0;
}
The term you're looking for is static linking. "DLL" stands for "Dynamically Linked Library", which is the opposite of static. You cannot statically link a dynamically linked library. You need a "normal" library for that.
Go to the properties for the project you are building (right click on project in the solution explorer and seleck propeties). Now expand the Configuration properties->Linker option and under General, set the path to the statically linked libraries (i.e., with .lib extensions). Now select the Configuration properties->Linker->Input option and type in the names of all the libraries you want it to statically link to. Now rebuild the project and they should be linked in to the executable. It will warn you if the paths to the files are not correct.
精彩评论