如何使用 python 调用 C++ 函数
How can I call a c++ function using python
我想知道是否有办法在 python 代码中使用 c++ 函数
例如,在进行研究后,我确实找到了使用 .dll 文件的解决方案。
但是找不到函数
我的代码:
fun.cpp:
#include <iostream>
extern int add(int a, int b) {
return a+b;
}
int main()
{
std::cout << "Hello World! from C++" << std::endl;
return 0;
}
使用 cmd 编译它:
g++ fun.cpp -o fun.dll
使用 Python 调用函数,ctypes:
from ctypes import *
import os
mydll = cdll.LoadLibrary("C:/Users/User/Desktop/ctypes/fun.dll")
result= mydll.add(10,1)
print("Addition value:-"+result)
但是我遇到了这个错误:
Traceback (most recent call last): File
"c:\Users\User.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\ptvsd_launcher.py",
line 43, in
main(ptvsdArgs) File "c:\Users\User.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\lib\python\old_ptvsd\ptvsd__main__.py",
line 432, in main
run() File "c:\Users\User.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\lib\python\old_ptvsd\ptvsd__main__.py",
line 316, in run_file
runpy.run_path(target, run_name='main') File "C:\Python36\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname) File "C:\Python36\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name) File "C:\Python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals) File "c:\Users\User\Desktop\ctypes\test.py", line 5, in
result= mydll.add(10,1) File "C:\Python36\lib\ctypes__init__.py", line 361, in getattr
func = self.getitem(name) File "C:\Python36\lib\ctypes__init__.py", line 366, in getitem
func = self._FuncPtr((name_or_ordinal, self)) AttributeError: function 'add' not found
我认为您应该检查 Python 的 LoadLibrary
是否正确调用了 "fun.dll" 的 main()
函数。 loading的时候应该会寻找DllMain()
函数,然后需要获取函数地址才能调用函数。
C++ 破坏了导出的名称。这是一个应该在 Windows 和 Linux 上编译的示例。 __declspec(dllexport)
需要在 Windows 上导出函数,但 Linux 不需要。 extern "C"
需要使用 C 约定导出函数名称,而不是名称损坏的 C++ 约定。 C++ 中需要名称修饰来指示函数参数和 return 类型,因为 C++ 可以有多个具有相同名称但采用不同参数的函数。 C 约定不支持同名的多个函数。
fun.cpp:
#ifdef _WIN32
# define API __declspec(dllexport)
#else
# define API
#endif
extern "C" API int add(int a, int b) {
return a+b;
}
Python:
from ctypes import *
dll = CDLL('fun')
result = dll.add(10,1)
print('Addition value:',result)
输出:
Addition value: 11
我想知道是否有办法在 python 代码中使用 c++ 函数
例如,在进行研究后,我确实找到了使用 .dll 文件的解决方案。 但是找不到函数 我的代码:
fun.cpp:
#include <iostream>
extern int add(int a, int b) {
return a+b;
}
int main()
{
std::cout << "Hello World! from C++" << std::endl;
return 0;
}
使用 cmd 编译它:
g++ fun.cpp -o fun.dll
使用 Python 调用函数,ctypes:
from ctypes import *
import os
mydll = cdll.LoadLibrary("C:/Users/User/Desktop/ctypes/fun.dll")
result= mydll.add(10,1)
print("Addition value:-"+result)
但是我遇到了这个错误:
Traceback (most recent call last): File "c:\Users\User.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\ptvsd_launcher.py", line 43, in main(ptvsdArgs) File "c:\Users\User.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\lib\python\old_ptvsd\ptvsd__main__.py", line 432, in main run() File "c:\Users\User.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\lib\python\old_ptvsd\ptvsd__main__.py", line 316, in run_file runpy.run_path(target, run_name='main') File "C:\Python36\lib\runpy.py", line 263, in run_path pkg_name=pkg_name, script_name=fname) File "C:\Python36\lib\runpy.py", line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "C:\Python36\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "c:\Users\User\Desktop\ctypes\test.py", line 5, in result= mydll.add(10,1) File "C:\Python36\lib\ctypes__init__.py", line 361, in getattr func = self.getitem(name) File "C:\Python36\lib\ctypes__init__.py", line 366, in getitem func = self._FuncPtr((name_or_ordinal, self)) AttributeError: function 'add' not found
我认为您应该检查 Python 的 LoadLibrary
是否正确调用了 "fun.dll" 的 main()
函数。 loading的时候应该会寻找DllMain()
函数,然后需要获取函数地址才能调用函数。
C++ 破坏了导出的名称。这是一个应该在 Windows 和 Linux 上编译的示例。 __declspec(dllexport)
需要在 Windows 上导出函数,但 Linux 不需要。 extern "C"
需要使用 C 约定导出函数名称,而不是名称损坏的 C++ 约定。 C++ 中需要名称修饰来指示函数参数和 return 类型,因为 C++ 可以有多个具有相同名称但采用不同参数的函数。 C 约定不支持同名的多个函数。
fun.cpp:
#ifdef _WIN32
# define API __declspec(dllexport)
#else
# define API
#endif
extern "C" API int add(int a, int b) {
return a+b;
}
Python:
from ctypes import *
dll = CDLL('fun')
result = dll.add(10,1)
print('Addition value:',result)
输出:
Addition value: 11