Boost Python C++ 学习 笔记

教程
https://www.boost.org/doc/libs/1_76_0/libs/python/doc/html/tutorial/tutorial/functions.html

  1. 编写greet.cpp(hello_ext指代module hello_ext 可修改) 文件
  2. 编写 Makefile文件
    hello_ext.so : greet.o
    g++ -shared greet.o -o hello_ext.so -lpython3.8 -lboost_python38
    greet.o : 
    g++ -c -fPIC -I/usr/include/python3.8 greet.cpp -o greet.o

    测试如下(example 2):

oem@nike0good:~/Documents/test_for_boost$ python3
Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello_ext
>>> print hello_ext.greet()
  File "<stdin>", line 1
    print hello_ext.greet()
          ^
SyntaxError: invalid syntax
>>> print(hello_ext.greet())
hello, world
>>> quit()
oem@nike0good:~/Documents/test_for_boost$ python3
Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
>>> planet = hello.World()
>>> planet.set("fdsj")
>>> planet.greet()
'fdsj'
>>> quit()