title: Dll dependencies missing when using ctypes with fortran dll compiled with intel fortran compiler and linking to intel's mkl library
body: <p>I am under windows. The fortran code in <code>mkl_example.f</code> :</p>
  <pre><code>    subroutine matmultmkl(M1, M2, M3, M, N, K) bind(c, name='matmultmkl')
          !DEC$ ATTRIBUTES DLLEXPORT :: matmultmkl
          use iso_c_binding, only: c_float, c_int
      
          integer(c_int),intent(in) :: M, N, K
          real(c_float), intent(in) :: M1(M, N), M2(N, K)
          real(c_float), intent(out):: M3(M, K)
  
          CALL DGEMM('N','N',M,K,N,1.,M1,M,M2,N,0.,M3,M)
      
      end subroutine 
  </code></pre>
  <p>I compile it with command line (in a cmd windows where I ran <code>compilervars.bat</code> before) using the following batch file :</p>
  <pre><code>@Echo off
  
  setlocal ENABLEDELAYEDEXPANSION
  SET &quot;IFORT_INITIAL_FLAGS=-c -fpp&quot;
  SET &quot;IFORT_OPTIMIZATION_FLAGS=/O3&quot;
  
  ifort %IFORT_OPTIMIZATION_FLAGS% %IFORT_INITIAL_FLAGS% /I&quot;C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2020.4.311\windows\mkl\include&quot; -o mkl_example.obj mkl_example.f
  ifort -dll -o mylib.dll mkl_example.obj /link /LIBPATH:&quot;C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2020.4.311\windows\mkl\lib\intel64_win&quot; mkl_intel_lp64.lib mkl_intel_thread.lib mkl_core.lib libiomp5md.lib 
  </code></pre>
  <p>Then I run the follow python script <code>mkl_example.py</code> :</p>
  <pre><code>from ctypes import *
  import time
  
  import os
  os.add_dll_directory(&quot;C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2020.4.311/windows/mkl/lib/intel64_win&quot;)
  os.add_dll_directory(&quot;C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2020.4.311/windows/compiler/lib/intel64_win&quot;)
  
  import numpy as np
  
  mylib = CDLL('./mylib.dll')
  
  mylib.matmultmkl.argtypes = [ POINTER(c_float), 
                                  POINTER(c_float), 
                                  POINTER(c_float),
                                  POINTER(c_int),
                                  POINTER(c_int),
                                  POINTER(c_int) ]
  mylib.matmultmkl.restype = None
  M=1000
  N=1000
  K=1000
  
  a = np.empty((M,N), dtype=c_float)
  b = np.empty((N,K), dtype=c_float)
  c = np.empty((M,K), dtype=c_float)
  
  a[:] = np.random.rand(M,N)
  b[:] = np.random.rand(N,K)
  
  # Fortran mkl call
  start = time.time()
  mylib.matmultmkl( a.ctypes.data_as(POINTER(c_float)), 
                      b.ctypes.data_as(POINTER(c_float)), 
                      c.ctypes.data_as(POINTER(c_float)), 
                      c_int(M), c_int(N), c_int(K) )
  stop = time.time()
  print(f&quot;Fortran mkl \t {stop - start}s&quot;)
  </code></pre>
  <p>The output is :</p>
  <pre><code>Traceback (most recent call last):
    File &quot;path\to\mkl_example.py&quot;, line 14, in &lt;module&gt;
      mylib = CDLL('./mylib.dll')
    File &quot;C:\PYTHON\3\3.8.2_64bits\lib\ctypes\__init__.py&quot;, line 373, in __init__
      self._handle = _dlopen(self._name, mode)
  FileNotFoundError: path\to\mylib.dll' (or one of its dependencies). Try using 
  the full path with constructor syntax.
  </code></pre>
  <p>If y remove all the mkl stuff and code a function myself, the <code>mylib.dll</code> is found at runtime by the python. Hence in the case where I link to the mkl, the message doesn't mean that even if <code>mylib.dll</code> is at the right place it is not found at runtime for some weird reason, but thet indeed one of it's dependencies is not found.</p>
  <p>I don't see what I can do except finding <em>all</em> dependencies at putting them in the same folder next to the fortran dll and the python file. But if this is the <em>only</em> way, maybe I prefer quitting definitely.</p>