
C++文件
test.h
#pragma once #include <stdio.h>
extern "C" char* show(char* input);
test.cpp
#include "test.h"
extern "C" char* show(char* input) { return input;
}
编译为so库
调用
import ctypes
myso = ctypes.cdll.LoadLibrary('./libtest.so') myso.show.restype = ctypes.c_char_p result = myso.show('good') print(result)
总结:python传参是不需要类型转换,只需要传入string类型,返回值需要指定为ctypes.c_char_p