动态库开发环境 eclipse + cdt
Python开发环境python3.6 +idea
注意,python调用C模块函数参数-类型转换
1. C部分
/*
* demo.h
*
* Created on: 2018年2月13日
* Author: tonydon
*/
#ifndef DEMO_H_
#define DEMO_H_
void SayHello(char *str);
#endif /* DEMO_H_ */
/*
* demo.c
*
* Created on: 2018年2月13日
* Author: tonydon
*/
#include <stdio.h>
#include "demo.h"
void SayHello(char* str){
printf("demo.SayHello() invoke : %s", str);
}
使用eclipse build shared 得到 libdemo.dll
2. Python 部分
# clib_invoke.py
from ctypes import *
libdemo = cdll.LoadLibrary('C:/workspaces/eclipseWorks/cpp1/demo/Release/libdemo.dll')
str = "test, 哈哈"
libdemo.SayHello(c_char_p(str.encode("utf-8")))
run结果