Skip to content

Commit 37bb307

Browse files
committed
Pulled out shared function for building C++ module
1 parent c3429ab commit 37bb307

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

python-bindings/overview_article/cython_example.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
cdef extern from "cppmult.hpp":
44
float cppmult(int int_param, float float_param)
55

6-
def pymult( ip, fp ):
7-
return cppmult( ip, fp )
6+
def pymult( int_param, float_param ):
7+
return cppmult( int_param, float_param )

python-bindings/overview_article/tasks.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,28 @@ def build_cppmult(c):
7070
)
7171
print("* Complete")
7272

73+
def compile_python_module(cpp_name, extension_name):
74+
invoke.run("g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
75+
"`python3 -m pybind11 --includes` "
76+
"-I /usr/include/python3.7 -I . "
77+
"{0} "
78+
"-o {1}`python3.7-config --extension-suffix` "
79+
"-L. -lcppmult -Wl,-rpath,.".format(cpp_name, extension_name)
80+
)
81+
7382
@invoke.task(build_cppmult)
7483
def build_pybind11(c):
7584
""" compile and link the pybind11 wrapper library """
7685
print_banner("Building PyBind11 Module")
77-
invoke.run("g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
78-
"`python3 -m pybind11 --includes` "
79-
"-I /usr/include/python3.7 -I . "
80-
"pybind11_wrapper.cpp "
81-
"-o pybind11_example`python3.7-config --extension-suffix` "
82-
"-L. -lcppmult -Wl,-rpath,. "
83-
)
86+
compile_python_module("pybind11_wrapper.cpp", "pybind11_example")
8487
print("* Complete")
8588

8689
@invoke.task()
8790
def test_pybind11(c):
8891
print_banner("Testing PyBind11 Module")
8992
invoke.run("python3 pybind11_test.py", pty=True)
9093

94+
9195
@invoke.task(build_cppmult)
9296
def build_cython(c):
9397
""" build the cython extension module """
@@ -96,12 +100,7 @@ def build_cython(c):
96100
invoke.run("cython --cplus -3 cython_example.pyx -o cython_wrapper.cpp")
97101

98102
# compile and link the cython wrapper library
99-
invoke.run("g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
100-
"-I /usr/include/python3.7 -I . "
101-
"cython_wrapper.cpp "
102-
"-o cython_example`python3.7-config --extension-suffix` "
103-
"-L. -lcppmult -Wl,-rpath,."
104-
)
103+
compile_python_module("cython_wrapper.cpp", "cython_example")
105104
print("* Complete")
106105

107106
@invoke.task()

0 commit comments

Comments
 (0)