如何构建Boost和编译Boost Python



我使用虚拟机来处理boost和python。(放弃尝试windows(

不知道如何编译。不确定链接和路径。不知道如何获得-lbox_python-lbox-lython3.8

操作系统和版本信息:

lsb_release -a 
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:    20.04
Codename:   focal

gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
python3 --version
Python 3.8.10

下载了最新的boostboost_1_78_0.tar.gz表单https://boostorg.jfrog.io/artifactory/main/release/1.78.0/source/

$tar -xvzf boost_1_78_0.tar.gz
cp boost_1_78_0 ~/boost
cd ~/boost/boost_1_78_0
./bootstrap.sh
#it did whatever it did to install b2
b2 install --prefix=../boost
cd ../boost
ls
>> include lib
# Great looks like include and lib is there

按照建议使用教程文件https://www.boost.org/doc/libs/1_76_0/libs/python/doc/html/tutorial/index.html

hello_ext.cpp

char const* greet()
{
return "hello, world";
}
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}

测试.py

import hello_ext
print(hello_ext.greet())

编译:

基于这里的帖子进行了尝试如何在Python 中编译、创建共享库和导入c++boost模块

g++ -I /usr/include/python3.8 -fpic -c -o hello_ext.o hello_ext.cpp
g++ -o orm.so -shared orm.o -lboost_python -lpython3.8

python3 test.py 
Traceback (most recent call last):
File "test.py", line 1, in <module>
import hello_ext
ImportError: /home/bb/Desktop/boostTest2/hello_ext.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv

尝试:

g++ -I /usr/include/python3.8 -fpic -c -o hello_ext.o hello_ext.cpp
/g++ -o hello_ext.so -shared hello_ext.o -L/home/bb/boost/boost/include/boost/python -L /usr/include/python3.8
python3 test.py 
Traceback (most recent call last):
File "test.py", line 1, in <module>
import hello_ext
ImportError: /home/bb/Desktop/boostTest2/hello_ext.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv

我做错了什么?为什么我不能编译?我需要做什么来编译共享对象?所以我可以把它导入python?

-L和-lib之间有什么区别?是以.a结尾的图书馆吗?

我真的很困惑,非常感谢任何帮助。我在谷歌上查找了很多东西和堆栈溢出,但我已经到了一个地步,我只是在兜圈子,不确定自己做错了什么。

感谢

对我来说,我收到错误是因为我安装了两个不同版本的boost库。一个是我使用CCD_ 1安装的,另一个是从源代码构建的新版本。所以,我不得不彻底清除它们,只安装最后一个。

最新更新