manhkhoa168
manhkhoa168

Reputation: 191

Error "Segmentation fault (core dumped)" when import module built from pybind11

i met issue with python3 while import module built from pybind11 "pcap.h" imported for libpcap in Linux

# 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);
}

Compile successed with

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

but when i import from python3, i met error

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)

only view info for error with 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)

Upvotes: 3

Views: 4494

Answers (2)

Joshua Swain
Joshua Swain

Reputation: 690

For me, the problem was the Python version I was building with. I was in a virtual environment for python3.8, but I was using the default compile command, which defaulted to python3.6

broken command:

c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` python-binding.cpp  -o matchNotification`python3-config --extension-suffix`

working command:

c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` python-binding.cpp  -o matchNotification`python3.8-config --extension-suffix`

Scroll to the ends of the commands to see the change

Upvotes: 1

Wim Lavrijsen
Wim Lavrijsen

Reputation: 3778

Link with the libpcap by adding -lpcap to your command line when building the module.

Upvotes: 1

Related Questions