Search code examples
static-librariespybind11python-poetrylinker-flags

Pybind11 build with poetry


I am currently trying to build a small pybind11 extension and to integrate smoothly with our current setup, it would be neat if that would work with poetry build && poetry install

from pybind11.setup_helpers import Pybind11Extension, build_ext

def build(setup_kwargs):
    ext_modules = [
        Pybind11Extension(
            "my_module",
            ["folder/one.cpp", "folder/another.cpp"],
            include_dirs=[".", "my_static_lib/include/"],
            extra_compile_args=['-O3', '-pthread'],
            language='c++',
            cxx_std=11
        ),
    ]
    setup_kwargs.update({
        "ext_modules": ext_modules,
        "cmd_class": {"build_ext": build_ext},
        "zip_safe": False,
    })

That compiles all fine, but since I don't know how to inform the linker about the static library that I depend on, I have undefined symbols when I load the package. Any idea how that works? Is this an odd thing to try? Thanks for your help or comments!


Solution

  • It was actually quite simple, I just did not find the documentation anywhere. There is an argument called "extra_link_args", where I can specify all these things:

    def build(setup_kwargs):
        ext_modules = [
            Pybind11Extension(
                "my_module",
                ["folder/one.cpp", "folder/another.cpp"],
                include_dirs=[".", "my_static_lib/include/"],
                extra_compile_args=['-O3'],
                extra_link_args=['my_static_lib/libmystatic.a', '-lbz2', '-pthread'],
                language='c++',
                cxx_std=11
            ),
        ]
        setup_kwargs.update({
            "ext_modules": ext_modules,
            "cmd_class": {"build_ext": build_ext},
            "zip_safe": False,
        })

    I still haven't figured out how do specify the compiler, but fortunately, it works for now with the default compiler on my mac.