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:


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

namespace py = pybind11;

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


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


#include <pybind11/pybind11.h>

class myClass
    int number;

    void addOne();

int getNumber();


#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 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.


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


I’m sure what you’re looking for is setuptools. Extension。 I highly recommend you check out this great 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,
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; }')

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

return True

# (c) Sylvain Corlay,
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,
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())
    elif ct == 'msvc':
      opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version())
    for ext in self.extensions:
      ext.extra_compile_args = opts

ext_modules = [
    ['main.cpp', 'myClass.cpp'],
      pybind11.get_include(True ),

  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