Python调用C++
发布日期:2021-05-14 09:33:29 浏览次数:22 分类:精选文章

本文共 4385 字,大约阅读时间需要 14 分钟。

Python ������ C++ ���������������������������������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������ Python ��� ctypes ��������� C++ ������������������DLL������������������������������������������������������������������


������������������

��������� Python ��� C++ ������������������������������������������������������olang������������������������������������Python ������������������ C ������������������������������ C++ ��������������������������� extern "C" ���.configure ��� diploma���

������������

������������������������ C++ ������������������.��������������� Python ���������������

// ��������������������� Python ���������class TestLib {public:    void display();    void display(int a);};// ������������������  void TestLib::display() {    std::cout << "First display" << std::endl;}// ���������������������  g++ -o libpycallclass.so -shared -fPIC ./libpycallclass.cpp
import ctypes# ���������������  lib = ctypes.cdll.LoadLibrary("./libpycallclass.so")# ������������������  lib.display()  lib.display(100)

������������������

������ Python ��� C++ ��������������������������������������������� ctypes ������������������������������������������������������������������������������

C ������ Python ������ ������
bool bool 1 ������
char str ������������������������
wchar_t str ������������������
int int ������������������
unsigned char int ������������������������
short int ������������������������
unsigned short int ������������������������
float float ������������������
double float ������������������������������
char * bytes ������������ None
wchar_t * str ������������ None
void * int ������������ None

���������������������

������ Python ������������������������������������������������������������������ C ��������������������������� ctypes ������������������������������

zoon = ctypes.create_string_buffer('49Q')

���������������������

ctypes ���������������������������������������������������������

from ctypes import *libc = cdll.LoadLibrary('libc.so.6')class Bottles(object):    def __init__(self, number):        self._as_parameter_ = number  # ��������������������������������� unicode ���������bottles = Bottles(42)libc.printf('%d bottles of beer\n', bottles)

���������������������������

  • ��������������������������������������� Python ��� C++ ���������������������������������������������������������������������������������������������������

    import ctypesfrom ctypes import *libc = cdll.LoadLibrary('libc.so.6')# ������ printf ���������������������������������libc.printf.argtypes = [c_char_p, c_char_p, c_int, c_double]try:    libc.printf('String is "%s", Int is %d, Double is %f\n', 'Hi', 10, 2.2)    libc.printf('%s, %d, %f\n', 'X', 2, 3)except ArgumentError:    print(f"ERROR: {str(e)}")
  • ���������������������������

    ������������������������ int ������������������������������������������

    from ctypes import *libc = cdll.LoadLibrary('libc.so.6')# ������ strchr ��������������� char *libc.strchr.restype = c_char_p# ������������result = libc.strchr('abcdefghij', b'd')print(result)
  • ���������������������

    ������������������������������������������������������������������������������������������������������

    from ctypes import *libc = cdll.LoadLibrary('libc.so.6')# ��������������������� RetroGame ������������class RetroGame(Structure):    _fields_ = [        ('pixels', c_int),        ('bytes', c_byte * 32)    ]retro_game = RetroGame(1024, b'\x00\x00\x00\x00')libc.process Retro_game
  • ������������������

    ������������������������������������������������

    • ������������������������������������������������
    • ������������������ C++ ������������������������������
    • ��������������� `POINTER` ���������������������������
    • ������������������������������������
    • ��������������� `struct` ���������������������������������

  • ������

    ��������������������� Python ������ C++ ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:罗技k380连接双系统(win10和ubuntu18)
    下一篇:C++全局参数重复定义

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2025年05月03日 21时27分12秒