问题描述
我在这里找到了一些我在本地尝试过的代码,它是关于使用 pybind11 从 python3 调用函数的。我成功安装了 pybind11 但是,我的 G++ 编译器找不到 Python.h 模块(因为 pybind11 需要 Python.h 头文件),所以我在这里搜索了一些解决方案,有人说尝试安装 python3.8-dev,有人还说试试安装 libpython3.8-dev,两种解决方案都不起作用。
我别无选择,只能寻找另一种解决方案,在那里我找到了 -I/usr/include/python3.x 的用法,所以我做到了:
g++ -I/usr/include/python3.8 main.cpp
我几乎拥有了一切,直到我发现这个看起来很奇怪的错误:
(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0x4f): undefined reference to `PyFloat_Type'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0x6e): undefined reference to `PyFloat_Type'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0x76): undefined reference to `PyType_IsSubtype'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0xa1): undefined reference to `PyFloat_AsDouble'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0xd9): undefined reference to `PyErr_Occurred'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0xfc): undefined reference to `PyErr_Clear'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0x11f): undefined reference to `PyNumber_Check'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0x14a): undefined reference to `PyNumber_Float'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0x174): undefined reference to `PyErr_Clear'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
我也试过:
g++ -I/usr/include/python3.8 main.cpp -lpython3.8m
它说:
usr/bin/ld: cannot find -lpython3.8m
collect2: error: ld returned 1 exit status
我也试过:
g++ -o main main.py \
-I/usr/include/python3 \
$(python3-config --cflags) $(python3-config --ldflags)
我得到了这个:
/usr/bin/ld: /tmp/ccIRtkYI.o: relocation R_X86_64_32S against undefined symbol `_Py_TrueStruct' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status
我也试过:
g++ -o main main.py \
-I/usr/include/python3 \
$(python3-config --cflags) $(python3-config --ldflags) fPIE
得到这个:
/home/ystyphn/.local/lib/python3.8/site-packages/pybind11/include/pybind11/cast.h:1071: undefined reference to `PyErr_Occurred'
/usr/bin/ld: /home/ystyphn/.local/lib/python3.8/site-packages/pybind11/include/pybind11/cast.h:1076: undefined reference to `PyErr_Clear'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
更新: 代码是:
#include <pybind11/embed.h>
#include <iostream>
namespace py = pybind11;
int main() {
py::scoped_interpreter python;
auto math = py::module::import("math");
double root_two = math.attr("sqrt")(2.0).cast<double>();
std::cout << "The square root of 2 is: " << root_two << "\n";
}
它来自线程: call a Python function from c++ using pybind11
是的,毕竟我还是喜欢编程,希望有人能看到
更新: 我创建了 CMakeLists 文件
cmake_minimum_required(VERSION 3.4)
project(example)
set(CMAKE_LANGUAGE_COMPILER "/usr/bin/c++")
add_subdirectory(pybind11)
add_executable(example main.cpp)
target_link_libraries(example PRIVATE pybind11::embed)
然后我从 github 下载了 pybind11,并将其作为子文件夹包含在我的示例中
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)