Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;通过Ctypes在Python中初始化_Python_C++_Ctypes - Fatal编程技术网

C++;通过Ctypes在Python中初始化

C++;通过Ctypes在Python中初始化,python,c++,ctypes,Python,C++,Ctypes,我用C++编写了以下代码: #include <iostream> class Foo{ public: void bar(){ std::cout << "Hello" << std::endl; } }; extern "C" { Foo* Foo_new(){ return new Foo(); } # I tried __declspec(dllexport) on these

我用C++编写了以下代码:

#include <iostream>

class Foo{
public:
    void bar(){
        std::cout << "Hello" << std::endl;
    }
};
extern "C" {
Foo* Foo_new(){ return new Foo(); }  # I tried __declspec(dllexport) on these funcs, same error
void Foo_bar(Foo* foo){ foo->bar(); }
}
问题是它无法加载库本身:

Traceback (most recent call last):
  File "C:/Users/avishah/PycharmProjects/Math/pythoncpp/test.py", line 4, in <module>
    lib = ctypes.CDLL('C:\\Users\\avishah\\CLionProjects\\Math\\lib\\libpythonclass.dll')
  File "C:\Python\Python38\lib\ctypes\__init__.py", line 373, in __init__
    self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'C:\Users\avishah\CLionProjects\Math\lib\libpythonclass.dll' (or one of its dependencies). Try using the full path with constructor syntax.
回溯(最近一次呼叫最后一次):
文件“C:/Users/avishah/PycharmProjects/Math/pythoncpp/test.py”,第4行,在
lib=ctypes.CDLL('C:\\Users\\avishah\\CLionProjects\\Math\\lib\\libpythonclass.dll')
文件“C:\Python\Python38\lib\ctypes\\ uuuuu init\ uuuu.py”,第373行,在\uuuu init中__
self.\u handle=\u dlopen(self.\u名称,模式)
FileNotFoundError:找不到模块“C:\Users\avishah\CLionProjects\Math\lib\libpythonclass.dll”(或其依赖项之一)。尝试使用构造函数语法的完整路径。
我在网上尝试了很多例子,但都因为这个错误而失败了。。。我注意到的一件事是其他示例都有.so文件,但由于我在Windows上,我使用.dll文件,另外还需要u declspec(dllexport)

编辑:
我正在使用MinGW编译器。这与编译器有关吗?我应该使用MSVC编译器吗?

在线搜索错误消息,这并不是一个很少见的问题。@UlrichEckhardt我是这样做的。。。但是我找不到太多,一些人建议使用os.abspath,或者通过os添加dll目录,这些都不适合我。使用dependency walker或类似工具来确定
libpythonclass.dll
的依赖项。或者,使用dependency walker打开VS的命令提示符,然后运行
dumpbin/dependents libpythonclass.dll
。我尝试了依赖项遍历程序,它显示了打开文件时出现的大量错误。系统找不到指定的文件(2),底部显示“错误:至少一个必需的隐式或转发依赖项未找到”。警告:至少一个延迟加载依赖项模块未找到。'这些是使用VS cmd的依赖项:KERNEL32.dll、msvcrt.dll、libstdc++-6.dll
Traceback (most recent call last):
  File "C:/Users/avishah/PycharmProjects/Math/pythoncpp/test.py", line 4, in <module>
    lib = ctypes.CDLL('C:\\Users\\avishah\\CLionProjects\\Math\\lib\\libpythonclass.dll')
  File "C:\Python\Python38\lib\ctypes\__init__.py", line 373, in __init__
    self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'C:\Users\avishah\CLionProjects\Math\lib\libpythonclass.dll' (or one of its dependencies). Try using the full path with constructor syntax.