我在上一篇文章中写过关于如何从C调用Python的知识 ,现在我们将讨论如何相反地从Python3调用C / C ++ 。 一旦我开始写这本书,我们将把整个话题展示到最后。 而且,这里也没有什么复杂的。
ç
这里的一切都很简单,Python可以毫无问题地调用C函数。
test.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; }
test.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_ */
如何编译:
gcc -fPIC -shared -o libtest.so test.c
源代码编译成一个动态库,可以战斗了。
我们传递给Python。 该示例说明了如何将参数传递给函数,获取函数的结果以及如何获取和更改全局变量的值。
main.py:
所有可能的数据类型及其名称都可以在Python 文档中找到。
处理结构
C-test.h中的结构声明:
typedef struct test_st_s test_st_t; struct test_st_s { int val1; double val2; char val3; };
用于处理我们的结构的功能:
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 ++
这里有点复杂,因为 ctypes只能与C函数一起使用。 这对我们来说不是问题,只是C将绑定C ++代码。
C ++类方法和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; }
但是有一个警告,绑定必须声明为externC。 这样,++编译器就不会重载绑定函数的名称。 如果他这样做,那么我们将无法通过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
如何编译:
g ++ -fPIC -shared -o libtestpp.so test.cpp
Python一样简单。
ctypes的优缺点
优点 :
缺点 :
- 在Python中,您需要描述C函数返回并接受哪些参数。
该代码试图清楚地注释,在此处少写)
我希望它会有用。
感激之情
DollaR84的帮助。
Palich239发现的错误。
参考文献