visual-studiocaffepycaffe

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:


Solution

  • 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.