【问题标题】:How to build OpenCV using Cmake/Visual Studio in Debug mode without Python debug library如何在没有 Python 调试库的调试模式下使用 Cmake/Visual Studio 构建 OpenCV
【发布时间】:2013-11-20 11:01:17
【问题描述】:

我正在尝试使用 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\build\modules\python\cv2.obj 错误 5 错误 LNK2019:未解析的外部符号 _imp_Py_InitModule4TraceRefs 在函数“struct _object * __cdecl init_cv(void)”(?init_cv@@YAPAU_object@@XZ) C:\Users...\OpenCV 中引用\2.4.7\build\modules\python\cv2.obj 错误 6 error LNK2001: unresolved external symbol __imp_Py_RefTotal C:\Users...\OpenCV\2.4.7\build\modules\python\cv2.obj 错误 7 error LNK1120: 6 unresolved externals C:\Users...\OpenCV\2.4.7\build\lib\Debug\cv2.pyd

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

【问题讨论】:

  • 如果您不小心选择了 Python..,能否检查您的 CMake 构建属性?在 CMake GUI 中搜索 python 并取消选择出现的任何内容。
  • @scap3y 感谢您的想法。因此,如果我想将 Python 与 OpenCV 一起使用,我必须在启用 Python 的情况下进行发布构建,然后重新配置以禁用 Python 并进行调试构建?这应该没问题,因为我想我只想在发布版本中使用 Python。
  • 如果您需要 Python,那么两者都可以启用。据我所知,您不可能为每个构建禁用某些模块链接。但是,您可以做的是生成2个不同的库(一个使用Python但用于发布,另一个没有Python但调试)并将它们安装在不同的目录中。
  • 发生这种情况是因为来自 Python.org 的 MSI 安装程序不提供调试版本的 python lib 文件。正如您发现复制发布版本不起作用。似乎是一个明显的问题,尤其是因为它们提供了 PDB,并且 pyconfig.h 试图引用 python27_d.lib

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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    找到 pyconfig.h 改变

    #ifdef _DEBUG 
    # define Py_DEBUG 
    #endif 
    

    #ifdef _DEBUG 
    //# define Py_DEBUG 
    #endif 
    

    香奈儿

    # 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
    

    【讨论】:

    • Thnaks,只需 #undef _DEBUG 以包含 Python.h 然后再次定义它会更容易。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 2018-08-24
    相关资源
    最近更新 更多