未定义的符号:_PyThreadState_Current 使用 pybind 包装的 C++ 代码时

undefined symbol: _PyThreadState_Current when using pybind wrapped C++ code

当我 运行 bazel test ... 时,cpp 代码将编译,但 Python 卡住了。 我在写这个问题之前阅读了这些,但我找不到任何解决方案:

https://github.com/pybind/pybind11/issues/314

https://github.com/carla-simulator/ros-bridge/issues/368

https://python-forum.io/thread-32297.html

OS: Linux 5.11.0-43-generic #47~20.04.2-Ubuntu SMP Mon Dec 13 11:06:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Python: Python 3.8.10

g++: g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

pybind11: v2.8.1

C++代码:

//math.cc
#include <pybind11/pybind11.h>

int add(int i, int j) {
  return i + j;
}

int subtract(int i, int j) {
  return i - j;
}

namespace py = pybind11;

PYBIND11_MODULE(math, m) {
  m.def("add", &add);

  m.def("subtract", &subtract);
}

Python代码:

#math_test.py
from module import t_math

assert t_math.add(1, 1) == 2
assert t_math.subtract(1, 1) == 0

构建:

load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")

pybind_extension(
  name = "t_math",
  srcs = ["math.cc"],
)
    
py_test(
  python_version = "PY3",
  name = "math_test",
  size = "small",
  srcs = ["math_test.py"],
  data = [":t_math.so"],
)

错误:

Traceback (most recent call last): File "/home/user/.cache/bazel/_bazel_user/a768e2cde210bf677ee66cfded678e04/sandbox/linux-sandbox/52/execroot/main/bazel-out/k8-fastbuild/bin/module/math_test.runfiles/main/module/math_test.py", line 7, in from module import t_math ImportError: /home/user/.cache/bazel/_bazel_user/a768e2cde210bf677ee66cfded678e04/sandbox/linux-sandbox/52/execroot/main/bazel-out/k8-fastbuild/bin/module/math_test.runfiles/main/module/t_math.so: undefined symbol: _PyThreadState_Current

有两个问题:

  1. PYBIND11_MODULE宏的第一个参数必须与pybind_extension
  2. 中的相同
  3. 此环境变量必须设置为:PYTHON_BIN_PATH=$(which python3)

固定示例

# BUILD
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")

pybind_extension(
  name = "new_math",
  srcs = ["ex.cpp"],
)
    
py_test(
  python_version = "PY3",
  name = "math_test",
  size = "small",
  srcs = ["math_test.py"],
  data = ["new_math.so"],
)
// ex.cpp
#include <pybind11/pybind11.h>

int add(int i, int j) {
  return i + j;
}

int subtract(int i, int j) {
  return i - j;
}

namespace py = pybind11;

PYBIND11_MODULE(new_math, m) {
  m.def("add", &add);

  m.def("subtract", &subtract);
}
# math_test.py
from module import new_math

assert new_math.add(1, 1) == 2
assert new_math.subtract(1, 1) == 0