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");
    }
7
  • Have you try two variants of build Release/Debug? Sometime this helps. Commented Jul 26, 2017 at 16:01
  • Unfortunately, I couldn't find and Debug build of numpy, so using only Release LIB / DLL Commented Jul 26, 2017 at 17:30
  • How are you referencing numpy in your c++ app? Did you build it from source according to numpy instructions? Commented Jul 29, 2017 at 18:33
  • No, I am using the pre-built one from lfd.uci.edu/~gohlke/pythonlibs/#numpy. Apparently, to build it a non-free fortran compilier is needed and I don't have one. Commented Jul 30, 2017 at 1:20
  • you can sign-up for beta ifort compiler which is available for at least few months Commented Jul 31, 2017 at 6:12

2 Answers 2

1
+50

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Could you please give a link to the official documentation of numpy that says that is is impossible.
@AndreyBelykh - Numpy documentation would likely not mention issues with linking with specific linkers+flags, that would be too specific. But I updated the answer with the official documentation of MSVS+numpy that allows the conclusion.
Thank you. Looks like I will have to avoid using import_array().
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.