python - 如何构建 Boost 和编译 Boost Python

标签 python c++ boost

我正在使用虚拟机来处理 boost 和 python。 (放弃在 Windows 上尝试)

不知道如何编译。不确定要链接什么和路径。不知道如何获取-lboost_python -lboost -lpython3.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

下载了最新的 boost boost_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())

编译:

根据这里的帖子进行了尝试How to compile, create shared library, and import c++ boost module in Python

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 库。一个是我使用 sudo apt-get install libboost-all-dev 安装的,另一个是我从源代码构建的新版本。因此,我必须彻底清除它们并仅安装最后一个。

关于python - 如何构建 Boost 和编译 Boost Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71002238/

相关文章:

python - 从表中提取数据时 MySQLdb Python 出现 500 错误

python - 连接作为 python 中的类实例属性的 numpy 数组

python - 从日期 python 中提取年/月到新列

c++ - 如何从大项目开始修改

python - boost python sleep wrapper导致整个python程序进入休眠状态

python - 使用条件从 df 创建列表

c++ - 是否有像 C# 中那样的 C++ new 声明

C++17:编译器为(静态存储持续时间)const 引用绑定(bind)创建的临时对象(和存储)是否可修改?

c++ - boost 文件系统 : recursive_directory_iterator constructor causes SIGTRAPS and debug problems

c++ - 如果插槽对象包含互斥锁和条件变量,则 Boost signals2 connect() 调用无法编译