我尝试编译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);
}
注意:
答案 0 :(得分:1)
boost_python
可能指向python 2.7版本,例如在debian stretch中(应该有与ubuntu 16.04类似的包)。
> cd /usr/lib/x86_64-linux-gnu
> ls -l libboost_python*.*
... libboost_python-py35.a
... libboost_python-py27.a
... libboost_python.a -> libboost_python-py27.a
... libboost_python-py27.so.1.55.0
... libboost_python-py27.so -> libboost_python-py27.so.1.62.0
... libboost_python.so -> libboost_python-py27.so
... libboost_python-py35.so.1.62.0
... libboost_python-py35.so -> libboost_python-py35.so.1.62.0
我猜那个库的python 3.6版本可能不容易获得。如果libboost_python-py35
与python 3.6不兼容,最简单的选择可能是使用python 3.5,即
-lboost_python-py35
用于动态链接。
答案 1 :(得分:0)
您以错误的顺序链接库。必须先boost_python
,python3.6m
必须在boost_python
之后,因为python3.6m
取决于-L/usr/include
。
P.S。:这是错误的:-L
:/usr/include
告诉链接器在哪里找到库,但-L/usr/lib
包含头文件,而不是库。你根本不需要它,但是如果你这样做,那就是<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
或类似的。