如何在没有Python调试库的调试模式下使用Cmake / Visual Studio构建OpenCV

时间:2013-11-20 11:01:18

标签: c++ python visual-studio opencv cmake

我正在尝试使用Visual Studio 2012(vc11)从Windows 7上的源代码构建OpenCv 2.4.7。 我有

  • 从github下载了来源
  • 切换到2.4.7标记
  • 使用cmake配置并生成VS解决方案
  • 在发布模式和调试模式下运行Win32的BUILDALL目标

在发布模式下,我可以毫无问题地构建所有内容。 但是,当我尝试构建调试模式时,我收到以下错误:

  

错误2错误LNK1104:无法打开文件'python27_d.lib'C:\ Users ... \ OpenCV \ 2.4.7 \ build \ modules \ python \ LINK

我没有python27_d.lib,所以我只是简单地将python27.lib复制到python27_d.lib,希望是最好的,并重新启动cmake配置(可能最后一点毫无意义)。

现在,当我尝试构建时,我遇到了这些错误:

  

错误1错误LNK2019:未解析的外部符号 imp _Py_NegativeRefcount在函数“struct _object * cdecl pycvCreateHist(struct _object *,struct _object *,struct _object *)”中引用(?pycvCreateHist @ @ YAPAU_object @@ PAU1 @ 00 @ Z)C:\ Users ... \ OpenCV \ 2.4.7 \ build \ modules \ python \ cv2.obj   错误2错误LNK2019:未解析的外部符号__imp _Py_Dealloc在函数“struct _object * cdecl pycvCreateHist(struct _object *,struct _object *,struct _object *)”中引用(?pycvCreateHist @@ YAPAU_object @@ PAU1 @ 00 @ Z)C:\ Users ... \ OpenCV \ 2.4.7 \ build \ modules \ python \ cv2.obj   错误3错误LNK2019:未解析的外部符号__imp _PyObject_DebugMalloc在函数“struct _object * cdecl pyopencv_VideoCapture_VideoCapture(struct _object *,struct _object *,struct _object *)”中引用(?pyopencv_VideoCapture_VideoCapture @@ YAPAU_object @@ PAU1 @ 00 @ Z)C:\ Users ... \ OpenCV \ 2.4.7 \ build \ modules \ python \ cv2.obj   错误4错误LNK2019:未解析的外部符号__imp _PyObject_DebugFree在函数“void cdecl Capture_dealloc(struct _object *)”中引用(?Capture_dealloc @@ YAXPAU_object @@@ Z)C:\ Users ... \ OpenCV \ 2.4.7 \建立\模块\蟒\ cv2.obj   错误5错误LNK2019:未解析的外部符号_ imp _Py_InitModule4TraceRefs在函数“struct _object * __cdecl init_cv(void)”中引用(?init_cv @@ YAPAU_object @@ XZ)C:\ Users ... \ OpenCV \ 2.4.7 \建立\模块\蟒\ cv2.obj   错误6错误LNK2001:未解析的外部符号__imp _Py_RefTotal C:\ Users ... \ OpenCV \ 2.4.7 \ build \ modules \ python \ cv2.obj   错误7错误LNK1120:6个未解析的外部C:\ Users ... \ OpenCV \ 2.4.7 \ build \ lib \ Debug \ cv2.pyd

除了下载Python的源代码并进行调试构建之外,我有一个合适的python27_d.lib(其中我认为我没有其他需要),关于如何解决这个问题的任何想法?

2 个答案:

答案 0 :(得分:2)

根据OpenCv网站,您需要在使用CMake后将Visual Studio项目切换为发布。 http://docs.opencv.org/3.1.0/d5/de5/tutorial_py_setup_in_windows.html

可以通过右键单击解决方案并单击配置管理器来访问此设置来完成此操作。

我已经使用Visual Studio 2015社区,OpenCV 3.2.0和python 3.6(Anaconda)对此进行了测试

答案 1 :(得分:0)

找到pyconfig.h 变化

#ifdef _DEBUG 
# define Py_DEBUG 
#endif 

#ifdef _DEBUG 
//# define Py_DEBUG 
#endif 

chagne

# ifdef _DEBUG 
# pragma comment(lib,"python27_d.lib") 
# else 
# pragma comment(lib,"python27.lib") 
# endif /* _DEBUG */ 

# ifdef _DEBUG 
# pragma comment(lib,"python27.lib") 
# else 
# pragma comment(lib,"python27.lib") 
# endif /* _DEBUG */ 

修改object.h

#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS) 
#define Py_TRACE_REFS 
#endif 

#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS) 
// #define Py_TRACE_REFS 
#endif