Commit 2c1f9e8d authored by Tammo Jan Dijkema's avatar Tammo Jan Dijkema

Add CMake-pybind11 (without pybind11 for now)

parent 02bc16f6
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)
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;
}
#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++");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment