在python中调用C++代码(使用Swig)
可以运行的例子
https://gist.github.com/mattbierbaum/1193397
调用示例
>>> import kitty
>>> k = kitty.kitty()
Constructor
>>> k.speak()
I'm a cat.
改动Makefile:
# standard compile options for the c++ executable
FLAGS = -fPIC
# the python interface through swig
PYTHONI = -I/usr/include/python3.8/
PYTHONL = -Xlinker -export-dynamic
# default super-target
all:
g++ -fPIC -c kitty.cc -o kitty.o
swig -c++ -python -o kitty_wrap.cxx kitty.i
g++ $(FLAGS) $(PYTHONI) -c kitty_wrap.cxx -o kitty_wrap.o
g++ $(PYTHONL) $(LIBFLAGS) -shared kitty.o kitty_wrap.o -o _kitty.so
解决报错1
解决方案:
sudo apt-get install python-dev # for python2.x installs
sudo apt-get install python3-dev # for python3.x installs
https://stackoverflow.com/questions/21530577/fatal-error-python-h-no-such-file-or-directory
记得要下载python3的版本!
一开始下载了第一个python2的,但是发现没有办法在python3中import包
https://stackoverflow.com/questions/51198679/python-module-and-multiple-python-dev-installations