gpt4 book ai didi

c++ - 导入从 pybind11 构建的模块时出现错误 "Segmentation fault (core dumped)"

转载 作者:行者123 更新时间:2023-12-01 14:53:53 24 4
gpt4 key购买 nike

我在导入从 pybind11 构建的模块时遇到了 python3 的问题
在 Linux 中为 libpcap 导入“pcap.h”

# test.cpp

#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
#include "pcap.h"

void open_pcap(std::string &filename)
{
char errbuf[PCAP_ERRBUF_SIZE];
char *file_name = const_cast<char *>(filename.c_str());

// "segmentation fault" if i add bellow line
pcap_t *pcapfd = pcap_open_offline(file_name, errbuf);
}

namespace py = pybind11;
PYBIND11_MODULE(test, m)
{
m.def("open_pcap", &open_pcap);
}

编译成功
c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` test.cpp -o test`python3-config --extension-suffix`

但是当我从 python3 导入时,我遇到了错误
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
Segmentation fault (core dumped)

仅使用 python3 -v -c "import test"查看错误信息
...
# /usr/lib/python3/dist-packages/apt/progress/__pycache__/text.cpython-36.pyc matches /usr/lib/python3/dist-packages/apt/progress/text.py
# code object from '/usr/lib/python3/dist-packages/apt/progress/__pycache__/text.cpython-36.pyc'
# /usr/lib/python3/dist-packages/apt/progress/__pycache__/base.cpython-36.pyc matches /usr/lib/python3/dist-packages/apt/progress/base.py
# code object from '/usr/lib/python3/dist-packages/apt/progress/__pycache__/base.cpython-36.pyc'
import 'fcntl' # <class '_frozen_importlib.BuiltinImporter'>
import 'apt.progress.base' # <_frozen_importlib_external.SourceFileLoader object at 0x7f583e1da7b8>
import 'apt.progress.text' # <_frozen_importlib_external.SourceFileLoader object at 0x7f583e20b2b0>
import 'apt.package' # <_frozen_importlib_external.SourceFileLoader object at 0x7f583fdad160>
# /usr/lib/python3/dist-packages/apt/__pycache__/cache.cpython-36.pyc matches /usr/lib/python3/dist-packages/apt/cache.py
# code object from '/usr/lib/python3/dist-packages/apt/__pycache__/cache.cpython-36.pyc'
import 'apt.cache' # <_frozen_importlib_external.SourceFileLoader object at 0x7f583fdbd2b0>
# /usr/lib/python3/dist-packages/apt/__pycache__/cdrom.cpython-36.pyc matches /usr/lib/python3/dist-packages/apt/cdrom.py
# code object from '/usr/lib/python3/dist-packages/apt/__pycache__/cdrom.cpython-36.pyc'
import 'apt.cdrom' # <_frozen_importlib_external.SourceFileLoader object at 0x7f583e1f30b8>
Segmentation fault (core dumped)

最佳答案

通过添加 -lpcap 与 libpcap 链接构建模块时到您的命令行。

关于c++ - 导入从 pybind11 构建的模块时出现错误 "Segmentation fault (core dumped)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59525460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com