Escribí sobre cómo llamar a Python desde C en un artículo anterior, ahora hablemos sobre cómo hacer lo contrario y llamar a C / C ++ desde Python3 . Una vez que comencé a escribir sobre esto, revelaremos todo el tema hasta el final. Además, aquí tampoco hay nada complicado.
C
Aquí todo es simple, Python puede llamar a funciones C sin ningún problema.
prueba.c:
#include "test.h" int a = 5; double b = 5.12345; char c = 'X'; int func_ret_int(int val) { printf("get func_ret_int: %d\n", val); return val; } double func_ret_double(double val) { printf("get func_ret_double: %f\n", val); return val; } char * func_ret_str(char *val) { printf("get func_ret_str: %s\n", val); return val; } char func_many_args(int val1, double val2, char val3, short val4) { printf("get func_many_args: int - %d, double - %f, char - %c, short - %d\n", val1, val2, val3, val4); return val3; }
prueba.h:
#ifndef _TEST_H_ #define _TEST_H_ #ifdef __cplusplus extern "C" { #endif #include <stdio.h> #include <string.h> #include <unistd.h> int func_ret_int(int val); double func_ret_double(double val); char *func_ret_str(char *val); char func_many_args(int val1, double val2, char val3, short val4) #ifdef __cplusplus } #endif #endif /* _TEST_H_ */
Cómo compilar:
gcc -fPIC -shared -o libtest.so test.c
La fuente se compila en una biblioteca dinámica y está lista para la batalla.
Pasamos a Python. El ejemplo muestra cómo pasar argumentos a una función, obtener el resultado de una función y cómo obtener y cambiar los valores de las variables globales.
main.py:
Todos los tipos de datos posibles y sus designaciones se pueden encontrar en la documentación de Python.
Trabajar con estructuras
C - declaración de estructura en test.h:
typedef struct test_st_s test_st_t; struct test_st_s { int val1; double val2; char val3; };
Función para trabajar con nuestra estructura:
test_st_t * func_ret_struct(test_st_t *test_st) { if (test_st) { printf("C get test_st: val1 - %d, val2 - %f, val3 - %c\n", test_st->val1, test_st->val2, test_st->val3); } return test_st; }
Python:
import sys import struct
C ++
Aquí es un poco más complicado porque Los ctypes solo pueden funcionar con funciones C. Esto no es un problema para nosotros, solo C enlazará el código C ++.
Métodos de clase C ++ y enlaces C:
#include "test.hpp" std::string test::ret_str(std::string val) { std::cout << "get ret_str: " << val << std::endl; return val; } int test::ret_int(int val) { std::cout << "get ret_int: " << val << std::endl; return val; } double test::ret_double(double val) { std::cout << "get ret_double: " << val << std::endl; return val; } /* * C C++ */ // test, . test *test_new() { return new test(); } // test. void test_del(test *test) { delete test; } /* * . */ // ret_str char *test_ret_str(test *test, char *val) { // char * std::string std::string str = test->ret_str(std::string(val)); // std::string char * char *ret = new char[str.length() + 1]; strcpy(ret, str.c_str()); return ret; } // ret_int int test_ret_int(test *test, int val) { return test->ret_int(val); } // ret_double double test_ret_double(test *test, double val) { return test->ret_double(val); } /* * . */ // a int test_get_a(test *test) { return test->a; } // b double test_get_b(test *test) { return test->b; } // c char test_get_c(test *test) { return test->c; }
Pero hay una advertencia, el enlace debe declararse como externo C. Para que el compilador ++ no sobrecargue los nombres de las funciones de enlace. Si lo hace, entonces no podremos trabajar con nuestras funciones a través de ctypes.
test.hpp:
#include <iostream> #include <string.h> class test { public: int a = 5; double b = 5.12345; char c = 'X'; std::string ret_str(std::string val); int ret_int(int val); double ret_double(double val); }; #ifdef __cplusplus extern "C" { #endif test *test_new(); void test_del(test *test); char *test_ret_str(test *test, char *val); int test_ret_int(test *test, int val); double test_ret_double(test *test, double val); int test_get_a(test *test); double test_get_b(test *test); char test_get_c(test *test); #ifdef __cplusplus } #endif
Cómo compilar:
g ++ -fPIC -compartido -o libtestpp.so test.cpp
Python es igual de fácil.
Pros y contras de los tipos
Pros :
- Puede conectar cualquier biblioteca C ya compilada
Contras :
- En Python, debe describir qué funciones de C devuelven y aceptan como argumentos.
El código intentó comentar claramente, escribir menos aquí)
Espero que te sea útil.
Gratitud
DollaR84 por su ayuda.
Palich239 por los errores encontrados.
Referencias