Skip to content
Snippets Groups Projects
Commit 02bc16f6 authored by Tammo Jan Dijkema's avatar Tammo Jan Dijkema
Browse files

Add examples

parent ee0ded7b
No related branches found
No related tags found
No related merge requests found
build/
*.so
*.dylib
cmake_minimum_required(VERSION 3.1)
project(Fibonacci VERSION 0.1
DESCRIPTION "Calculate Fibonacci Numbers using C++"
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)
#include <iostream>
#include "fibonacci.h"
int main() {
for (unsigned int i = 0; i < 10; i++) {
std::cout << i << ": " << fibonacci(i) << std::endl;
}
return 0;
}
# cython: language_level = 3
# distutils: language = c++
cdef extern from "fibonacci.h":
cpdef unsigned long fibonacci(unsigned int n)
#ifndef FIBONACCI_H
#define FIBONACCI_H
unsigned long fibonacci(unsigned int n);
#endif
from setuptools import setup, Extension
from Cython.Build import cythonize
import os
ext_modules = [
Extension(
'fib_wrapper',
sources=['fib_wrapper.pyx'],
libraries=['fibonacci'],
runtime_library_dirs=[os.path.abspath('build')],
extra_link_args=['-Wl,-rpath', os.path.abspath('build')],
include_dirs=['include'],
library_dirs=['build'],
)
]
setup(
name='fib_wrapper',
version='0.0.1',
ext_modules=cythonize(ext_modules)
)
#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;
}
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
#include <iostream>
#include "fibonacci.h"
int main() {
for (unsigned int i = 0; i < 10; i++) {
std::cout << i << ": " << fibonacci(i) << std::endl;
}
return 0;
}
File added
#ifndef FIBONACCI_H
#define FIBONACCI_H
unsigned long fibonacci(unsigned int n);
#endif
#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;
}
#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++");
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment