Search code examples
c++c++14python-3.7cppyy

how to load library in cppyy?


I am trying cppyy in python to import C++ files. I want to load HSMdApi.h and HSMdApi.lib. Here is the dir structure

enter image description here

Here is the code

import cppyy
cppyy.include('include/HSMdApi.h')
cppyy.load_library('win64/HSMdApi.lib')

I load the header file succefully. But when I load the lib, I got an error

Error in <TWinNTSystem::DynamicPathName>: win64/HSMdApi.lib does not exist in [a list of environment PATH] or has wrong file extension (.dll)

if I remove the .lib, I got another error.

cppyy.load_library('win64/HSMdApi')

>> cling::DynamicLibraryManager::loadLibrary(): LoadLibrary: returned 126: The specified module could not be found.

I am using win10 64bit, AMD64bit, installed cppyy with Anaconda3, python 3.7. I remember there was an warning about "bdist_wheel" during cppyy installation.

 Command "bdist_wheel" is disabled
  WARNING: Legacy build of wheel for 'cppyy' created no files.
  Command output:
  Command "bdist_wheel" is disabled

I guess there is something setting problems. Since cppyy is quite new, I cannot find similar problems. Thank you for your help.


Solution

  • The library to load is the (dynamic) .dll, never the (static) .lib library. You do not need to specify the extension as it is added if not provided. Is the python executable used also 64 (the build is printed in the welcome message when starting python interactively)? The bdist_wheel warning can be ignored (wheels are disabled b/c pip does not respect requirement dependency ordering, but normal build does, hence fine if building for install instead of for wheels).

    load_library is meant to be a more portable version of ctypes.CDLL which you could alternatively try as maybe that gives a better error message. For CDLL, you do need to specify the full path. Example:

    import ctypes, os
    l = ctypes.CDLL(os.path.join('win64', 'HSMdApi.dll'))
    

    All that said, the above assumes that the problem is with loading of the HSMdApi.dll, but that should not involve cling or modules. Do you have dictionary remnants in the directory from where you are running the script? Or .rootmap files accessible through the PATH envar that point to libraries that have since been removed?