Python – pybind11: Use the setup tool link

pybind11: Use the setup tool link… here is a solution to the problem.

pybind11: Use the setup tool link

I’m trying to build a variation of this example using Python’s setuptools. In the example, there is only one file, main.cpp. However, in my version I added another class. Therefore, there are three files in total:

main.cpp

#include <pybind11/pybind11.h>
#include "myClass.h"

namespace py = pybind11;

PYBIND11_MODULE(python_example, m) {
    m.doc() = R"pbdoc(
        Pybind11 example plugin

)pbdoc";

py::class_<myClass>(m, "myClass")
    .def(py::init<>())
    .def("addOne", &myClass::addOne)
    .def("getNumber", &myClass::getNumber)
    ;
}

myClass.h

#include <pybind11/pybind11.h>

class myClass
{
public:
    int number;

myClass();
    void addOne();

int getNumber();
};

myClass.cpp

#include <pybind11/pybind11.h>
#include "myClass.h"

myClass::myClass() {
    number = 1;
}
void myClass::addOne() {
    number = number + 1;
}

int myClass::getNumber() {
    return number;
}

If I use the original setup.py file in the example, it doesn’t work because I need to link myClass.cpp with main.cpp. How do I use setuptools to do this? Basically, I’m looking for setuptools equivalent to CMake’s target_link_libraries.

I

ask this because I have very little experience with CMake. It will be easier for me to use setuptools.

Solution

I’m sure what you’re looking for is setuptools. Extension。 I highly recommend you check out this great example: https://github.com/pybind/python_example It should be able to guide you through what you need to do.

This is what I extracted for your code. Note that it is basically copied and pasted from example

from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext

import sys, re
import setuptools
import pybind11

# (c) Sylvain Corlay, https://github.com/pybind/python_example
def has_flag(compiler, flagname):

import tempfile

with tempfile. NamedTemporaryFile('w', suffix='.cpp') as f:

f.write('int main (int argc, char **argv) { return 0; }')

try:
      compiler.compile([f.name], extra_postargs=[flagname])
    except setuptools.distutils.errors.CompileError:
      return False

return True

# (c) Sylvain Corlay, https://github.com/pybind/python_example
def cpp_flag(compiler):

if   has_flag(compiler,'-std=c++14'): return '-std=c++14'
  elif has_flag(compiler,'-std=c++11'): return '-std=c++11'
  raise RuntimeError('Unsupported compiler: at least C++11 support is needed')

# (c) Sylvain Corlay, https://github.com/pybind/python_example
class BuildExt(build_ext):

c_opts = {
    'msvc': ['/EHsc'],
    'unix': [],
  }

if sys.platform == 'darwin':
    c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']

def build_extensions(self):
    ct = self.compiler.compiler_type
    opts = self.c_opts.get(ct, [])
    if ct == 'unix':
      opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version())
      opts.append(cpp_flag(self.compiler))
    elif ct == 'msvc':
      opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version())
    for ext in self.extensions:
      ext.extra_compile_args = opts
    build_ext.build_extensions(self)

ext_modules = [
  Extension(
    'python_example',
    ['main.cpp', 'myClass.cpp'],
    include_dirs=[
      pybind11.get_include(False),
      pybind11.get_include(True ),
    ],
    language='c++'
  ),
]

setup(
  name             = 'python_example',
  ext_modules      = ext_modules,
  install_requires = ['pybind11>=2.2.0'],
  cmdclass         = {'build_ext': BuildExt},
  zip_safe         = False,
)

Note that most of the code doesn’t address your specific problem, but rather makes C++11/14 work in a robust manner. I’ve tried to keep most of it in the original example, also to get the full working code here.

Related Problems and Solutions