Windows Caffe 调试模式下 pycaffe 中的 Lnk2019 错误

Lnk2019 error in pycaffe in debug mode for Caffe for Windows

我正在使用 BVLC Caffe on the Windows branch,目前不受支持。

当我在 Visual Studio 2013 年尝试在调试模式下编译 pycaffe 时出现错误

_caffe.obj : error LNK2019: unresolved external symbol __imp__Py_NegativeRefcount referenced in function _import_array
_caffe.obj : error LNK2019: unresolved external symbol __imp__Py_Dealloc referenced in function _import_array
_caffe.obj : error LNK2001: unresolved external symbol __imp__Py_RefTotal

但是,pycaffe 在 Release 模式下可以正常编译。我正在使用 Python 2.7.12 :: Anaconda 4.1.1(64 位)并且我在 libs 目录中添加了一个 python27_d.lib。

这不是另一个问题的重复,因为:

Edit: The answer below is only valid for python < 3.8. As of 3.8, this is no longer necessary as both debug and release are ABI compatible Edit 2: While it might be true that they are ABI compatible, using python 3.8 from conda-forge was not resolved. It still required this change.

将 pyconfig.h 从您的 python 目录复制到 pycaffe 源代码所在的位置。

找到以下几行:

#ifdef _DEBUG
#   define Py_DEBUG
#endif

然后编辑它,使其看起来像这样:

#ifdef _DEBUG
//# define Py_DEBUG
#endif

基本上,不要定义Py_DEBUG。 或者,您可以直接修改 pyconfig.h 文件而无需先复制它。

问题的出现是因为 python 在调试模式下编译了在发布模式下找不到的额外代码,因此如果编译正确,libs 和 dll 应该不会相同。

取消定义 Py_DEBUG 对我没有帮助。相反,阅读 documentation on Py_DECREF 有所帮助:“以下函数用于 Python 的运行时动态嵌入:Py_IncRef(PyObject *o),Py_DecRef(PyObject *o)。”