Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lnk2019 error in pycaffe in debug mode for Caffe for Windows

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:

  • The symbols are resolved in the Release mode but not the Debug mode. (https://stackoverflow.com/a/12573818/1637126)

  • The symbols are not virtual (https://stackoverflow.com/a/12574407/1637126)

  • The symbols are declared and defined in release mode (What is an undefined reference/unresolved external symbol error and how do I fix it?)

  • The Python27.lib and Python27_d.lib libraries exist and are in the same directory. (https://stackoverflow.com/a/12574400/1637126)

  • Both the Release and Debug libraries are linked in the same order. (https://stackoverflow.com/a/24675715/1637126)

  • The symbols are in C++ and moreover work in Release mode but not Debug mode (https://stackoverflow.com/a/12574420/1637126)

  • Recompiling and restarting doesn't work. (https://stackoverflow.com/a/20358542/1637126)

  • The Release and Debug modes of the Python libraries, although named differently are in fact copies of each other. So what works for one should work for the other. (https://stackoverflow.com/a/12574423/1637126)

  • They are not template classes. (https://stackoverflow.com/a/26233563/1637126)

like image 319
empty Avatar asked Aug 09 '16 21:08

empty


2 Answers

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.

like image 90
thejinx0r Avatar answered Nov 07 '22 18:11

thejinx0r


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)."

like image 20
ScaledLizard Avatar answered Nov 07 '22 18:11

ScaledLizard