Skip to content

Instantly share code, notes, and snippets.

@atifkarim
Last active March 16, 2021 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifkarim/b73ff21417176ba8c227ee704a4e0109 to your computer and use it in GitHub Desktop.
Save atifkarim/b73ff21417176ba8c227ee704a4e0109 to your computer and use it in GitHub Desktop.
bind class template in seperate file using pybind11
pybind_cpp_cmake
-- /include/sample_class_template.h
-- /lib/pybind11
-- /src/binding.cpp
/binding_module_class_template.cpp
/sample_class_template.cpp
CMakeLists.txt
#include <pybind11/pybind11.h>
#include <pybind11/complex.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>
#include <pybind11/numpy.h>
// ignore the unnecessary header file declared till this line
#include "sample_class_template.h"
#include "sample_class_template.cpp"
namespace py = pybind11;
template <typename T = int>
void class_template_do_add_testing(py::module &m, const std::string& typestr1);
PYBIND11_MODULE(somecode, m)
{
class_template_do_add_testing<int>(m, "int");
}
#include "sample_class_template.h"
#include "sample_class_template.cpp"
#include <pybind11/pybind11.h>
#include <pybind11/complex.h>
#include <pybind11/stl.h>
namespace py = pybind11;
template <typename T = int>
void class_template_do_add_testing(py::module &m, const std::string& typestr1)
{
py::class_< DO_ADD<int>>(m, "DO_ADD")
.def(py::init<int>())
.def("show_val", &DO_ADD<int>::show_val);
};
cmake_minimum_required(VERSION 3.10)
project(somecode)
set(MAIN_TARGET_NAME ${PROJECT_NAME})
set(LIB_GEN_PATH ${PROJECT_SOURCE_DIR}/scripts CACHE STRING "Where generated library will be placed")
# include all header file. required to make cpp lib and fotr pybind module
include_directories(${PROJECT_SOURCE_DIR}/include)
# for pybind11 module
add_subdirectory(lib/pybind11)
# a unique name is set for the pybind module
set(PYBIND_TARGET_NAME somecode)
# linking all file which will be binded by pybind. All required cpp file will come here
pybind11_add_module(${PYBIND_TARGET_NAME} ${PROJECT_SOURCE_DIR}/src/sample_class_template.cpp
${PROJECT_SOURCE_DIR}/src/binding_module_class_template.cpp
${PROJECT_SOURCE_DIR}/src/binding.cpp)
# module path(*_cpython_*.so file) for pybind
set_target_properties(${PYBIND_TARGET_NAME}
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${LIB_GEN_PATH}
)
#include "sample_class_template.h"
#include <iostream>
/**
* A sample class template function definition
*/
template <typename T>
void DO_ADD<T>:: show_val(){
cout<<"from show_val function x: "<<x<<endl;
}
#ifndef _SAMPLE_CLASS_TEMPLATE_
#define _SAMPLE_CLASS_TEMPLATE_
#include <iostream>
using namespace std;
/**
* A sample class template
*/
template <typename T>
class DO_ADD{
private:
T x;
public:
DO_ADD(T val):x(val)
{cout<<"DO_ADD constructor is called and x: "<<x<<endl;}
void show_val();
~ DO_ADD() {};
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment