Use opencv official file configuration, static compilation, and run OpenCV-2.3.x/2.4.x instance

Source: Internet
Author: User

Copyright Notice: All original articles in this blog can be used for personal, educational, or non-commercial purposes, however, it is important to ensure the integrity of the article and to indicate the original author, source and this statement in the form of a hyperlink without any modification.

Blog address: Http://blog.csdn.net/shuxiao9058

Original Author: Ji ya

When we need to test the program or send the program to other colleagues or students to let them run to test the program performance or logic, is it necessary for others to configure the opencv runtime environment step by step as you did when developing a program !? If you do not do this, a system error, as shown in figure 1, is displayed (the program cannot be started because "opencv_highgui241d.dll" is lost in the computer. ") Is it too difficult to reinstall the program to solve this problem? In fact, we can compile the program in static link mode, therefore, the steps for configuring the runtime environment are missing.

Figure 1 A system error is prompted when the dynamically compiled program runs on a machine without opencv configured.

Static compilation, that is, the compiled program can be directly copied to other machines and run directly. Without the need to attach a link library and configure environment variables.

Because opencv has provided the OpenCV-2.3.x -win-superpack.exeor opencv-2.4.x.exe file since 2.3.0, after decompression of this file, we will send a folder named "build", which has the Link Library of each platform, it also provides a static link library on the Windows platform. This article describes an example to illustrate how to configure and use the static Link Library.

As shown in the preceding figure (opencv2.3.x/2.4.x in visual studio2005/2008 and visual studio2010 configuration methods), place the opencv library file to configure the VC ++ DIRECTORY Options and configure the environment variables (this step can be left blank, but add the "bin" Directory of opencv to the "VC ++ executable file" directory option in the VC ++ directory ), note that the library directory used here is "staticlib" instead of "lib", which is not described here.

After the VC ++ directory in Visual Studio 2008 is configured, you can write the code.

(1) edit the code

"Create a project", select "Visual C ++-Empty Project", enter the project name "staticopencv", "add new project-code-C ++ file", and enter the file name "staticopencv. CPP ", edit the code. The test code used here is as follows:

#include <opencv/highgui.h> int main( int argc, char** argv ) {         IplImage*img = cvLoadImage( argv[1] );         cvNamedWindow("OpenCV", CV_WINDOW_AUTOSIZE );         cvShowImage("OpenCV", img );         cvWaitKey(0);         cvReleaseImage(&img );         cvDestroyWindow("OpenCV" );          return( 0 );}

(2) There are two ways to add additional dependencies. For details about how to add dependencies, see opencv 2.3.x/2.4.x in Visual Studio 2005/2008 and Visual Studio 2010.

The list of OpenCV-2.4.1 library files (select the appropriate one when using, here I want to use libjasperd. lib, libmongod. lib, libpngd. lib ", libmongod. lib, zlibd. lib (note that these must be used, because these are third-party libraries used by opencv) and opencv_highgui241d.lib and opencv_core241d.lib library files.

libjasper.liblibjasperd.liblibjpeg.liblibjpegd.liblibpng.liblibpngd.liblibtiff.liblibtiffd.libopencv_calib3d241.libopencv_calib3d241d.libopencv_contrib241.libopencv_contrib241d.libopencv_core241.libopencv_core241d.libopencv_features2d241.libopencv_features2d241d.libopencv_ffmpeg241.dllopencv_flann241.libopencv_flann241d.libopencv_gpu241.libopencv_gpu241d.libopencv_haartraining_engine.libopencv_haartraining_engined.libopencv_highgui241.libopencv_highgui241d.libopencv_imgproc241.libopencv_imgproc241d.libopencv_legacy241.libopencv_legacy241d.libopencv_ml241.libopencv_ml241d.libopencv_nonfree241.libopencv_nonfree241d.libopencv_objdetect241.libopencv_objdetect241d.libopencv_photo241.libopencv_photo241d.libopencv_stitching241.libopencv_stitching241d.libopencv_ts241.libopencv_ts241d.libopencv_video241.libopencv_video241d.libopencv_videostab241.libopencv_videostab241d.libzlib.libzlibd.lib

To help us know which library files we need to use, here we use the second method described in "opencv2.3.x/2.4.x in visual studio2005/2008 and visual studio2010 configuration methods" to add the program dependency library. We add the following code between the header file and the main function:

#pragma comment( lib,"libjasperd.lib" )#pragma comment( lib,"libjpegd.lib" )#pragma comment( lib,"libpngd.lib" )#pragma comment( lib,"libtiffd.lib" )#pragma comment( lib, "zlibd.lib") #pragma comment( lib,"opencv_highgui241d.lib" )#pragma comment( lib,"opencv_core241d.lib" )

Compile the program and you will find that the program cannot be compiled normally (as shown in error 2). This is mainly related to msvcrt. Lib (msvcr90.dll.

Figure 2 Compilation produces many errors

After querying the relevant information, we can see that you need to configure the project properties as follows: Right-click the project name and select "properties-configuration properties-C/C ++-code generation ", here we find that the default option of the "RunTime" option is "multi-threaded debug DLL (/MDD)", and there are four options (/mt,/MTD,/MD,/MDD ), the first two groups are static links to the Runtime Library. The code is directly inserted into the program without the support of the Runtime Library. The last two groups are dynamic links to the Runtime Library, msvcr90.dll or msvcp90.dll is required for running.

So here we set the "RunTime" option to "multi-thread (/mt)" or "multi-thread debugging (/MTD) "(pay attention to your compilation mode (debug or release), as shown in 3. Click OK after completing the settings.

Figure 3 set the "RunTime" option to "multi-thread debugging (/MTD )"

Then we compile the program again and find two more errors, as shown in figure 4.

Figure 4 there are two other errors in compiling a program

 

This is because our program is a Win32 program. We also need to add the vfw32.lib and comctl32.lib dependent library files. The final code is as follows:

# Include <opencv/highgui. h> // These dependent library files must be added. // These are third-party libraries used by opencv # pragma comment (Lib, "libjasperd. lib ") # pragma comment (Lib," libmongod. lib ") # pragma comment (Lib," libpngd. lib ") # pragma comment (Lib," libmongod. lib ") # pragma comment (Lib," zlibd. lib ") // Add the dependent library file according to your program # pragma comment (Lib," opencv_highgui241d.lib ") # pragma comment (Lib," opencv_core241d.lib ") // Add the two because Win32 Program # pragma comment (Lib, "vfw32.lib") # pragma comment (Lib, "comctl32.lib") int main (INT argc, char ** argv) {iplimage * IMG = cvloadimage (argv [1]); cvnamedwindow ("opencv", cv_window_autosize); cvshowimage ("opencv", IMG); cvwaitkey (0 ); cvreleaseimage (& IMG); cvdestroywindow ("opencv"); Return (0 );}

Compilation again, succeeded !!! At this point, we can copy the compiled execution program to other machines (the opencv function library and environment variables are not configured) to test whether the program is OK.

Note:

1. You may need to ignore the libcmt library as needed;

2. the executable program after static compilation is much larger than the executable program after dynamic compilation;

3. Do not compile the C ++ version of opencv program statically, which may be related to;

4. Reference link: http://blog.csdn.net/shuxiao9058/article/details/7525067

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.