这两天测试OpenCV显示到MFC的Picture控件上,终于测试成功了,但是换到release模式下就会imread失败。发现问题是导入的lib问题。
因为VS如果通过Property Manager永久修改环境配置(即OpenCV头文件路径、库文件路径、库文件列表),debug模式的配置也会被release模式继承,对于路径来说没什么问题,对于库文件列表(即Property Manage -- Linker --Input)就有问题了。对OpenCV而言,xxxd.lib是给debug模式用的,xxx.lib是给release模式用的,如果要切换debug和release模式那每次都要重新配置一遍input。于是我把xxxd.lib和xxxd.lib都加入Input列表了,但是问题是,两者并存时,编译器会选择前面一个,也就是还是要debug模式下把xxxd.lib放在前面,xxx.lib放在后面,release模式下反之。
每次配置太麻烦了,于是我选择在预编译头文件stdafx.h中用宏定义来区分包含哪些库文件,因为windows下的debug模式是定义宏_DEBUG来设置的,所以一个#ifdef语句就OK了,下面给出自动生成这些代码的程序代码
#include "stdafx.h" #include <iostream> #include <string> #include <vector> #include <io.h> using namespace std; string dir_name = R"(D:Program Filesopencvuildx86vc14lib)"; string output_path = R"(C:output.txt)"; int main() { _finddata_t fd; intptr_t handle; vector<string> lib_debug; vector<string> lib_release; if ((handle = _findfirst((dir_name + "\*.lib").c_str(), &fd)) != -1) { while (_findnext(handle, &fd) != -1) { int len = strlen(fd.name); if (fd.name[len - 5] == 'd') { lib_debug.emplace_back(fd.name); } else { lib_release.emplace_back(fd.name); } } } _findclose(handle); FILE* fp = fopen(output_path.c_str(), "w+"); fprintf(fp, "#ifdef _DEBUG "); for (const auto& libname : lib_debug) { fprintf(fp, " #pragma comment(lib, "%s") ", libname.c_str()); } fprintf(fp, "#else "); for (const auto& libname : lib_release) { fprintf(fp, " #pragma comment(lib, "%s") ", libname.c_str()); } fprintf(fp, "#endif "); fclose(fp); printf("successed! "); return 0; }
dir_name根据安装OpenCV的路径而定,最终生成的结果output.txt如下(我这里是OpenCV 2.4.13)
#ifdef _DEBUG #pragma comment(lib, "opencv_calib3d2413d.lib") #pragma comment(lib, "opencv_contrib2413d.lib") #pragma comment(lib, "opencv_core2413d.lib") #pragma comment(lib, "opencv_features2d2413d.lib") #pragma comment(lib, "opencv_flann2413d.lib") #pragma comment(lib, "opencv_gpu2413d.lib") #pragma comment(lib, "opencv_highgui2413d.lib") #pragma comment(lib, "opencv_imgproc2413d.lib") #pragma comment(lib, "opencv_legacy2413d.lib") #pragma comment(lib, "opencv_ml2413d.lib") #pragma comment(lib, "opencv_nonfree2413d.lib") #pragma comment(lib, "opencv_objdetect2413d.lib") #pragma comment(lib, "opencv_ocl2413d.lib") #pragma comment(lib, "opencv_photo2413d.lib") #pragma comment(lib, "opencv_stitching2413d.lib") #pragma comment(lib, "opencv_superres2413d.lib") #pragma comment(lib, "opencv_ts2413d.lib") #pragma comment(lib, "opencv_video2413d.lib") #pragma comment(lib, "opencv_videostab2413d.lib") #else #pragma comment(lib, "opencv_contrib2413.lib") #pragma comment(lib, "opencv_core2413.lib") #pragma comment(lib, "opencv_features2d2413.lib") #pragma comment(lib, "opencv_flann2413.lib") #pragma comment(lib, "opencv_gpu2413.lib") #pragma comment(lib, "opencv_highgui2413.lib") #pragma comment(lib, "opencv_imgproc2413.lib") #pragma comment(lib, "opencv_legacy2413.lib") #pragma comment(lib, "opencv_ml2413.lib") #pragma comment(lib, "opencv_nonfree2413.lib") #pragma comment(lib, "opencv_objdetect2413.lib") #pragma comment(lib, "opencv_ocl2413.lib") #pragma comment(lib, "opencv_photo2413.lib") #pragma comment(lib, "opencv_stitching2413.lib") #pragma comment(lib, "opencv_superres2413.lib") #pragma comment(lib, "opencv_ts2413.lib") #pragma comment(lib, "opencv_video2413.lib") #pragma comment(lib, "opencv_videostab2413.lib") #endif
复制到新工程的stdafx.h最下面就行,这样的话就不需要配置input了,但是缺点是每次都要把代码复制进来,不过复制粘贴的操作也不会花费多少时间