6

I'm using BVLC Caffe on the Windows branch, which is currently unsupported.

When I try to compile pycaffe in debug mode on Visual Studio 2013 I get the errors

_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

However, pycaffe compiles in Release mode fine. I'm using Python 2.7.12 :: Anaconda 4.1.1 (64-bit) and I have added a python27_d.lib to the libs directory.

This not a duplicate of another question because:

Community
  • 1
  • 1
empty
  • 5,194
  • 3
  • 32
  • 58
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ken White Aug 09 '16 at 22:16

2 Answers2

12

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.

Copy pyconfig.h from your python directory to where the pycaffe source code is.

Find the following lines:

#ifdef _DEBUG
#   define Py_DEBUG
#endif

And edit it such that it looks like this:

#ifdef _DEBUG
//# define Py_DEBUG
#endif

Basically, don't define Py_DEBUG. Alternatively, you can simply modify the pyconfig.h file directly without copying it first.

The issue arises because python compiles extra code in debug mode that is not found in release mode, hence the libs and dlls should not be the same if compiled properly.

thejinx0r
  • 444
  • 7
  • 10
  • 2
    This worked for me! I hate that it involves manually editing a configuration file but, hey, you do what ya gotta do. Just make sure you keep good notes for the next poor sucker that has to maintain your code. – Kenny Cason Nov 30 '17 at 15:16
1

Undefining Py_DEBUG did not help me. Instead, reading the documentation on Py_DECREF helped: "The following functions are for runtime dynamic embedding of Python: Py_IncRef(PyObject *o), Py_DecRef(PyObject *o)."

ScaledLizard
  • 171
  • 1
  • 9