Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using meson with pybind11 #4677

Closed
richardgedwards opened this issue Dec 26, 2018 · 6 comments
Closed

Using meson with pybind11 #4677

richardgedwards opened this issue Dec 26, 2018 · 6 comments

Comments

@richardgedwards
Copy link

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?

@germandiagogomez
Copy link
Contributor

germandiagogomez commented Dec 26, 2018 via email

@richardgedwards
Copy link
Author

richardgedwards commented Dec 26, 2018

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.

@germandiagogomez
Copy link
Contributor

germandiagogomez commented Dec 26, 2018 via email

@richardgedwards
Copy link
Author

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'))

@fishbotics
Copy link

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!

@liveck
Copy link

liveck commented Sep 23, 2021

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',
    ]
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants