├── .gitignore ├── Examples ├── hello_ext │ ├── example.py │ ├── README.md │ ├── hello_ext.cpp │ └── makefile ├── class_virtual_nopure │ ├── README.md │ ├── example.py │ ├── class_virtual_nopure.cpp │ └── makefile ├── basic_interface │ ├── README.md │ ├── basic_interface.cpp │ ├── test.py │ └── makefile ├── class_virtual │ ├── example.py │ ├── README.md │ ├── class_virtual.cpp │ └── makefile ├── exception_translation │ ├── example.py │ ├── README.md │ ├── makefile │ └── exception_translation.cpp ├── class_member │ ├── class_member.cpp │ ├── README.md │ └── makefile ├── class_property │ ├── class_property.cpp │ ├── README.md │ └── makefile ├── enums │ ├── enums.cpp │ ├── README.md │ └── makefile ├── reference_count │ ├── reference_count.cpp │ ├── README.md │ └── makefile ├── embedding │ ├── README.md │ ├── embdding.cpp │ └── makefile ├── extracting_c++_objects │ ├── README.md │ ├── extracting.cpp │ └── makefile ├── call_policies │ ├── README.md │ ├── call_policies.cpp │ └── makefile ├── derived_object_types │ ├── README.md │ ├── derived_object_types.cpp │ └── makefile ├── class_hello │ ├── class_hello.cpp │ ├── README.md │ └── makefile ├── special_operators │ ├── README.md │ ├── special_operators.cpp │ └── makefile ├── iterators │ ├── README.md │ ├── iterators.cpp │ └── makefile ├── inheritance │ ├── inheritance.cpp │ ├── README.md │ └── makefile ├── operators │ ├── README.md │ ├── operators.cpp │ └── makefile └── overloading │ ├── README.md │ ├── overloading.cpp │ └── makefile ├── .gitattributes └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | *.o 3 | *.so 4 | *.pyd 5 | .* 6 | -------------------------------------------------------------------------------- /Examples/hello_ext/example.py: -------------------------------------------------------------------------------- 1 | import hello_ext 2 | print(hello_ext.greet()) 3 | 4 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.css linguist-language=C++ 2 | *.less linguist-language=C++ 3 | *.js linguist-language=C++ 4 | *.html linguist-language=C++ 5 | *.md linguist-language=C++ 6 | *.png linguist-language=C++ 7 | -------------------------------------------------------------------------------- /Examples/hello_ext/README.md: -------------------------------------------------------------------------------- 1 | # hello_ext 2 | 3 | ###### python3 hello_ext 4 | 5 | cd Boost-Python-Examples/Examples/hello_ext 6 | make 7 | python3 example.py 8 | 9 | hello, world 10 | -------------------------------------------------------------------------------- /Examples/hello_ext/hello_ext.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char const* greet() 4 | { 5 | return "hello, world"; 6 | } 7 | 8 | BOOST_PYTHON_MODULE(hello_ext) 9 | { 10 | using namespace boost::python; 11 | def("greet", greet); 12 | } 13 | -------------------------------------------------------------------------------- /Examples/class_virtual_nopure/README.md: -------------------------------------------------------------------------------- 1 | # class_virtual_nopure 2 | 3 | ###### python3 class_virtual_nopure 4 | 5 | cd Boost-Python-Examples/Examples/class_virtual_nopure 6 | make 7 | python3 example.py 8 | 9 | x 10 | 0 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Examples/basic_interface/README.md: -------------------------------------------------------------------------------- 1 | # basic_interface 2 | 3 | ###### python3 basic_interface 4 | 5 | cd Boost-Python-Examples/Examples/basic_interface 6 | make 7 | python3 8 | 9 | python3 test.py 10 | ['I', ' ', 'a', 'b', 'a', 'r', 'n', 'd', 's', 'o', 'm', 'e'] 11 | I abarndsome 12 | -------------------------------------------------------------------------------- /Examples/class_virtual/example.py: -------------------------------------------------------------------------------- 1 | import class_virtual 2 | 3 | class DerivedA(class_virtual.Base): 4 | pass 5 | 6 | class DerivedB(class_virtual.Base): 7 | def f(self): 8 | return "x" 9 | 10 | if __name__ == "__main__": 11 | da = DerivedA() 12 | db = DerivedB() 13 | print(db.f()) 14 | da.f() 15 | -------------------------------------------------------------------------------- /Examples/class_virtual_nopure/example.py: -------------------------------------------------------------------------------- 1 | import class_virtual_nopure 2 | 3 | class DerivedA(class_virtual_nopure.Base): 4 | pass 5 | 6 | class DerivedB(class_virtual_nopure.Base): 7 | def f(self): 8 | return "x" 9 | 10 | if __name__ == "__main__": 11 | da = DerivedA() 12 | db = DerivedB() 13 | print(db.f()) 14 | print(da.f()) 15 | -------------------------------------------------------------------------------- /Examples/exception_translation/example.py: -------------------------------------------------------------------------------- 1 | from exception_translation import * 2 | try: 3 | func_from_cpp(False) 4 | print("reach here") 5 | func_from_cpp(True) 6 | print("Not going to reach here") 7 | except MyCPPException as e: 8 | print("Exception occure") 9 | print("e.message: ", e.message) 10 | print("e.extraData", e.extraData) 11 | 12 | -------------------------------------------------------------------------------- /Examples/class_virtual/README.md: -------------------------------------------------------------------------------- 1 | # class_virtual 2 | 3 | ###### python3 class_virtual 4 | 5 | cd Boost-Python-Examples/Examples/class_virtual 6 | make 7 | python3 example.py 8 | 9 | Default constructor of Base 10 | Default constructor of Base 11 | x 12 | Traceback (most recent call last): 13 | File "example.py", line 14, in 14 | da.f() 15 | RuntimeError: Pure virtual function called 16 | 17 | -------------------------------------------------------------------------------- /Examples/class_member/class_member.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Var 4 | { 5 | Var(std::string name) : name(name), value() {} 6 | std::string const name; 7 | float value; 8 | }; 9 | 10 | #include 11 | using namespace boost::python; 12 | 13 | BOOST_PYTHON_MODULE(class_member) 14 | { 15 | class_("Var", init()) 16 | .def_readonly("name", &Var::name) 17 | .def_readwrite("value", &Var::value); 18 | } 19 | -------------------------------------------------------------------------------- /Examples/class_property/class_property.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Num 4 | { 5 | float get() const { return value; } 6 | void set(float val) { value = val; } 7 | float value = 10.0; 8 | }; 9 | 10 | #include 11 | using namespace boost::python; 12 | 13 | BOOST_PYTHON_MODULE(class_property) 14 | { 15 | class_("Num") 16 | .add_property("rovalue", &Num::get) 17 | .add_property("value", &Num::get, &Num::set); 18 | } 19 | -------------------------------------------------------------------------------- /Examples/basic_interface/basic_interface.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace boost::python; 4 | 5 | object f(object x, object y) 6 | { 7 | if (y == "foo") 8 | x.slice(3, 7) = "bar"; 9 | else 10 | x.attr("items") += y(3, x); 11 | return x; 12 | } 13 | 14 | object getfunc() 15 | { 16 | return object(f); 17 | } 18 | 19 | BOOST_PYTHON_MODULE(basic_interface) 20 | { 21 | def("f", f); 22 | def("getfunc", getfunc); 23 | } 24 | -------------------------------------------------------------------------------- /Examples/exception_translation/README.md: -------------------------------------------------------------------------------- 1 | # exception_translation 2 | 3 | ###### python3 exception_translation 4 | 5 | make 6 | python3 example.py 7 | 8 | Call from func_from_cpp 9 | reach here 10 | Call from func_from_cpp 11 | translating exception 12 | destructor of MyCPPException 13 | destructor of MyCPPException 14 | Exception occure 15 | e.message: Throwing an exception as requested. 16 | e.extraData This is the extra data. 17 | 18 | -------------------------------------------------------------------------------- /Examples/enums/enums.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace boost::python; 4 | 5 | enum choice { red, blue }; 6 | 7 | struct X 8 | { 9 | enum nested { red, blue }; 10 | }; 11 | 12 | BOOST_PYTHON_MODULE(enums) 13 | { 14 | enum_("choice") 15 | .value("red", red) 16 | .value("blue", blue) 17 | ; 18 | 19 | scope in_X = class_("X"); 20 | enum_("nested") 21 | .value("red", X::red) 22 | .value("blue", X::blue); 23 | } 24 | -------------------------------------------------------------------------------- /Examples/reference_count/reference_count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace boost::python; 3 | 4 | struct py_pointer 5 | { 6 | py_pointer(PyObject *pyobj): p(handle<>(pyobj)) { } 7 | object p; 8 | }; 9 | 10 | struct py_object 11 | { 12 | py_object(PyObject *pyobj): p(handle<>(borrowed(pyobj))) { } 13 | object p; 14 | }; 15 | 16 | BOOST_PYTHON_MODULE(reference_count) 17 | { 18 | class_("py_pointer", init()); 19 | class_("py_object", init()); 20 | } 21 | -------------------------------------------------------------------------------- /Examples/embedding/README.md: -------------------------------------------------------------------------------- 1 | # embdding 2 | 3 | ###### python3 embdding 4 | 5 | make 6 | ./embdding 7 | 8 | I am called from cpp 9 | sys.version: 3.5.2 (default, Sep 14 2017, 22:51:06) 10 | [GCC 5.4.0 20160609] 11 | In cpp, method 1, five_squared: 25 12 | In cpp, method 2, five_squared: 25 13 | catch ZeroDivisionError in cpp 14 | Traceback (most recent call last): 15 | File "", line 1, in 16 | ZeroDivisionError: division by zero 17 | In python, going to sleep for 20 seconds 18 | 19 | -------------------------------------------------------------------------------- /Examples/extracting_c++_objects/README.md: -------------------------------------------------------------------------------- 1 | # extracting_c++_objects 2 | 3 | ###### python3 extracting_c++_objects 4 | 5 | cd Boost-Python-Examples/Examples/extracting_c++_objects 6 | make 7 | python3 8 | 9 | Python 3.5.2 (default, Sep 14 2017, 22:51:06) 10 | [GCC 5.4.0 20160609] on linux 11 | Type "help", "copyright", "credits" or "license" for more information. 12 | >>> from extracting import * 13 | length: 3 angle: 4 14 | >>> vec345 = Vec2(4.0, 5.0) 15 | length: 4 angle: 5 16 | >>> test(vec345) 17 | l: 4 v.length: 4 18 | -------------------------------------------------------------------------------- /Examples/enums/README.md: -------------------------------------------------------------------------------- 1 | # enums 2 | 3 | ###### python3 enums 4 | 5 | Python 3.5.2 (default, Sep 14 2017, 22:51:06) 6 | [GCC 5.4.0 20160609] on linux 7 | Type "help", "copyright", "credits" or "license" for more information. 8 | >>> import enums 9 | >>> c = enums.choice(enums.choice.red) 10 | >>> c 11 | enums.choice(0) 12 | >>> c = enums.choice(enums.choice.blue) 13 | >>> c 14 | enums.choice(1) 15 | 16 | >>> n = enums.X.nested(enums.X.nested.red) 17 | >>> n = enums.X.nested(enums.X.nested.blue) 18 | >>> n 19 | enums.nested(1) 20 | >>> 21 | -------------------------------------------------------------------------------- /Examples/call_policies/README.md: -------------------------------------------------------------------------------- 1 | # call_policies 2 | 3 | ###### python3 call_policies 4 | 5 | cd Boost-Python-Examples/Examples/call_policies 6 | make 7 | python3 8 | 9 | Python 3.5.2 (default, Nov 17 2016, 17:05:23) 10 | [GCC 5.4.0 20160609] on linux 11 | Type "help", "copyright", "credits" or "license" for more information. 12 | >>> from call_policies import * 13 | >>> y = Y() 14 | >>> z = Z() 15 | >>> r = f(y, z) 16 | In function f 17 | >>> y.x.get() 18 | 3 19 | >>> r.get() 20 | 3 21 | >>> y.x.set(2) 22 | >>> r.get() 23 | 2 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Examples/basic_interface/test.py: -------------------------------------------------------------------------------- 1 | import basic_interface 2 | 3 | class X(list): 4 | def __init__(self, *args, **kwargs): 5 | super(X, self).__init__(*args, **kwargs) 6 | 7 | def __setattr__(self, name, value): 8 | self.__dict__[name] = value 9 | 10 | def __getattr__(self, item): 11 | return self.__dict__[item] 12 | 13 | 14 | def func_y(a, b): 15 | return a + len(b) 16 | 17 | if __name__ == "__main__": 18 | item1 = X("I am handsome") 19 | item1.items = 1 20 | item2 = "foo" 21 | print(basic_interface.f(item1, item2)) 22 | print("".join(basic_interface.getfunc()(item1, func_y))) 23 | 24 | -------------------------------------------------------------------------------- /Examples/class_virtual_nopure/class_virtual_nopure.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace boost::python; 4 | 5 | struct Base 6 | { 7 | virtual ~Base() {} 8 | virtual int f() { return 0; } 9 | }; 10 | 11 | struct BaseWrap : Base, wrapper 12 | { 13 | int f() 14 | { 15 | if (override f = this->get_override("f")) 16 | return f(); 17 | return Base::f(); 18 | } 19 | int default_f() { return this->Base::f(); } 20 | }; 21 | 22 | BOOST_PYTHON_MODULE(class_virtual_nopure) 23 | { 24 | class_("Base") 25 | .def("f", &Base::f, &BaseWrap::default_f); 26 | } 27 | -------------------------------------------------------------------------------- /Examples/derived_object_types/README.md: -------------------------------------------------------------------------------- 1 | # derived_object_types 2 | 3 | ###### python3 derived_object_types 4 | 5 | cd Boost-Python-Examples/Examples/derived_object_types 6 | make 7 | python3 example.py 8 | 9 | Python 3.5.2 (default, Aug 18 2017, 17:48:00) 10 | [GCC 5.4.0 20160609] on linux 11 | Type "help", "copyright", "credits" or "license" for more information. 12 | >>> from derived_object_types import * 13 | >>> t = test() 14 | >>> print(t.to_list(1, "ss")) 15 | [1, 'ss'] 16 | >>> print(t.to_tuple()) 17 | (123, 'D', 'Hello, World', 0.0) 18 | >>> print(t.to_str("hello!")) 19 | HELLO! is bigger than hello! 20 | -------------------------------------------------------------------------------- /Examples/class_hello/class_hello.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct World 5 | { 6 | World() { std::cout << "default constructor" << std::endl; } 7 | World(std::string msg): msg(msg) { std::cout << "constructor of struct World" << std::endl; } 8 | void set(std::string msg) { this->msg = msg; } 9 | std::string greet() { return msg; } 10 | std::string msg; 11 | }; 12 | 13 | #include 14 | using namespace boost::python; 15 | 16 | BOOST_PYTHON_MODULE(class_hello) 17 | { 18 | class_("World") 19 | .def("greet", &World::greet) 20 | .def("set", &World::set) 21 | .def(init()); 22 | } 23 | -------------------------------------------------------------------------------- /Examples/special_operators/README.md: -------------------------------------------------------------------------------- 1 | # special_operators 2 | 3 | ###### python3 special_operators 4 | 5 | cd Boost-Python-Examples/Examples/special_operators 6 | make 7 | python3 8 | 9 | Python 3.5.2 (default, Nov 17 2016, 17:05:23) 10 | [GCC 5.4.0 20160609] on linux 11 | Type "help", "copyright", "credits" or "license" for more information. 12 | >>> import special_operators 13 | >>> r = special_operators.Rational(3, 4) 14 | Default constructor of Rational 15 | >>> print(r) 16 | I am Rational 17 | >>> r ** r 18 | 0.8059274488676564 19 | >>> abs(r) 20 | 21 | >>> abs(r) 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Examples/class_hello/README.md: -------------------------------------------------------------------------------- 1 | # class_hello 2 | 3 | ###### python3 class_hello 4 | 5 | cd Boost-Python-Examples/Examples/class_hello 6 | make 7 | python3 8 | 9 | Python 3.5.2 (default, Nov 17 2016, 17:05:23) 10 | [GCC 5.4.0 20160609] on linux 11 | Type "help", "copyright", "credits" or "license" for more information. 12 | >>> import class_hello 13 | >>> world = class_hello.World() 14 | default constructor 15 | >>> world.greet() 16 | '' 17 | >>> world = class_hello.World("I am world") 18 | constructor of struct World 19 | >>> world.greet() 20 | 'I am world' 21 | >>> world.set("Good") 22 | >>> world.greet() 23 | 'Good' 24 | >>> quit() 25 | 26 | -------------------------------------------------------------------------------- /Examples/class_property/README.md: -------------------------------------------------------------------------------- 1 | # class_property 2 | 3 | ###### python3 class_property 4 | 5 | cd Boost-Python-Examples/Examples/class_property 6 | make 7 | python3 8 | 9 | Python 3.5.2 (default, Nov 17 2016, 17:05:23) 10 | [GCC 5.4.0 20160609] on linux 11 | Type "help", "copyright", "credits" or "license" for more information. 12 | >>> import class_property 13 | >>> num = class_property.Num() 14 | >>> num.rovalue 15 | 10.0 16 | >>> num.value 17 | 10.0 18 | >>> num.value = 3 19 | >>> num.value 20 | 3.0 21 | >>> num.rovalue = 3 22 | Traceback (most recent call last): 23 | File "", line 1, in 24 | AttributeError: can't set attribute 25 | -------------------------------------------------------------------------------- /Examples/class_member/README.md: -------------------------------------------------------------------------------- 1 | # class_member 2 | 3 | ###### python3 class_member 4 | 5 | cd Boost-Python-Examples/Examples/class_member 6 | make 7 | python3 8 | 9 | Python 3.5.2 (default, Nov 17 2016, 17:05:23) 10 | [GCC 5.4.0 20160609] on linux 11 | Type "help", "copyright", "credits" or "license" for more information. 12 | >>> import class_member 13 | >>> var = class_member.Var("I am Var") 14 | >>> var.name 15 | 'I am Var' 16 | >>> var.value 17 | 0.0 18 | >>> var.value = 3 19 | >>> var.value 20 | 3.0 21 | >>> var.name = "!" 22 | Traceback (most recent call last): 23 | File "", line 1, in 24 | AttributeError: can't set attribute 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Examples/iterators/README.md: -------------------------------------------------------------------------------- 1 | # iterators 2 | 3 | ###### python3 iterators 4 | 5 | make 6 | python3 7 | 8 | Python 3.5.2 (default, Sep 14 2017, 22:51:06) 9 | [GCC 5.4.0 20160609] on linux 10 | Type "help", "copyright", "credits" or "license" for more information. 11 | >>> from iterators import * 12 | >>> f = Field() 13 | >>> for i in f.pions: 14 | ... print(i) 15 | ... 16 | 1 17 | 2 18 | 3 19 | 4 20 | 5 21 | >>> for i in f.bogons: 22 | ... print(i) 23 | ... 24 | 1 25 | 2 26 | 3 27 | 4 28 | 5 29 | 30 | >>> x = list_int() 31 | >>> x 32 | 33 | >>> x.assign([1,2,3,4,6]) 34 | >>> 35 | 36 | -------------------------------------------------------------------------------- /Examples/derived_object_types/derived_object_types.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace boost::python; 4 | 5 | struct test 6 | { 7 | list to_list(object x, object y) 8 | { 9 | list lst; 10 | lst.append(x); 11 | lst.append(y); 12 | return lst; 13 | } 14 | tuple to_tuple() 15 | { 16 | return make_tuple(123, 'D', "Hello, World", 0.0); 17 | } 18 | object to_str(str name) 19 | { 20 | str name2 = name.upper(); 21 | object msg = "%s is bigger than %s" % make_tuple(name2, name); 22 | return msg; 23 | } 24 | }; 25 | 26 | BOOST_PYTHON_MODULE(derived_object_types) 27 | { 28 | class_("test") 29 | .def("to_list", &test::to_list) 30 | .def("to_tuple", &test::to_tuple) 31 | .def("to_str", &test::to_str); 32 | } 33 | -------------------------------------------------------------------------------- /Examples/reference_count/README.md: -------------------------------------------------------------------------------- 1 | # enums 2 | 3 | ###### python3 enums 4 | 5 | Python 3.5.2 (default, Sep 14 2017, 22:51:06) 6 | [GCC 5.4.0 20160609] on linux 7 | Type "help", "copyright", "credits" or "license" for more information. 8 | >>> from reference_count import * 9 | >>> import sys 10 | >>> a = [] 11 | >>> sys.getrefcount(a) 12 | 2 13 | >>> py_pointer(a) 14 | 15 | >>> sys.getrefcount(a) 16 | 2 17 | >>> py_object(a) 18 | 19 | >>> sys.getrefcount(a) 20 | 4 21 | >>> sys.getrefcount(a) 22 | 3 23 | >>> sys.getrefcount(a) 24 | 3 25 | >>> sys.getrefcount(a) 26 | 3 27 | >>> sys.getrefcount(a) 28 | 3 29 | >>> 30 | -------------------------------------------------------------------------------- /Examples/class_virtual/class_virtual.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace boost::python; 5 | 6 | struct Base 7 | { 8 | Base() { std::cout << "Default constructor of Base" << std::endl; } 9 | float get_val() { std::cout << "get_val from Base" << std::endl; return 10.0; } 10 | virtual ~Base() { std::cout << "Destructor of Base" << std::endl; } 11 | virtual int f() = 0; 12 | }; 13 | 14 | struct BaseWrap : Base, wrapper // wrapping classes that are meant to override in Python easier 15 | { 16 | int f() override { std::cout << "Calling f() in BaseWrap" << std::endl; return this->get_override("f")();} 17 | }; 18 | 19 | BOOST_PYTHON_MODULE(class_virtual) 20 | { 21 | class_("Base") 22 | .def("f", pure_virtual(&Base::f)); 23 | } 24 | -------------------------------------------------------------------------------- /Examples/extracting_c++_objects/extracting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //#include 3 | #include 4 | using namespace boost::python; 5 | 6 | struct Vec2 7 | { 8 | Vec2(double x, double y): length(x), angle(y) { std::cout << "length: " << length << " angle: " << angle << std::endl; } 9 | const double length; 10 | const double angle; 11 | }; 12 | 13 | 14 | void test(object vec345) 15 | { 16 | double l = extract(vec345.attr("length")); 17 | Vec2& v = extract(vec345); 18 | assert(l == v.length); 19 | extract x(vec345); 20 | if (x.check()) 21 | std::cout << "l: " << l << " v.length: " << v.length << std::endl; 22 | } 23 | 24 | BOOST_PYTHON_MODULE(extracting) 25 | { 26 | object vec345 = ( 27 | class_("Vec2", init()) 28 | .def_readonly("length", &Vec2::length) 29 | .def_readonly("angle", &Vec2::angle) 30 | )(3.0, 4.0); 31 | def("test", test); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Examples/inheritance/inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace boost::python; 5 | 6 | struct Base 7 | { 8 | Base() { std::cout << "Default constructor of Base" << std::endl; } 9 | float get_val() { std::cout << "get_val from Base" << std::endl; return 10.0; } 10 | virtual ~Base() { std::cout << "Destructor of Base" << std::endl; } 11 | }; 12 | 13 | struct Derived : Base 14 | { 15 | Derived() { std::cout << "Deault constructor of Derived" << std::endl; } 16 | virtual ~Derived() override { std::cout << "Destructor of Derived" << std::endl; } 17 | }; 18 | 19 | void b(Base *) 20 | { 21 | std::cout << "Function b(Base *)" << std::endl; 22 | } 23 | 24 | void d(Derived *) 25 | { 26 | std::cout << "Function d(Derived *)" << std::endl; 27 | } 28 | 29 | Base* factory() 30 | { 31 | return new Derived; 32 | } 33 | 34 | BOOST_PYTHON_MODULE(inheritance) 35 | { 36 | class_("Base") 37 | .def("get_val", &Base::get_val); 38 | 39 | class_ >("Derived"); 40 | def("b", b); 41 | def("d", d); 42 | def("factory", factory, return_value_policy()); 43 | } 44 | -------------------------------------------------------------------------------- /Examples/call_policies/call_policies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace boost::python; 4 | 5 | struct Z { 6 | void set(const int i) { val = i; } 7 | int get() const { return val; } 8 | int val = 2; 9 | }; 10 | 11 | struct X { 12 | void pr() { std::cout << "pr val: " << val << " from x" << std::endl; } 13 | void set(const int i) { val = i; } 14 | int get() const { return val; } 15 | int val = 3; 16 | }; 17 | 18 | struct Y { 19 | X x; Z* z; 20 | int z_value() { return z->get(); } 21 | }; 22 | 23 | 24 | X &f(Y &y, Z *z) 25 | { 26 | std::cout << "In function f" << std::endl; 27 | y.z = z; 28 | return y.x; 29 | } 30 | 31 | BOOST_PYTHON_MODULE(call_policies) 32 | { 33 | class_("X") 34 | .def("pr", &X::pr) 35 | .def("get", &X::get) 36 | .def("set", &X::set); 37 | class_("Y") 38 | .add_property("x", &Y::x) 39 | .def("z_value", &Y::z_value); 40 | class_("Z") 41 | .def("get", &Z::get) 42 | .def("set", &Z::set); 43 | def("f", f, return_value_policy()); 44 | def("ff", f, return_internal_reference<1, 45 | with_custodian_and_ward<1, 2> >()); 46 | } 47 | -------------------------------------------------------------------------------- /Examples/operators/README.md: -------------------------------------------------------------------------------- 1 | # operators 2 | 3 | ###### python3 operators 4 | 5 | cd Boost-Python-Examples/Examples/operators 6 | make 7 | python3 8 | 9 | Python 3.5.2 (default, Nov 17 2016, 17:05:23) 10 | [GCC 5.4.0 20160609] on linux 11 | Type "help", "copyright", "credits" or "license" for more information. 12 | >>> import operators 13 | >>> f1 = operators.FilePos() 14 | FilePos default constructor 15 | >>> f2 = operators.FilePos() 16 | FilePos default constructor 17 | >>> 18 | >>> f1 + 3 19 | FilePos copy constructor 20 | FilePos default constructor 21 | FilePos copy constructor 22 | 23 | >>> 3 + f2 24 | FilePos copy constructor 25 | FilePos copy constructor 26 | FilePos default constructor 27 | FilePos copy constructor 28 | 29 | >>> 30 | >>> f1 - f2 31 | FilePos copy constructor 32 | FilePos copy constructor 33 | 0 34 | >>> 35 | >>> f1 - 3 36 | FilePos copy constructor 37 | FilePos default constructor 38 | FilePos copy constructor 39 | 40 | >>> 41 | >>> f1 += 3 42 | >>> f1 -= 5 43 | >>> f1 < f2 44 | FilePos copy constructor 45 | FilePos copy constructor 46 | 47 | 48 | -------------------------------------------------------------------------------- /Examples/inheritance/README.md: -------------------------------------------------------------------------------- 1 | # inheritance 2 | 3 | ###### python3 inheritance 4 | 5 | cd Boost-Python-Examples/Examples/inheritance 6 | make 7 | python3 8 | 9 | Python 3.5.2 (default, Nov 17 2016, 17:05:23) 10 | [GCC 5.4.0 20160609] on linux 11 | Type "help", "copyright", "credits" or "license" for more information. 12 | >>> import inheritance 13 | >>> base = inheritance.Base() 14 | Default constructor of Base 15 | 16 | >>> derived = inheritance.Derived() 17 | Default constructor of Base 18 | Deault constructor of Derived 19 | 20 | >>> inheritance.b(base) 21 | Function b(Base *) 22 | >>> inheritance.b(derived) 23 | Function b(Base *) 24 | >>> inheritance.d(base) 25 | Traceback (most recent call last): 26 | File "", line 1, in 27 | Boost.Python.ArgumentError: Python argument types in 28 | inheritance.d(Base) 29 | did not match C++ signature: 30 | d(Derived*) 31 | >>> inheritance.d(derived) 32 | Function d(Derived *) 33 | 34 | >>> inheritance.factory() 35 | Default constructor of Base 36 | Deault constructor of Derived 37 | 38 | 39 | >>> quit() 40 | Destructor of Derived 41 | Destructor of Base 42 | Destructor of Base 43 | Destructor of Derived 44 | Destructor of Base 45 | 46 | -------------------------------------------------------------------------------- /Examples/iterators/iterators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | /* 6 | * If you want to use stl_input_iterator, boost version should be higher than boost 1.58 7 | */ 8 | using namespace boost::python; 9 | 10 | std::vector ivec = { 1, 2, 3, 4, 5 }; 11 | 12 | struct Field 13 | { 14 | std::vector::iterator b_begin() { return ivec.begin(); } 15 | std::vector::iterator b_end() { return ivec.end(); } 16 | std::vector::iterator p_begin = ivec.begin(); 17 | std::vector::iterator p_end = ivec.end(); 18 | }; 19 | 20 | void cpp_iterator() 21 | { 22 | object get_iterator = iterator>(); 23 | object iter = get_iterator(ivec); 24 | // object first = iter.next(); // no member named 'next' in 'boost::python::api::object' 25 | } 26 | 27 | template 28 | void list_assign(std::list& l, object o) 29 | { 30 | // Turn a Python sequence into a STL input range 31 | boost::python::stl_input_iterator begin(o), end; 32 | l.assign(begin, end); 33 | } 34 | 35 | BOOST_PYTHON_MODULE(iterators) 36 | { 37 | // def("__iter__", iterator>()) 38 | def("cpp_iterator", cpp_iterator); 39 | class_("Field") 40 | .add_property("pions", range(&Field::p_begin, &Field::p_end)) 41 | .add_property("bogons", range(&Field::b_begin, &Field::b_end)); 42 | 43 | class_>("list_int") 44 | .def("assign", &list_assign); 45 | } 46 | -------------------------------------------------------------------------------- /Examples/special_operators/special_operators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace boost::python; 7 | class Rational 8 | { 9 | friend Rational abs(Rational); 10 | friend std::ostream& operator<<(std::ostream&, Rational); 11 | public: 12 | Rational(int numerator, int denominator):numerator(numerator), denominator(denominator) 13 | { std::cout << "Default constructor of Rational" << std::endl; } 14 | operator double() const { return static_cast(numerator) / static_cast(denominator); } 15 | std::string str() const { return "I am Rational"; } 16 | private: 17 | int numerator; 18 | int denominator; 19 | }; 20 | 21 | double pow(Rational r1, Rational r2) 22 | { 23 | return pow(double(r1), double(r2)); 24 | } 25 | 26 | Rational abs(Rational r) 27 | { 28 | if (r.numerator > 0 and r.denominator < 0) 29 | return Rational(r.numerator, -r.denominator); 30 | else if (r.numerator < 0 and r.denominator > 0) 31 | return Rational(-r.numerator, r.denominator); 32 | else 33 | return r; 34 | } 35 | 36 | std::ostream& operator<<(std::ostream &os, Rational r) 37 | { 38 | os << r.numerator << " / " << r.denominator << " is " << double(r) << std::endl; 39 | return os; 40 | } 41 | 42 | 43 | 44 | BOOST_PYTHON_MODULE(special_operators) 45 | { 46 | class_("Rational", init()) 47 | .def(float_(self)) // __float__ 48 | .def(pow(self, other())) // __pow__ 49 | .def(abs(self)) // abs 50 | // .def(str(self)); // seems not work, don't know why yet 51 | .def("__str__", &Rational::str); 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Examples/operators/operators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace boost::python; 4 | 5 | class FilePos 6 | { 7 | friend FilePos& operator+=(FilePos&, int); 8 | friend FilePos& operator-=(FilePos&, int); 9 | public: 10 | FilePos(int val = 1):value(val) { std::cout << "FilePos default constructor" << std::endl; } 11 | FilePos(const FilePos& f): value(f.get_val()) { std::cout << "FilePos copy constructor" << std::endl;} 12 | int get_val() const { return value; } 13 | private: 14 | int value; 15 | }; 16 | 17 | FilePos operator+(FilePos f, int v) 18 | { 19 | return FilePos(v + f.get_val()); 20 | } 21 | 22 | FilePos operator+(int v, FilePos f) 23 | { 24 | return f + v; 25 | } 26 | 27 | int operator-(FilePos f1, FilePos f2) 28 | { 29 | return f1.get_val() - f2.get_val(); 30 | } 31 | 32 | FilePos operator-(FilePos f, int i) 33 | { 34 | return f.get_val() - i; 35 | } 36 | 37 | FilePos& operator+=(FilePos &f, int i) 38 | { 39 | f.value += i; 40 | return f; 41 | } 42 | 43 | FilePos& operator-=(FilePos &f, int i) 44 | { 45 | f.value -= i; 46 | return f; 47 | } 48 | 49 | bool operator<(FilePos f1, FilePos f2) 50 | { 51 | return f1.get_val() < f2.get_val(); 52 | } 53 | 54 | 55 | BOOST_PYTHON_MODULE(operators) 56 | { 57 | class_("FilePos") 58 | .def(init()) 59 | .def(self + int()) // __add__ 60 | .def(int() + self) // __radd__ 61 | .def(self - self) // __sub__ 62 | .def(self - int()) // __sub__ 63 | .def(self += int()) // __iadd__ 64 | .def(self -= other()) // when type does not support default-constructor 65 | .def(self < self); // __lt__ 66 | } 67 | 68 | -------------------------------------------------------------------------------- /Examples/embedding/embdding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace boost::python; 5 | 6 | void running_python_code(const char *code) 7 | { 8 | object main_module = import("__main__"); 9 | object main_namespace = main_module.attr("__dict__"); 10 | object result = exec(code, main_namespace); 11 | } 12 | 13 | void get_result() 14 | { 15 | int five_squared; 16 | 17 | object main_module = import("__main__"); 18 | object main_namespace = main_module.attr("__dict__"); 19 | // method 1 20 | object ignored = exec("result = 5 ** 2", main_namespace); 21 | five_squared = extract(main_namespace["result"]); 22 | std::cout << "In cpp, method 1, five_squared: " << five_squared << std::endl; 23 | // method 2 24 | object result = eval("5 ** 2"); 25 | five_squared = extract(result); 26 | std::cout << "In cpp, method 2, five_squared: " << five_squared << std::endl; 27 | } 28 | 29 | void exception_handling() 30 | { 31 | try 32 | { 33 | object result = eval("5/0"); 34 | std::cout << "I will never be printed" << std::endl; 35 | } 36 | catch (error_already_set const &) 37 | { 38 | if (PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) 39 | { 40 | std::cout << "catch ZeroDivisionError in cpp" << std::endl; 41 | PyErr_Print(); 42 | } 43 | else 44 | PyErr_Print(); 45 | } 46 | } 47 | 48 | void sleep_for_20_seconds() 49 | { 50 | object main_module = import("__main__"); 51 | object main_namespace = main_module.attr("__dict__"); 52 | exec("print(\"In python, going to sleep for 20 seconds\")\n" 53 | "import time\n" 54 | "time.sleep(20)", main_namespace); 55 | 56 | } 57 | 58 | int main() 59 | { 60 | Py_Initialize(); 61 | running_python_code("import sys\n" 62 | "print(\"I am called from cpp\")\n" 63 | "print(\"sys.version: \", sys.version)"); 64 | get_result(); 65 | exception_handling(); 66 | sleep_for_20_seconds(); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Examples/overloading/README.md: -------------------------------------------------------------------------------- 1 | # overloading 2 | 3 | ###### python3 overloading 4 | 5 | cd Boost-Python-Examples/Examples/overloading 6 | make 7 | python3 8 | 9 | Python 3.5.2 (default, Aug 18 2017, 17:48:00) 10 | [GCC 5.4.0 20160609] on linux 11 | Type "help", "copyright", "credits" or "license" for more information. 12 | >>> from overloading import * 13 | >>> x = X() 14 | >>> x.f(1) 15 | f(int a) 16 | True 17 | >>> x.f(1, 2) 18 | f(int a, double b) 19 | True 20 | >>> x.f(1,2,3.3) 21 | Traceback (most recent call last): 22 | File "", line 1, in 23 | Boost.Python.ArgumentError: Python argument types in 24 | X.f(X, int, int, float) 25 | did not match C++ signature: 26 | f(X {lvalue}, int, int, int) 27 | f(X {lvalue}, int, double, char) 28 | f(X {lvalue}, int, double) 29 | f(X {lvalue}, int) 30 | >>> x.f(1,2,3) 31 | int(a, b, c), a + b + c is: 6 32 | 6 33 | 34 | 35 | >>> not_member_func(4, "z", 2, 1) 36 | not mem func: a: 4 b: z c: 2 d: 1 37 | True 38 | >>> not_member_func(4, "z", 2, 1) 39 | not mem func: a: 4 b: z c: 2 d: 1 40 | True 41 | >>> not_member_func(4, "z") 42 | not mem func: a: 4 b: z c: 2 d: 3 43 | True 44 | >>> not_member_func(4, "z", 1) 45 | not mem func: a: 4 b: z c: 1 d: 3 46 | True 47 | >>> not_member_func(4, "z", 1, 3) 48 | not mem func: a: 4 b: z c: 1 d: 3 49 | True 50 | 51 | >>> xx = X() 52 | >>> xx.mem_func(1) 53 | variable argument: a: 1 b:  c: 2 d: 3 54 | True 55 | 56 | >>> xxx = X(1) 57 | other constructor 58 | >>> xxx = X(1, "s") 59 | other constructor 60 | >>> 61 | 62 | >>> xxx = X(1) 63 | other constructor 64 | >>> xxx.mem_func(4) 65 | variable argument: a: 4 b:  c: 2 d: 3 66 | True 67 | >>> xxx.mem_func(4, "z") 68 | variable argument: a: 4 b: z c: 2 d: 3 69 | True 70 | >>> 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Examples/overloading/overloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace boost::python; 6 | 7 | struct X 8 | { 9 | X() = default; 10 | X(int a, char b = 'D', std::string c = "constructor", double d = 0.0) { std::cout << "other constructor" << std::endl;} 11 | bool f(int a) 12 | { 13 | std::cout << "f(int a)" << std::endl; 14 | return true; 15 | } 16 | 17 | bool f(int a, double b) 18 | { 19 | std::cout << "f(int a, double b)" << std::endl; 20 | return true; 21 | } 22 | 23 | bool f(int a, double b, char c) 24 | { 25 | std::cout << "f(int a, double b, char c)" << std::endl; 26 | return true; 27 | } 28 | 29 | int f(int a, int b, int c) 30 | { 31 | int result = a + b + c; 32 | std::cout << "int(a, b, c), a + b + c is: " << result << std::endl; 33 | return a + b + c; 34 | } 35 | 36 | bool mem_func(int a, char b = 1, unsigned c = 2, double d = 3) 37 | { 38 | std::cout << "variable argument: a: " << a << " b: " << b << " c: " << c << " d: " << d << std::endl; 39 | return true; 40 | } 41 | }; 42 | 43 | bool not_mem_func(int a, char b = 1, unsigned c = 2, double d = 3) 44 | { 45 | std::cout << "not mem func: a: " << a << " b: " << b << " c: " << c << " d: " << d << std::endl; 46 | return true; 47 | } 48 | 49 | bool (X::*fx1)(int) = &X::f; 50 | bool (X::*fx2)(int, double) = &X::f; 51 | bool (X::*fx3)(int, double, char) = &X::f; 52 | int (X::*fx4)(int, int, int) = &X::f; 53 | 54 | /* 55 | // pointer name should be same as member function name, or compile error will be reported 56 | bool (X::*mem_func)(int, char, unsigned, double) = &X::mem_func; 57 | */ 58 | 59 | BOOST_PYTHON_FUNCTION_OVERLOADS(not_member_func_overloads, not_mem_func, 1, 4); 60 | BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(mem_func_overloads, X::mem_func, 1, 4); 61 | 62 | BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(f_mem_overloads, X::f, 1, 3); 63 | 64 | BOOST_PYTHON_MODULE(overloading) 65 | { 66 | class_("X") 67 | .def("f", fx1) 68 | .def("f", fx2) 69 | .def("f", fx3) 70 | .def("f", fx4) 71 | .def(init<>()) 72 | .def(init>()) 73 | .def("mem_func", &X::mem_func, mem_func_overloads()) 74 | // the overloaded functions must have a common sequence of initial arguments 75 | .def("f_mem_func", fx3, f_mem_overloads()); 76 | def("not_member_func", not_mem_func, not_member_func_overloads()); 77 | } 78 | -------------------------------------------------------------------------------- /Examples/enums/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = enums 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/embedding/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = embdding 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/hello_ext/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = hello_ext 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/iterators/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = iterators 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/operators/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = operators 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/call_policies/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = call_policies 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/class_hello/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = class_hello 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/class_member/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = class_member 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/class_virtual/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = class_virtual 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/inheritance/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = inheritance 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/overloading/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = overloading 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/basic_interface/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = basic_interface 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/class_property/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = class_property 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/reference_count/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = reference_count 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/extracting_c++_objects/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = extracting 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/special_operators/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = special_operators 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/class_virtual_nopure/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = class_virtual_nopure 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/derived_object_types/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = derived_object_types 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/exception_translation/makefile: -------------------------------------------------------------------------------- 1 | # default PTRHON_VERSION is 3.6 on Mac, 3.5 in other os 2 | # if you installed other python version, Please specific your Python Version below 3 | PYTHON_VERSION = None 4 | TARGET = exception_translation 5 | 6 | CFLAGS = -lm -pthread -O3 -std=c++11 -march=native -Wall -funroll-loops -Wno-unused-result 7 | osname := $(shell uname) 8 | 9 | # Only need to change if you install boost-python from source 10 | BOOST_INC = /usr/local/include/boost 11 | BOOST_LIB = /usr/local/lib 12 | 13 | # $(info $$osname is [${osname}]) 14 | 15 | # if PYTHON_VERSION not set, set default PYTHON_VERSION 16 | ifeq ($(osname), Darwin) 17 | EXPORT_DYNAMIC_NAME = export_dynamic 18 | ifeq ($(PYTHON_VERSION), None) 19 | PYTHON_VERSION = 3.6 20 | endif 21 | else 22 | EXPORT_DYNAMIC_NAME = -export-dynamic 23 | ifeq ($(PYTHON_VERSION), None) 24 | PYTHON_VERSION = 3.5 25 | endif 26 | endif 27 | 28 | $(eval REMAINDER := $$$(PYTHON_VERSION)) 29 | FIRST := $(subst $(REMAINDER),,$(PYTHON_VERSION)) 30 | 31 | # set default PYTHON_INCLUDE and LIBPYTHON_PATH for different os 32 | # PYTHON_INCLUDE should be the path contain pyconfig.h 33 | # LIBPYTHON_PATH should be the path contain libpython3.6 or libpython3.5 or libpython2.7 or whichever your python version 34 | ifeq ($(osname), Darwin) 35 | ifeq ($(FIRST), 3) 36 | PYTHON_INCLUDE = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/ 37 | LIBPYTHON_PATH = /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/ 38 | else 39 | PYTHON_INCLUDE = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7/ 40 | LIBPYTHON_PATH = /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/ 41 | endif 42 | else 43 | PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION) 44 | LIBPYTHON_PATH = /usr/lib/python$(PYTHON_VERSION)/config 45 | endif 46 | 47 | # boost python lib, on mac, default is lboost_python for python2.x, lboost_python3 for python3.x 48 | # on ubuntu, name ===> python3.5m 49 | # on mac, name ===> python3.6 50 | ifeq ($(FIRST), 3) 51 | BOOST = lboost_python3 52 | ifeq ($(osname), Darwin) 53 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 54 | else 55 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION)m 56 | endif 57 | else 58 | BOOST = lboost_python 59 | PYTHON_VERSION_FINAL = $(PYTHON_VERSION) 60 | endif 61 | 62 | 63 | $(TARGET).so: $(TARGET).o 64 | g++ $(CFLAGS) -shared -Wl,-$(EXPORT_DYNAMIC_NAME) $(TARGET).o -L$(BOOST_LIB) -$(BOOST) -L$(LIBPYTHON_PATH) -lpython$(PYTHON_VERSION_FINAL) -o $(TARGET).so 65 | 66 | $(TARGET).o: $(TARGET).cpp 67 | g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp 68 | -------------------------------------------------------------------------------- /Examples/exception_translation/exception_translation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace boost::python; 6 | 7 | class MyCPPException : public std::exception 8 | { 9 | private: 10 | std::string message; 11 | std::string extraData; 12 | public: 13 | MyCPPException(std::string message, std::string extraData) 14 | { 15 | this->message = message; 16 | this->extraData = extraData; 17 | } 18 | 19 | const char *what() const throw() 20 | { 21 | return this->message.c_str(); 22 | } 23 | 24 | ~MyCPPException() throw() 25 | { 26 | std::cout << "destructor of MyCPPException" << std::endl; 27 | } 28 | 29 | std::string getMessage() 30 | { 31 | return this->message; 32 | } 33 | 34 | std::string getExtraData() 35 | { 36 | return this->extraData; 37 | } 38 | }; 39 | 40 | void func_from_cpp(bool throwException) 41 | { 42 | std::cout << "Call from func_from_cpp" << std::endl; 43 | if (throwException) 44 | { 45 | throw MyCPPException("Throwing an exception as requested.", "This is the extra data."); 46 | } 47 | } 48 | 49 | PyObject *createExceptionClass(const char* name, PyObject* baseTypeObj = PyExc_Exception) 50 | { 51 | std::string scopeName = extract(scope().attr("__name__")); 52 | std::string qualifiedName0 = scopeName + "." + name; 53 | char* qualifiedName1 = const_cast(qualifiedName0.c_str()); 54 | 55 | PyObject* typeObj = PyErr_NewException(qualifiedName1, baseTypeObj, 0); 56 | if(!typeObj) throw_error_already_set(); 57 | scope().attr(name) = handle<>(borrowed(typeObj)); 58 | return typeObj; 59 | } 60 | 61 | PyObject *myCPPExceptionType = nullptr; 62 | 63 | void translateMyCPPException(const MyCPPException &e) 64 | { 65 | std::cout << "translating exception" << std::endl; 66 | assert(myCPPExceptionType != nullptr); 67 | object pythonExceptionInstance(e); 68 | object exception_new(handle<>(borrowed(myCPPExceptionType))); 69 | exception_new.attr("message") = pythonExceptionInstance.attr("message"); 70 | exception_new.attr("extraData") = pythonExceptionInstance.attr("extraData"); 71 | PyErr_SetObject(myCPPExceptionType, exception_new.ptr()); 72 | } 73 | 74 | BOOST_PYTHON_MODULE(exception_translation) 75 | { 76 | class_ myCPPExceptionClass("MyCPPException", init()); 77 | myCPPExceptionClass 78 | .add_property("message", &MyCPPException::getMessage) 79 | .add_property("extraData", &MyCPPException::getExtraData); 80 | 81 | myCPPExceptionType = createExceptionClass("MyCPPException"); 82 | register_exception_translator(translateMyCPPException); 83 | def("func_from_cpp", func_from_cpp); 84 | } 85 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Boost Python Examples 2 | 3 | - - - 4 | 5 | * [C++ Boost](#C++-Boost) 6 | 7 | - - - 8 | 9 | ### C++ Boost Python 10 | 11 | * [Installation](#Boost-Installation-with-python3-support) 12 | * [Ubuntu](#OS-Ubuntu-16.10) 13 | * [Mac](#OS-Mac) 14 | * [Demo](#Demo) 15 | * [python3 hello_ext example](#python3-hello_ext) 16 | * [python2 hello_ext example](#python2-hello_ext) 17 | * Examples 18 | * [hello_ext](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/hello_ext) 19 | * [class_hello](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/class_hello) 20 | * [class_member](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/class_member) 21 | * [class_property](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/class_property) 22 | * [inheritance](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/inheritance) 23 | * [class_virtual](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/class_virtual) 24 | * [class_virtual_nopure](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/class_virtual_nopure) 25 | * [operators](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/operators) 26 | * [special_operators](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/special_operators) 27 | * [call_policies](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/call_policies) 28 | * [overloading](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/overloading) 29 | * Object Interface 30 | * [basic_interface](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/basic_interface) 31 | * [derived_object_types](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/derived_object_types) 32 | * [extracting_c++_objects](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/extracting_c++_objects) 33 | * [enums](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/enums) 34 | * [reference_count](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/reference_count) 35 | 36 | * [embedding](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/embedding) 37 | * [iterators](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/iterators) 38 | * [exception_translation](https://github.com/zpoint/Boost-Python-Examples/tree/master/Examples/exception_translation) 39 | 40 | ##### Boost Installation with python3 support 41 | 42 | * OS Ubuntu 16.10 43 | 44 | * From source 45 | * Download the latest version [here](http://www.boost.org/) 46 | * Description of **user-config.jam** please refer to [here](http://www.boost.org/build/doc/html/bbv2/overview/configuration.html) 47 | 48 | tar -xzvf boost_1_65_1.tar.gz 49 | cd boost_1_65_1 50 | 51 | # sudo find / -name "python3.5m" # If you don't know where python3.5m is 52 | # which python3 # If you don't know your python3 install directory 53 | echo "using mpi ; 54 | using gcc : : g++ ; 55 | using python : 3.5 : /usr/bin/python3 : /usr/include/python3.5m : /usr/local/lib ;" > ~/user-config.jam 56 | 57 | ./bootstrap.sh --with-python=/usr/bin/python3 --with-python-version=3.5 --with-python-root=/usr/local/lib/python3.5 --prefix=/usr/local 58 | sudo ./b2 install -a --with=all 59 | sudo ldconfig 60 | 61 | * From apt 62 | 63 | sudo apt-get install libboost-all-dev 64 | # link libboost_python3.so 65 | sudo find / -name "libboost_python-py35.so" 66 | # cd to where you find it 67 | cd /usr/lib/x86_64-linux-gnu/ 68 | sudo ln -s libboost_python-py35.so libboost_python3.so 69 | 70 | 71 | * OS Mac 72 | 73 | brew install boost-python --with-python3 74 | # if your python version or lib path is different from mine, please change 75 | # INCLUDE_PATH and LIBPYTHON_PATH in makefile 76 | 77 | # INCLUDE_PATH: 78 | # shold be the path contains "pyconfig.h", below two command can help you find the path 79 | # sudo find / -name "pyconfig.h" 80 | # python3 -c "import sys; print('\n'.join(sys.path))" 81 | 82 | # LIBPYTHON_PATH: 83 | # on mac, with version python3.6, lib name is: libpython3.6.dylib 84 | # sudo find / -name "libpython3.6" 85 | 86 | ##### Demo 87 | 88 | * If you install from source, and you change the prefix path when install, you need also change the **BOOST_INC** and **BOOST_LIB** in **makefile** 89 | * My default python version is python3.5 on Ubuntu, python3.6 on Mac, if you want to use with python 2.7, change **"PYTHON_VERSION"** in makefile 90 | 91 | ###### python3 hello_ext 92 | 93 | git clone https://github.com/zpoint/Boost-Python-Examples.git 94 | cd Boost-Python-Examples/Examples/hello_ext 95 | make 96 | 97 | python3 98 | Python 3.5.2 (default, Nov 17 2016, 17:05:23) 99 | [GCC 5.4.0 20160609] on linux 100 | Type "help", "copyright", "credits" or "license" for more information. 101 | >>> import hello_ext 102 | >>> hello_ext.greet() 103 | 'hello, world' 104 | 105 | 106 | 107 | ###### python2 hello_ext 108 | 109 | git clone https://github.com/zpoint/Boost-Python-Examples.git 110 | cd Boost-Python-Examples/Examples/hello_ext 111 | vim makefile # change the first line "PYTHON_VERSION = None" to "PYTHON_VERSION = 2.7" 112 | make 113 | 114 | python 115 | Python 2.7.12 (default, Nov 19 2016, 06:48:10) 116 | [GCC 5.4.0 20160609] on linux2 117 | Type "help", "copyright", "credits" or "license" for more information. 118 | >>> import hello_ext 119 | >>> hello_ext.greet() 120 | 'hello, world' 121 | --------------------------------------------------------------------------------