<分区>
fib.cpp
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
using namespace std;
extern "C" const char *hello (char* b){
static string s = "hello ";
s = s + b;
return s.c_str();
}
wrap.py
import ctypes
_libfib = ctypes.CDLL('./fib.so');
def ctypes_hello(a):
_libfib.hello.restype = ctypes.c_char_p;
return _libfib.hello(ctypes.c_char_p(a));
Generate .so files
g++ -std=c++11 -shared -c -fPIC fib.cpp -o fib.o
g++ -std=c++11 -shared -Wl,-soname,fib.so -o fib.so fib.o
从命令行运行 wrap.py
from wrap import *
ctypes_hello("world")
Its working perfectly with python 2. I am getting Error bytes or integer address expected instead of str instance when I switch to Python 3