diff --git a/pybind11-cmake/CMakeLists.txt b/pybind11-cmake/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..191c5ea92306cd824b47dd8599e256630e35f1fb --- /dev/null +++ b/pybind11-cmake/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.1) + +project(Fibonacci VERSION 0.1 + DESCRIPTION "Calculate Fibonacci numbers using C++, with python binding" + LANGUAGES CXX) + +add_library(fibonacci SHARED src/fibonacci.cpp include/fibonacci.h) +target_include_directories(fibonacci PUBLIC include) + +add_executable(fib_cli apps/fib_cli.cpp) +target_link_libraries(fib_cli PUBLIC fibonacci) diff --git a/pybind11-cmake/Makefile b/pybind11-cmake/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..35bf03e21c899900d44281ac1a7886093f34b6a4 --- /dev/null +++ b/pybind11-cmake/Makefile @@ -0,0 +1,35 @@ +all: libfibonacci.so fibmod fib_cli + +fib_cli: apps/fib_cli.cpp libfibonacci.so + # compile fib_cli.cpp using the c++ compiler + # save to `fib_cli` + # link against the library `fibonacci` + # include the current directory in the directories searched for libraries + # include the current directory in the directories searched for headers + # compile the path where the library is to find into the binary + $(CXX) apps/fib_cli.cpp \ + -o fib_cli \ + -lfibonacci \ + -L. \ + -Iinclude \ + -Wl,-rpath . + +libfibonacci.so: include/fibonacci.h src/fibonacci.cpp + # compile fibonacci.cpp into a shared library + # into libfibonacci.so + $(CXX) src/fibonacci.cpp \ + -I include \ + -o libfibonacci.so \ + -Wl,-rpath . \ + --shared + +fibmod: libfibonacci.so src/fibonacci_wrapper.cpp + # compile a python wrapper using pybind 11 + $(CXX) -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup \ + -Iinclude -L. -lfibonacci \ + `python3 -m pybind11 --includes` \ + src/fibonacci_wrapper.cpp \ + -o fibmod`python3-config --extension-suffix` + +clean: + rm -f fib_cli libfibonacci.so fibmod.cpython* fib_cli diff --git a/pybind11-cmake/apps/fib_cli.cpp b/pybind11-cmake/apps/fib_cli.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cf58fae75235c8044efc50c1306012d99b20825 --- /dev/null +++ b/pybind11-cmake/apps/fib_cli.cpp @@ -0,0 +1,12 @@ +#include <iostream> +#include "fibonacci.h" + + +int main() { + + for (unsigned int i = 0; i < 10; i++) { + std::cout << i << ": " << fibonacci(i) << std::endl; + } + + return 0; +} diff --git a/pybind11-cmake/fib_cli b/pybind11-cmake/fib_cli new file mode 100755 index 0000000000000000000000000000000000000000..3db9e49b452e304224eacc2e1d356fb616b04d8d Binary files /dev/null and b/pybind11-cmake/fib_cli differ diff --git a/pybind11-cmake/include/fibonacci.h b/pybind11-cmake/include/fibonacci.h new file mode 100644 index 0000000000000000000000000000000000000000..57fd44944277a33d08f597f6b2a90b7cee4d2c11 --- /dev/null +++ b/pybind11-cmake/include/fibonacci.h @@ -0,0 +1,4 @@ +#ifndef FIBONACCI_H +#define FIBONACCI_H +unsigned long fibonacci(unsigned int n); +#endif diff --git a/pybind11-cmake/src/fibonacci.cpp b/pybind11-cmake/src/fibonacci.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ef56cea2511af865d91ad9274d81088b616ef97 --- /dev/null +++ b/pybind11-cmake/src/fibonacci.cpp @@ -0,0 +1,14 @@ +#include "fibonacci.h" + +unsigned long fibonacci(unsigned int n) { + unsigned long n0 = 1; + unsigned long n1 = 1; + unsigned long tmp = 1; + + for (unsigned int i = 0; i < n; i++) { + tmp = n1; + n1 = n0 + n1; + n0 = tmp; + } + return n0; +} diff --git a/pybind11-cmake/src/fibonacci_wrapper.cpp b/pybind11-cmake/src/fibonacci_wrapper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..104883b7d6e7755e889e59213322a542f17e9330 --- /dev/null +++ b/pybind11-cmake/src/fibonacci_wrapper.cpp @@ -0,0 +1,8 @@ +#include <pybind11/pybind11.h> +#include "fibonacci.h" + +PYBIND11_MODULE(fibmod, m) { + m.doc() = "pybind11 example plugin for fibonacci"; + + m.def("fibonacci", &fibonacci, "Fibonacci function using C++"); +}