Wednesday, March 1, 2017

Python: 調用C++的程序

hello.h:

#include <iostream>
extern "C" {
  void display(char *msg);
}

hello.cpp:

#include "hello.h"
void display(char* msg) {
        std::cerr<<msg<<std::endl;
}

Compile:

gcc -shared hello.cpp -fPIC -o hello.so

Python:

#!/usr/bin/python
#-*- encoding:utf8 -*-
from ctypes import *
test = cdll.LoadLibrary('./hello.so')
test.display('Hello, world')


No comments: