pybind / pybind/pybind11

Problem with embedded Python in combination with CMake 3.4+

Open
#1,662 7 comments 3 reactions 1 assignee View on GitHub

@henryiii is already working on this.

Since Jul 31, 2020.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

I am not sure whether this is a CMake issue, or a pybind11 issue. The following is a minimal example that reproduces the problem.

A simple module that exposes a function (cpp_module_pybind11.cpp):

#include <pybind11/pybind11.h>
namespace py = pybind11;

PYBIND11_MODULE(cpp_module, m) {
  m.def("add", [](int i, int j) { return i + j; });
}

A Python module that imports cpp_module (issue.py):

import cpp_module

An executable that imports issue:

#include <pybind11/embed.h>
namespace py = pybind11;

int main()
{
  py::scoped_interpreter guard{};
  py::module::import("issue");
}

I am using the following CMake file:

cmake_minimum_required(VERSION 3.4)

find_package(pybind11 REQUIRED)
find_package(Threads)

pybind11_add_module(cpp_module cpp_module_pybind11.cpp)

add_executable(example main.cpp)
target_link_libraries(example PRIVATE pybind11::embed Threads::Threads ${CMAKE_DL_LIBS})

Running ./example after compiling everything (cmake . && make) results in the following error (in combination with Python 3.7):

terminate called after throwing an instance of 'pybind11::error_already_set'
  what():  ImportError: ./cpp_module.cpython-37m-x86_64-linux-gnu.so: undefined symbol: PyInstanceMethod_Type

At:
  ./issue.py(1): <module>
  <frozen importlib._bootstrap>(219): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(728): exec_module
  <frozen importlib._bootstrap>(677): _load_unlocked
  <frozen importlib._bootstrap>(967): _find_and_load_unlocked
  <frozen importlib._bootstrap>(983): _find_and_load

zsh: abort (core dumped)  ./example

When modifying the CMake file to set the minimum required version to 3.3, everything works without any issues.

The root cause seems to be a missing (?) -rdynamic flag when building the executable. Indeed, setting the minimum required CMake version to 3.4 and then manually adding -rdynamic to the target_link_libraries command fixes the issue.

Is this an issue in the pybind CMake files, or an issue in CMake itself? This is the CMake 3.4 changelog: https://cmake.org/cmake/help/latest/release/3.4.html

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.