Created
August 26, 2018 08:07
-
-
Save lagagain/63c38cb960285d5dc06268a8c129b1ac to your computer and use it in GitHub Desktop.
[pratice] Python3.5 call C++ function with extern "C"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "hello.h" | |
| #include<Python.h> | |
| using namespace std; | |
| extern "C"{ | |
| static PyObject *_hello(PyObject *self, PyObject *args){ | |
| hello(); | |
| return self; | |
| } | |
| static PyMethodDef HelloMethods[] = { | |
| {"hello", _hello, METH_VARARGS,""}, | |
| {NULL, NULL, 0, NULL} | |
| }; | |
| static struct PyModuleDef hellomodule = { | |
| PyModuleDef_HEAD_INIT, | |
| "hello", /* name of module */ | |
| NULL, /* module documentation, may be NULL */ | |
| -1, /* size of per-interpreter state of the module, | |
| or -1 if the module keeps state in global variables. */ | |
| HelloMethods | |
| }; | |
| PyMODINIT_FUNC PyInit_hello(void){ | |
| return PyModule_Create(&hellomodule); | |
| } | |
| } | |
| void hello(){ | |
| cout<<"Hello"<<endl; | |
| return ; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<Python.h> | |
| #include<iostream> | |
| #include<cstdlib> | |
| void hello(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import hello | |
| hello.hello() | |
| print("END") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<iostream> | |
| #include<cstdlib> | |
| #include "./hello.h" | |
| using namespace std; | |
| int main(){ | |
| hello(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CPP = c++ | |
| C = gcc | |
| DELETE = rm -f | |
| all: | |
| ${CPP} -fPIC -shared hello.cpp -o hello.so -I/usr/include/python3.5/ -L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu/ -lpython3.5 | |
| exec_hello: | |
| ${CPP} main.cpp hello.cpp -o hello -I/usr/include/python3.5/ -L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu/ -lpython3.5 | |
| clean: | |
| ${DELETE} hello hello.so |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use
maketo build hello.so, then execpython3 hello.pyor use
make exec_helloto build exec file, it test c++ code is right.finally, use
make cleanto delete .so file and exec file.