From http://blog.csdn.net/chenyusiyuan/article/details/8706177
Here we will introduce a method to automatically identify the opencv version number in the header file and add the corresponding dependent Library (. Lib file) accordingly. DetailsCodeAs follows:
# Ifndef _ predep_h _ # Define _ Predep_h _ # Pragma Once # Include " Targetver. h " # Include <Stdio. h> # Include <Tchar. h> # Include <Iostream>
# Include " Opencv2/CORE/version. HPP " # Include " Opencv2/imgproc. HPP " # Include " Opencv2/highgui. HPP " # Include " Opencv2/calib3d/calib3d. HPP " # Define Cv_version_id cvaux_str (cv_major_version) cvaux_str (cv_minor_version) cvaux_str (cv_subminor_version) # Ifdef _ debug # Define Cvlib (name) "opencv _" name cv_version_id "D" # Else # Define Cvlib (name) "opencv _" name cv_version_id # Endif # Pragma Comment (Lib, cvlib ("core ")) # Pragma Comment (Lib, cvlib ("imgproc ")) # Pragma Comment (Lib, cvlib ("highgui ")) # Pragma Comment (Lib, cvlib ("FLANN ")) # Pragma Comment (Lib, cvlib ("features2d ")) # Pragma Comment (Lib, cvlib ("calib3d ")) # Endif
First, obtain the version number through the version. HPP file of opencv, and then concatenate the numbers cv_major_version, cv_minor_version, and cv_subminor_version into a string cv_version_id. Then, define a macro cvlib named lib File Based on the naming rules of the Lib file of opencv and the current compiling environment (debug or release. Finally, use the # pragma comment (Lib, *) command to add the required dependent libraries.
The convenience of the above method is that we only need to specify the name of the dependent Library, through the defined macro can automatically generate and add the dependent library corresponding to the installed opencv version; in addition, when you upgrade opencv in the future, you do not need to change the code unless the new version of the dependent database naming rules change,ProgramIt will automatically determine the opencv version and find the corresponding dependent library.
This method is based on wxWidgets. For more techniques, see wxWidgets \ include \ msvc \ wx \ setup. h and wxWidgets \ include \ wx \ version. h.
p.s.: In addition, it is recommended to set a special item in the system environment variable to store the installation path of opencv, for example, name it opencvdir, then, in the Visual Studio project property VC ++ directory, set the include directory and library directory to $ (opencvdir)/include and $ (opencvdir)/lib. In this way, when you upgrade opencv later, you only need to modify the opencvdir path in the system environment variable, and you do not need to modify the project code and compiler settings.