8

I am building a C++ application that will call python + numpy and I would like to DELAYLOAD the python dll. I use Visual Studio 2015 on Windows with 64 bit python 3.6. The DELAYLOAD works fine as long as I am not using numpy. As soon as I call import_array(), I can no longer build with DELAYLOAD option. The linker error is

LNK1194 cannot delay-load 'python36.dll' due to import of data symbol '__imp_PyExc_ImportError'; link without /DELAYLOAD:python36.dll.

Here is my code:

// Initialize python
Py_Initialize();

// If I remove this line, I am able to build with DELAYLOAD
import_array();

Is there any way to make delay load possible when using numpy?

Alternative question: is it possible to create and fill with data a numpy.recarray without calling import_array()?

EDIT: I decided to get rid of import_array(). Here is some of the code that I use to initialize Python:

    if (!Py_IsInitialized())
    {
        // Initialize Python
        Py_Initialize();

        // Initialize threads
        PyEval_InitThreads();

        // Needed for datetime
        PyDateTime_IMPORT;

        // Needed to avoid use of Py_None, Py_True, and Py_False;
        // which cause inability to use DELAYLOAD
        HMODULE pythonDll = GetModuleHandle(L"python36.dll");
        if (pythonDll == nullptr)
        {
            throw gcnew NotSupportedException(L"GS_ERR_CannotInitialize");
        }
        PythonHelper::_pyNone = (PyObject*)GetProcAddress(pythonDll, "_Py_NoneStruct");
        PythonHelper::_pyTrue = (PyObject*)GetProcAddress(pythonDll, "_Py_TrueStruct");
        PythonHelper::_pyFalse = (PyObject*)GetProcAddress(pythonDll, "_Py_FalseStruct");
    }
Andrey Belykh
  • 2,578
  • 4
  • 32
  • 46
  • Have you try two variants of build Release/Debug? Sometime this helps. – fghj Jul 26 '17 at 16:01
  • Unfortunately, I couldn't find and Debug build of numpy, so using only Release LIB / DLL – Andrey Belykh Jul 26 '17 at 17:30
  • How are you referencing numpy in your c++ app? Did you build it from source according to numpy instructions? – denfromufa Jul 29 '17 at 18:33
  • No, I am using the pre-built one from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy. Apparently, to build it a non-free fortran compilier is needed and I don't have one. – Andrey Belykh Jul 30 '17 at 01:20
  • you can sign-up for beta ifort compiler which is available for at least few months – denfromufa Jul 31 '17 at 06:12
  • Are you writing an extension module, or embedding Python in an application? Probably not related to your issue, but did you recall including `Python.h` before any standard headers? – sancho.s ReinstateMonicaCellio Jul 31 '17 at 06:40
  • I am embedding Python in an application. Here is my includes: #pragma unmanaged #include #include "Python.h" #include "datetime.h" #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #include "numpy/arrayobject.h" #pragma managed #include – Andrey Belykh Jul 31 '17 at 13:30

2 Answers2

1

Is there any way to make delay load possible when using numpy?

You might not be able to use DELAYLOAD with import_array:

  1. You cannot delay load a DLL if data is imported from it (official documentation).

  2. import_array imports the module where the function-pointer table is stored and points the correct variable to it (official documentation).

I doubt you are dealing with a case of exporting classes versus exporting data members. See this, this, or this.

0

This might also be caused by optimisation, as seen here.

You can also try to Remove unreferenced code and data in the project settings.

grml
  • 147
  • 8