mesonbuild / meson

The Meson Build System

Home Page:http://mesonbuild.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using meson with pybind11

richardgedwards opened this issue · comments

The pybind11 website indicates that to compile with pybind11 one should issue the following:

c++ -O3 -Wall -shared -std=c++11 -fPIC python3 -m pybind11 --includes example.cpp -o examplepython3-config --extension-suffix

How would one get this from the meson-build system?

Hi, thanks for the quick response. pybind11 is a header only library, so adding it to my project is not a problem. As far as I understand, using the wrapdb package as a subproject only helps with adding the pybind11 header to my existing project and does not provide details on how to use it in the existing project. Please correct me if I am wrong.

The problem I am having is getting the correct name of my static library after it is compiled.

Here is my meson.build file:

project('pybindtest', 'cpp', default_options : ['cpp_std=c++14',])
deps = [dependency('python3')] 
src = ['example.cpp']
shared_library('example', sources : src, dependencies : deps)

This compiles a library called libexample.so, where I need the library to be named: example.cpython-36m-x86_64-linux-gnu.so which is generated by concatenating the cpp file name (without extension) example + the output from the the command python3-config --extension-suffix. Is there a way to rename the static library from libexample.so to example.cpython-36m-x86_64-linux-gnu.so within the meson build system. Of course I can do this manually, however I would like to automate the process if possible.

After some research I found the following meson.build works to build pybind11 shared modules.

project('pybindtest', 'cpp', default_options : ['cpp_std=c++11',])
pymod = import('python')
py = pymod.find_installation('python3')
py.extension_module('example', sources : 'example.cpp', dependencies : dependency('python3'))

I know it's been a while since this closed, but @richardgedwards do you have an example repo with how you set everything up to work?

Thanks!

I know it's been a while since this closed, but @richardgedwards do you have an example repo with how you set everything up to work?

Thanks!

since pybind is header-only, all you need is include_dir

project('pybindexample', 'cpp', default_options : ['cpp_std=c++17',])

py3_inst = import('python').find_installation('python3')

pybind11_config = find_program('pybind11-config')
pybind11_config_ret = run_command(pybind11_config, ['--includes'])
pybind11 = declare_dependency(
    include_directories: [pybind11_config_ret.stdout().split('-I')[-1].strip()],
)

python3 = dependency('python3', ) 


py3avlw = py3_inst.extension_module('example',
    sources: ['example.cc',], 
    dependencies : [python3,pybind11],
    link_language : 'cpp',
    override_options: [
        'cpp_rtti=true',
    ]
)