问题描述
总结:我无法从 pybind11 documentation 中得到一个示例来在 Docker 中使用 g++ 进行编译。
我有一个包含两个文件的目录,hello.cpp
和 Dockerfile
。 Dockerfile
的内容是
FROM phusion/baseimage:0.11
# Use baseimage-docker's init system
CMD ["/sbin/my_init"]
# Update and install packages
RUN apt update && apt -y upgrade && apt -y install \
build-essential \
python3-dev \
python3-pip
RUN pip3 install pybind11
# Clean up apt mess
RUN apt clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
C++ 文件 hello.cpp
包含
#include <pybind11/embed.h> // everything needed for embedding
namespace py = pybind11;
int main() {
py::scoped_interpreter guard{}; // start the interpreter and keep it alive
py::print("Hello,World!"); // use the Python API
}
.cpp 内容来自 pybind11 tutorial
为了在容器内进行编译,我使用
找出了pybind11所在的位置 python3 -c "import pybind11; print(pybind11.get_include());"
然后我找到了 Python.h 所在的位置
python3-config --includes
我也使用了
指定的标志 python3-config --cflags --ldflags
当我运行以下命令时,出现错误。
g++ -I /usr/local/lib/python3.6/dist-packages/pybind11/include \
-I /usr/include/python3.6m \
-Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.6-e9shER/python3.6-3.6.9=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu \
-L/usr/lib -lpython3.6m -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions \
-std=c++14 \
-fPIC \
hello.cpp
call_calc.cpp: In function ‘int main()’:
call_calc.cpp:13:9: warning: unused variable ‘n’ [-Wunused-variable]
int n = result.cast<int>();
^
/tmp/ccpr9DIE.o: In function `pybind11_static_get':
/usr/local/lib/python3.6/dist-packages/pybind11/include/pybind11/detail/class.h:48: undefined reference to `PyProperty_Type'
/tmp/ccpr9DIE.o: In function `pybind11_static_set':
/usr/local/lib/python3.6/dist-packages/pybind11/include/pybind11/detail/class.h:54: undefined reference to `PyProperty_Type'
/tmp/ccpr9DIE.o: In function `pybind11::cast_error::set_error() const':
/usr/local/lib/python3.6/dist-packages/pybind11/include/pybind11/detail/common.h:722: undefined reference to `PyExc_RuntimeError'
/usr/local/lib/python3.6/dist-packages/pybind11/include/pybind11/detail/common.h:722: undefined reference to `PyErr_SetString'
/tmp/ccpr9DIE.o: In function `pybind11::detail::translate_exception(std::__exception_ptr::exception_ptr)':
...snipped...
/usr/local/lib/python3.6/dist-packages/pybind11/include/pybind11/cast.h:1052: undefined reference to `PyNumber_Check'
/usr/local/lib/python3.6/dist-packages/pybind11/include/pybind11/cast.h:1053: undefined reference to `PyNumber_Long'
/usr/local/lib/python3.6/dist-packages/pybind11/include/pybind11/cast.h:1056: undefined reference to `PyErr_Clear'
/tmp/ccpr9DIE.o: In function `main':
/usr/local/lib/python3.6/dist-packages/pybind11/include/pybind11/pybind11.h:942: undefined reference to `PyImport_ImportModule'
collect2: error: ld returned 1 exit status
有几个相关的问题 1,2,3 但我无法将它们应用于我的情况。
我可以让 simple function example 工作。
当我使用
g++ -O3 -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes` example_hello_world_py_inside.cpp
我确实得到了一个二进制文件,但运行该二进制文件会触发分段错误。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)