【发布时间】:2018-02-10 07:54:08
【问题描述】:
我正在尝试编译 this 示例,但出现关于 undefined reference to PyInt_Type/PyString_FromString/PyNumber_Divide 等的错误。我已经将我的构建与 boost_python 和 python3.6m 关联起来。
我正在构建它
g++ example.cpp -L/usr/include/boost/python -lboost_python -lpython3.6m -I/usr/include/python3.6m
main.cpp
#include <boost/python.hpp>
#include <boost/python/detail/wrap_python.hpp>
#include <boost/python/exec.hpp>
#include <boost/python.hpp>
#include <iostream>
#include <string>
#include <Python.h>
using namespace boost::python;
int main() {
Py_Initialize();
object main_module = import("__main__");
object main_namespace = main_module.attr("__dict__");
object ignored = exec("hello = file('hello.txt', 'w')\n"
"hello.write('Hello world!')\n"
"hello.close()",
main_namespace);
}
注意:
另外:如果我正确理解以下发生的事情:当我将我的构建与 lboost_python 链接时,它会使用一些函数 PyInt_Type、PyString_FromString。它有关于它们的返回类型和输入参数的信息,但不是它们的真实定义,即函数体,它是在其他一些库(在我的情况下是 python 库)中定义的,我必须告诉链接器这个库要包含在建造。我的理解正确吗?如果是,那么为什么链接 python3.6m 没有帮助?
【问题讨论】:
-
如果你完全删除 Qt 会怎样,它会起作用吗?
-
@UlrichEckhardt 不,它没有。我用
g++ example.cpp -L/usr/include -lpython3.6m -L/usr/include/boost/python -lboost_python -I/usr/include/boost/python/ -I/usr/include/python3.6m编译它,它给出了完全相同的错误。 -
好吧,然后编辑你的问题,让它更接近一个实际最小的例子。