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

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)

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

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

Related

how to configure bazel toolchain for cross compile use rules_foreign_cc

my project need cross compile for arm aarch64 on ubuntu x86_64, but failed at configure stage, below is log
log
# Execution platform: //platforms:linux_gcc9_aarch64
SUBCOMMAND: # #boost//:filesystem [action 'Linking external/boost/libfilesystem.so', configuration: fc89852de14da3c51e8226c7c5e3087929e4f56710d05ed9ab86ed21b8eb16d4, execution platform: //platforms:linux_gcc9_aarch64]
(cd /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo && \
exec env - \
PATH=/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/root/src/go/bin:/usr/local/jdk-11.0.15/bin:/root/.ft:/data/.cache/npm/global/bin \
PWD=/proc/self/cwd \
external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc -shared -o bazel-out/k8-fastbuild/bin/external/boost/libfilesystem.so bazel-out/k8-fastbuild/bin/external/boost/_objs/filesystem/codecvt_error_category.o bazel-out/k8-fastbuild/bin/external/boost/_objs/filesystem/directory.o bazel-out/k8-fastbuild/bin/external/boost/_objs/filesystem/exception.o bazel-out/k8-fastbuild/bin/external/boost/_objs/filesystem/operations.o bazel-out/k8-fastbuild/bin/external/boost/_objs/filesystem/path.o bazel-out/k8-fastbuild/bin/external/boost/_objs/filesystem/path_traits.o bazel-out/k8-fastbuild/bin/external/boost/_objs/filesystem/portability.o bazel-out/k8-fastbuild/bin/external/boost/_objs/filesystem/unique_path.o bazel-out/k8-fastbuild/bin/external/boost/_objs/filesystem/utf8_codecvt_facet.o bazel-out/k8-fastbuild/bin/external/boost/_objs/filesystem/windows_file_codecvt.o -Wl,-S -lstdc++ -lm -Wl,-no-as-needed -Wl,-z,relro,-z,now -Wall -pass-exit-codes '-fuse-ld=gold')
# Configuration: fc89852de14da3c51e8226c7c5e3087929e4f56710d05ed9ab86ed21b8eb16d4
# Execution platform: //platforms:linux_gcc9_aarch64
ERROR: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/external/rules_foreign_cc/toolchains/BUILD.bazel:130:10: BootstrapGNUMake external/rules_foreign_cc/toolchains/make failed: (Exit 1): bash failed: error executing command
(cd /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo && \
exec env - \
PATH=/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/root/src/go/bin:/usr/local/jdk-11.0.15/bin:/root/.ft:/data/.cache/npm/global/bin \
/bin/bash -c bazel-out/k8-opt-exec-277332AD/bin/external/rules_foreign_cc/toolchains/make_tool_foreign_cc/wrapper_build_script.sh)
# Configuration: 920514b75167e08aaf5b08b6fc5ff1721a7e3be841d2c442cd3624a15e568e52
# Execution platform: //platforms:linux_gcc9_aarch64
rules_foreign_cc: Build failed!
rules_foreign_cc: Keeping temp build directory and dependencies directory for debug.
rules_foreign_cc: Please note that the directories inside a sandbox are still cleaned unless you specify --sandbox_debug Bazel command line flag.
rules_foreign_cc: Printing build logs:
_____ BEGIN BUILD LOGS _____
+ AR=/root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc-ar
+ ARFLAGS=rcsD
+ CC=/root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc
+ CFLAGS='-fPIC -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -march=armv8-a -g0 -O3 -DNDEBUG -D_FORTIFY_SOURCE=2 -ffunction-sections -fdata-sections -funsafe-math-optimizations -ftree-vectorize -std=c99 -no-canonical-prefixes -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=redacted -D__TIMESTAMP__=redacted -D__TIME__=redacted'
+ LD=/root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc
+ LDFLAGS='-lstdc++ -lm -Wl,-no-as-needed -Wl,-z,relro,-z,now -Wall -pass-exit-codes -fuse-ld=gold -Wl,--gc-sections'
+ ./configure --without-guile --with-guile=no --disable-dependency-tracking --prefix=/root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/bazel-out/k8-opt-exec-277332AD/bin/external/rules_foreign_cc/toolchains/make
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports the include directive... yes (GNU style)
checking for gcc... /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... configure: error: in `/root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/bazel-out/k8-opt-exec-277332AD/bin/external/rules_foreign_cc/toolchains/make.build_tmpdir':
config.log
This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake.
It was created by gperftools configure 2.9.1, which was
generated by GNU Autoconf 2.69. Invocation command line was
$ /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/com_github_gperftools_gperftools/configure --prefix=/root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/bazel-out/k8-fastbuild/bin/external/com_github_gperftools_gperftools/gperftools_build.build_tmpdir/gperftools_build --enable-shared=no --enable-frame-pointers --disable-libunwind
## --------- ##
## Platform. ##
## --------- ##
hostname = ubuntu
uname -m = x86_64
uname -r = 5.4.0-109-generic
uname -s = Linux
uname -v = #123-Ubuntu SMP Fri Apr 8 09:10:54 UTC 2022
/usr/bin/uname -p = unknown
/bin/uname -X = unknown
/bin/arch = unknown
/usr/bin/arch -k = unknown
/usr/convex/getsysinfo = unknown
/usr/bin/hostinfo = unknown
/bin/machine = unknown
/usr/bin/oslevel = unknown
/bin/universe = unknown
PATH: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/bazel-out/k8-fastbuild/bin/external/com_github_gperftools_gperftools/gperftools_build.ext_build_deps/bin
PATH: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo
PATH: /root/.cargo/bin
PATH: /usr/local/sbin
PATH: /usr/local/bin
PATH: /usr/sbin
PATH: /usr/bin
PATH: /sbin
PATH: /bin
PATH: /usr/games
PATH: /usr/local/games
PATH: /snap/bin
PATH: /usr/local/go/bin
PATH: /root/src/go/bin
PATH: /usr/local/jdk-11.0.15/bin
PATH: /root/.ft
PATH: /data/.cache/npm/global/bin
PATH: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/bazel-out/k8-fastbuild/bin/external/com_github_gperftools_gperftools/gperftools_build.ext_build_deps/bin/toolchains
## ----------- ##
## Core tests. ##
## ----------- ##
configure:2617: checking build system type
configure:2631: result: x86_64-pc-linux-gnu
configure:2651: checking host system type
configure:2664: result: x86_64-pc-linux-gnu
configure:2700: checking for a BSD-compatible install
configure:2768: result: /usr/bin/install -c
configure:2779: checking whether build environment is sane
configure:2834: result: yes
configure:2978: checking for a thread-safe mkdir -p
configure:3017: result: /bin/mkdir -p
configure:3024: checking for gawk
configure:3040: found /usr/bin/gawk
configure:3051: result: gawk
configure:3062: checking whether /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/bazel-out/k8-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make/bin/make sets $(MAKE)
configure:3084: result: yes
configure:3113: checking whether /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/bazel-out/k8-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make/bin/make supports nested variables
configure:3130: result: yes
configure:3260: checking whether to enable maintainer-specific portions of Makefiles
configure:3269: result: no
configure:3296: checking for git
configure:3314: found /usr/local/bin/git
configure:3327: result: /usr/local/bin/git
configure:3400: checking whether /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/bazel-out/k8-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make/bin/make supports the include directive
configure:3415: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/bazel-out/k8-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolchains/make/bin/make -f confmf.GNU && cat confinc.out
this is the am__doit target
configure:3418: $? = 0
configure:3437: result: yes (GNU style)
configure:3507: checking for gcc
configure:3534: result: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc
configure:3763: checking for C compiler version
configure:3772: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc --version >&5
aarch64-none-linux-gnu-gcc (GNU Toolchain for the A-profile Architecture 9.2-2019.12 (arm-9.10)) 9.2.1 20191025
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
configure:3783: $? = 0
configure:3772: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc -v >&5
Using built-in specs.
COLLECT_GCC=/root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/external/gcc9_arm_aarch64/bin/../libexec/gcc/aarch64-none-linux-gnu/9.2.1/lto-wrapper
Target: aarch64-none-linux-gnu
Configured with: /tmp/dgboter/bbs/rhev-vm2--rhe6x86_64/buildbot/rhe6x86_64--aarch64-none-linux-gnu/build/src/gcc/configure --target=aarch64-none-linux-gnu --prefix= --with-sysroot=/aarch64-none-linux-gnu/libc --with-build-sysroot=/tmp/dgboter/bbs/rhev-vm2--rhe6x86_64/buildbot/rhe6x86_64--aarch64-none-linux-gnu/build/build-aarch64-none-linux-gnu/install//aarch64-none-linux-gnu/libc --with-bugurl=https://bugs.linaro.org/ --enable-gnu-indirect-function --enable-shared --disable-libssp --disable-libmudflap --enable-checking=release --enable-languages=c,c++,fortran --with-gmp=/tmp/dgboter/bbs/rhev-vm2--rhe6x86_64/buildbot/rhe6x86_64--aarch64-none-linux-gnu/build/build-aarch64-none-linux-gnu/host-tools --with-mpfr=/tmp/dgboter/bbs/rhev-vm2--rhe6x86_64/buildbot/rhe6x86_64--aarch64-none-linux-gnu/build/build-aarch64-none-linux-gnu/host-tools --with-mpc=/tmp/dgboter/bbs/rhev-vm2--rhe6x86_64/buildbot/rhe6x86_64--aarch64-none-linux-gnu/build/build-aarch64-none-linux-gnu/host-tools --with-isl=/tmp/dgboter/bbs/rhev-vm2--rhe6x86_64/buildbot/rhe6x86_64--aarch64-none-linux-gnu/build/build-aarch64-none-linux-gnu/host-tools --enable-fix-cortex-a53-843419 --with-pkgversion='GNU Toolchain for the A-profile Architecture 9.2-2019.12 (arm-9.10)'
Thread model: posix
gcc version 9.2.1 20191025 (GNU Toolchain for the A-profile Architecture 9.2-2019.12 (arm-9.10))
configure:3783: $? = 0
configure:3772: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc -V >&5
aarch64-none-linux-gnu-gcc: error: unrecognized command line option '-V'
aarch64-none-linux-gnu-gcc: fatal error: no input files
compilation terminated.
configure:3783: $? = 1
configure:3772: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc -qversion >&5
aarch64-none-linux-gnu-gcc: error: unrecognized command line option '-qversion'; did you mean '--version'?
aarch64-none-linux-gnu-gcc: fatal error: no input files
compilation terminated.
configure:3783: $? = 1
configure:3803: checking whether the C compiler works
configure:3825: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc -fPIC -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c99 -no-canonical-prefixes -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=redacted -D__TIMESTAMP__=redacted -D__TIME__=redacted -lstdc++ -lm -Wl,-no-as-needed -Wl,-z,relro,-z,now -Wall -pass-exit-codes -fuse-ld=gold -lpthread conftest.c >&5
configure:3829: $? = 0
configure:3877: result: yes
configure:3880: checking for C compiler default output file name
configure:3882: result: a.out
configure:3888: checking for suffix of executables
configure:3895: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc -o conftest -fPIC -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c99 -no-canonical-prefixes -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=redacted -D__TIMESTAMP__=redacted -D__TIME__=redacted -lstdc++ -lm -Wl,-no-as-needed -Wl,-z,relro,-z,now -Wall -pass-exit-codes -fuse-ld=gold -lpthread conftest.c >&5
configure:3899: $? = 0
configure:3921: result:
configure:3943: checking whether we are cross compiling
configure:3951: /root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/gcc9_arm_aarch64/bin/aarch64-none-linux-gnu-gcc -o conftest -fPIC -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c99 -no-canonical-prefixes -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=redacted -D__TIMESTAMP__=redacted -D__TIME__=redacted -lstdc++ -lm -Wl,-no-as-needed -Wl,-z,relro,-z,now -Wall -pass-exit-codes -fuse-ld=gold -lpthread conftest.c >&5
configure:3955: $? = 0
configure:3962: ./conftest
/root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/external/com_github_gperftools_gperftools/configure: line 3964: ./conftest: cannot execute binary file: Exec format error
configure:3966: $? = 126
configure:3973: error: in `/root/.cache/bazel/_bazel_root/dbf178c3b436e7b271af34e78939cab5/execroot/bazel_simple_demo/bazel-out/k8-fastbuild/bin/external/com_github_gperftools_gperftools/gperftools_build.build_tmpdir':
configure:3975: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details
bazel toolchain configuration:
toolchain:
http_archive(
name = "gcc9_arm_aarch64",
build_file = "#bazel_build_file_repo//bazel:gcc_arm_aarch64.BUILD",
sha256 = "8dfe681531f0bd04fb9c53cf3c0a3368c616aa85d48938eebe2b516376e06a66",
strip_prefix = "gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu",
#urls = ["https://developer.arm.com/-/media/Files/downloads/gnu-a/9.2-2019.12/binrel/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu.tar.xz"],
urls = ["file:///root/src/cpp/toolchains/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu.tar.xz"],
)
platform config
constraint_setting(name = "gcc_version")
constraint_value(
name = "gcc_9",
constraint_setting = ":gcc_version",
)
constraint_value(
name = "gcc_10",
constraint_setting = ":gcc_version",
)
constraint_value(
name = "gcc_11",
constraint_setting = ":gcc_version",
)
platform(
name = "linux_gcc9_aarch64",
constraint_values = [
"#platforms//cpu:aarch64",
"#platforms//os:linux",
":gcc_9",
],
)
toolchain config
toolchain(
name = "gcc9_arm_aarch64_xcompile_toolchain",
exec_compatible_with = [
"#platforms//cpu:x86_64",
"#platforms//os:linux",
],
target_compatible_with = [
"#platforms//cpu:aarch64",
"#platforms//os:linux",
"//platforms:gcc_9",
],
toolchain = "#gcc9_arm_aarch64//:cc_toolchain",
toolchain_type = "#bazel_tools//tools/cpp:toolchain_type",
)
here is a very tiny demo to reproduce it:
[enter link description here][https://github.com/xiedeacc/bazel_simple_demo.git]

Problem using VariantDir with a SCons Builder that generates a list of files to use as source for Program

I have a SConstruct that build my tool in 3 steps:
build a static library. In my case it's the antlr4 c++ runtime
generate the antlr4 cpp and h files (listener, visitor etc) of an antlr4's grammar
finally build a program using those files and linking to the antlr4 runtime library.
1 and 2 can be built in any order, even together but 3 ahs to be built when the other two have finished.
All this works perfectly when I don't use any VariantDir, however SCons fails when I try to use a variant directory, saying that it can't find the autogenerated files:
cons: *** [build/Parser/CoralBaseListener.o] Source `build/Parser/CoralBaseListener.cpp' not found, needed by target `build/Parser/CoralBaseListener.o'.
this is the whole SConstruct:
import os
import subprocess
env = Environment(
CC = "clang",
CXX = "clang++",
CXXFLAGS = ['-std=c++11', '-stdlib=libc++']
)
env.Append(ENV = {'CLASSPATH': './dependencies/antlr4/antlr-4.8-complete.jar'})
# RelWithDbgInfo
env.Append(CXXFLAGS = ['-O2', '-g', '-DNDEBUG'])
VariantDir('./build', './src')
VariantDir('./build/antlr4Runtime', './dependencies/antlr4Runtime')
#
# Builder for generating grammar files with antlr4 (the java app)
#
def auto_antlr_generated_files(prefix):
return [File(prefix + _file + ext) for _file in [
'CoralBaseListener',
'CoralBaseVisitor',
'CoralLexer',
'CoralListener',
'CoralParser',
'CoralVisitor'] for ext in ['.cpp', '.h']
]
def antlr_emitter(target, source, env):
target = auto_antlr_generated_files('src/Parser/')
return target, source
AntlrBuilder = Builder(
action='java org.antlr.v4.Tool $SOURCE -Dlanguage=Cpp -package Coral -visitor -listener -o src/Parser',
emitter=antlr_emitter
)
env.Append(BUILDERS={'Antlr': AntlrBuilder})
#
# Cloning the Environment for Antlr4 runtime
#
antlrEnv = env.Clone()
antlrEnv.Append(CXXFLAGS = [
'-Wall',
'-pedantic',
'-W',
'-Wno-overloaded-virtual',
'-Wno-dollar-in-identifier-extension',
'-Wno-four-char-constants'
])
#
# Building in 3 steps, first the antlr4 runtime library, then the parser files with Antlr then the final program
#
AntlrRuntime = antlrEnv.StaticLibrary(
'lib/antlr4-runtime',
source =
Glob('./build/antlr4Runtime/*.cpp') + \
Glob('./build/antlr4Runtime/atn/*.cpp') + \
Glob('./build/antlr4Runtime/dfa/*.cpp') + \
Glob('./build/antlr4Runtime/misc/*.cpp') + \
Glob('./build/antlr4Runtime/support/*.cpp') + \
Glob('./build/antlr4Runtime/tree/*.cpp') + \
Glob('./build/antlr4Runtime/tree/pattern/*.cpp') + \
Glob('./build/antlr4Runtime/tree/xpath/*.cpp'),
CPPPATH = './build/antlr4Runtime',
ARFLAGS = 'qc'
)
ParserFiles = env.Antlr(source='./Coral.g4')
CoralLang = env.Program(target = 'coral',
source = Glob('./build/*.cpp') + Glob('./build/Parser/*.cpp'),
CPPPATH = ['./build', './build/antlr4Runtime'],
LIBPATH = './lib',
LIBS = ['antlr4-runtime']
)
Requires(ParserFiles, AntlrRuntime)
Depends(auto_antlr_generated_files('./build/src/Parser/'), ParserFiles)
Depends(CoralLang, ParserFiles)
It is probably worth mentioning that at this point, when I have the error, the autogenerated antlr4 files are not in ./src nor in ./build, So basically the command never generated them in first place.
note that without VariantDir everything works as expected. Everything is build in the right steps without problems
import os
import subprocess
env = Environment(
CC = "clang",
CXX = "clang++",
CXXFLAGS = ['-std=c++11', '-stdlib=libc++']
)
env.Append(ENV = {'CLASSPATH': './dependencies/antlr4/antlr-4.8-complete.jar'})
# RelWithDbgInfo
env.Append(CXXFLAGS = ['-O2', '-g', '-DNDEBUG'])
#
# Builder for generating grammar files with antlr4 (the java app)
#
def auto_antlr_generated_files(prefix):
return [File(prefix + _file + ext) for _file in [
'CoralBaseListener',
'CoralBaseVisitor',
'CoralLexer',
'CoralListener',
'CoralParser',
'CoralVisitor'] for ext in ['.cpp', '.h']
]
def antlr_emitter(target, source, env):
target = auto_antlr_generated_files('src/Parser/')
return target, source
AntlrBuilder = Builder(
action='java org.antlr.v4.Tool $SOURCE -Dlanguage=Cpp -package Coral -visitor -listener -o src/Parser',
emitter=antlr_emitter
)
env.Append(BUILDERS={'Antlr': AntlrBuilder})
#
# Cloning the Environment for Antlr4 runtime
#
antlrEnv = env.Clone()
antlrEnv.Append(CXXFLAGS = [
'-Wall',
'-pedantic',
'-W',
'-Wno-overloaded-virtual',
'-Wno-dollar-in-identifier-extension',
'-Wno-four-char-constants'
])
#
# Building in 3 steps, first the antlr4 runtime library, then the parser files with Antlr then the final program
#
AntlrRuntime = antlrEnv.StaticLibrary(
'lib/antlr4-runtime',
source =
Glob('./dependencies/antlr4Runtime/*.cpp') + \
Glob('./dependencies/antlr4Runtime/atn/*.cpp') + \
Glob('./dependencies/antlr4Runtime/dfa/*.cpp') + \
Glob('./dependencies/antlr4Runtime/misc/*.cpp') + \
Glob('./dependencies/antlr4Runtime/support/*.cpp') + \
Glob('./dependencies/antlr4Runtime/tree/*.cpp') + \
Glob('./dependencies/antlr4Runtime/tree/pattern/*.cpp') + \
Glob('./dependencies/antlr4Runtime/tree/xpath/*.cpp'),
CPPPATH = './dependencies/antlr4Runtime',
ARFLAGS = 'qc'
)
ParserFiles = env.Antlr(source='./Coral.g4')
CoralLang = env.Program(target = 'coral',
source = Glob('./src/*.cpp') + Glob('./src/Parser/*.cpp'),
CPPPATH = ['./src', './dependencies/antlr4Runtime'],
LIBPATH = './lib',
LIBS = ['antlr4-runtime']
)
Requires(ParserFiles, AntlrRuntime)
Depends(CoralLang, ParserFiles)
I have the feeling that I'm doing something remarkably silly but I can't figure out what exactly.
This is what SCons output with the first SConstruct, the one that uses VariantDir:
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: *** [build/Parser/CoralBaseListener.o] Source `build/Parser/CoralBaseListener.cpp' not found, needed by target `build/Parser/CoralBaseListener.o'.
scons: building terminated because of errors.
This is really all and if I read this correctly, SCons is jumping straight to point 3 and of course at it fails because point 2 didn't create the files that point 3 needs as source.
This is instead a snippet of the output of the second SConstruct, the one that doesn't use VariantDir. (I cut off some of the similar lines where Scons create the static library, but they are all similar, as you know)
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
clang++ -o dependencies/antlr4Runtime/ANTLRErrorListener.o -c -std=c++11 -stdlib=libc++ -O2 -g -DNDEBUG -Wall -pedantic -W -Wno-overloaded-virtual -Wno-dollar-in-identifier-extension -Wno-four-char-constants -Idependencies/antlr4Runtime dependencies/antlr4Runtime/ANTLRErrorListener.cpp
...
clang++ -o dependencies/antlr4Runtime/tree/xpath/XPathWildcardElement.o -c -std=c++11 -stdlib=libc++ -O2 -g -DNDEBUG -Wall -pedantic -W -Wno-overloaded-virtual -Wno-dollar-in-identifier-extension -Wno-four-char-constants -Idependencies/antlr4Runtime dependencies/antlr4Runtime/tree/xpath/XPathWildcardElement.cpp
ar qc lib/libantlr4-runtime.a dependencies/antlr4Runtime/ANTLRErrorListener.o ... dependencies/antlr4Runtime/tree/xpath/XPathWildcardElement.o
ranlib lib/libantlr4-runtime.a
java org.antlr.v4.Tool Coral.g4 -Dlanguage=Cpp -package Coral -visitor -listener -o src/Parser
clang++ -o src/Parser/CoralListener.o -c -std=c++11 -stdlib=libc++ -O2 -g -DNDEBUG -Isrc -Idependencies/antlr4Runtime src/Parser/CoralListener.cpp
clang++ -o src/Parser/CoralBaseListener.o -c -std=c++11 -stdlib=libc++ -O2 -g -DNDEBUG -Isrc -Idependencies/antlr4Runtime src/Parser/CoralBaseListener.cpp
clang++ -o src/Parser/CoralParser.o -c -std=c++11 -stdlib=libc++ -O2 -g -DNDEBUG -Isrc -Idependencies/antlr4Runtime src/Parser/CoralParser.cpp
clang++ -o src/Parser/CoralLexer.o -c -std=c++11 -stdlib=libc++ -O2 -g -DNDEBUG -Isrc -Idependencies/antlr4Runtime src/Parser/CoralLexer.cpp
clang++ -o src/main.o -c -std=c++11 -stdlib=libc++ -O2 -g -DNDEBUG -Isrc -Idependencies/antlr4Runtime src/main.cpp
clang++ -o src/Parser/CoralVisitor.o -c -std=c++11 -stdlib=libc++ -O2 -g -DNDEBUG -Isrc -Idependencies/antlr4Runtime src/Parser/CoralVisitor.cpp
clang++ -o src/Parser/CoralBaseVisitor.o -c -std=c++11 -stdlib=libc++ -O2 -g -DNDEBUG -Isrc -Idependencies/antlr4Runtime src/Parser/CoralBaseVisitor.cpp
clang++ -o coral src/main.o src/Parser/CoralBaseListener.o src/Parser/CoralBaseVisitor.o src/Parser/CoralLexer.o src/Parser/CoralListener.o src/Parser/CoralParser.o src/Parser/CoralVisitor.o -Llib -lantlr4-runtime
scons: done building targets.
So reading this is obvious that Scons start with building the static library, then he runs antlr to generate the files needed to parse my syntax and finally, when all the dependencies are created, it build the program.

Why does radare2 return an empty list of imports for a non empty main()?

I have the following little program I want to generate a call graph for:
#include <string>
int main() {
auto s = std::string("hello");
s += " welt";
return 0;
}
I compile it in a way which I hope preserves all information in order to be sure all required symbols can be found in the binary:
g++ foo.cpp -O0 -fno-function-cse -fomit-frame-pointer
Then I run radare2 to generate a call graph, format it using Python's pprint and search for sym.main:
radare2 -qAc 'agCj' a.out \
| python3 -c "import sys, json, pprint; pprint.pprint(json.load(sys.stdin))" \
| grep -C 5 sym.main
The output contains:
{'imports': ['sym.std::allocator_char_::allocator',
'sym.std::__cxx11::basic_string_char_std::char_traits_char__std::allocator_char__::basic_string_charconst__std::allocator_char_const',
'sym.std::allocator_char_::_allocator',
'sym.std::__cxx11::basic_string_char_std::char_traits_char__std::allocator_char__::operator___charconst',
'sym.std::__cxx11::basic_string_char_std::char_traits_char__std::allocator_char__::_basic_string'],
'name': 'sym.main',
'size': 88}
Which means (at least I think so) that main() contains calls to the listed symbols.
When I now repeat everything but adding -static to the g++ call, the sym.main entry does not contain any symbol any more:
g++ foo.cpp -O0 -g -static -fno-function-cse -fomit-frame-pointer
results in
{'imports': [], 'name': 'sym.main', 'size': 92},
Is there anything I can do about this? How can I see calls to the standard libraries as well when they are glued to the binary?

How to configure ocamlc

As this post suggests, ocaml 4.03.0 compiler or greater defaults warning 31 as an error. This makes me unable to install the camlidl package with the command opam install camidl because it contains a module with conflicting name as in stdlib.cma. How should I configure ocaml so I can run the command?
This is the error message:
#=== ERROR while installing camlidl.1.05 ======================================#
# opam-version 1.2.2
# os darwin
# command make all
# path /Users/xxx/.opam/system/build/camlidl.1.05
# compiler system (4.05.0)
# exit-code 2
# env-file /Users/xxx/.opam/system/build/camlidl.1.05/camlidl-58074-a71bb4.env
# stdout-file /Users/xxx/.opam/system/build/camlidl.1.05/camlidl-58074-a71bb4.out
# stderr-file /Users/xxx/.opam/system/build/camlidl.1.05/camlidl-58074-a71bb4.err
### stdout ###
# [...]
# ocamlc -g -c lexer_midl.mli
# ocamlc -g -c lexer_midl.ml
# ocamlc -g -c parse.mli
# ocamlc -g -c parse.ml
# ocamlc -g -c fixlabels.mli
# ocamlc -g -c fixlabels.ml
# ocamlc -g -c normalize.mli
# ocamlc -g -c normalize.ml
# ocamlc -g -c main.ml
# ocamlc -g -o camlidl config.cmo utils.cmo clflags.cmo prefix.cmo lexpr.cmo cvttyp.cmo variables.cmo array.cmo struct.cmo enum.cmo union.cmo cvtval.cmo structdecl.cmo enumdecl.cmo uniondecl.cmo typedef.cmo funct.cmo constdecl.cmo intf.cmo file.cmo predef.cmo linenum.cmo parse_aux.cmo parser_midl.cmo lexer_midl.cmo parse.cmo fixlabels.cmo normalize.cmo main.cmo
### stderr ###
# [...]
# File "parse_aux.ml", line 459, characters 10-26:
# Warning 3: deprecated: String.lowercase
# Use String.lowercase_ascii instead.
# 12 shift/reduce conflicts.
# File "array.cmo", line 1:
# Warning 31: files array.cmo and /usr/local/lib/ocaml/stdlib.cma(Array) both define a module named Array
# File "_none_", line 1:
# Error: Some fatal warnings were triggered (1 occurrences)
# make[1]: *** [camlidl] Error 2
# make: *** [all] Error 2

No type named 'VarDictionary' in namespace 'pp' using PNaCl

Can someone explain me why am I getting the compile error
error: no type named 'VarDictionary' in namespace 'pp'
pp::VarDictionary dictionary;
~~~~^
I'm tying to set a dictionary in the function
virtual void HandleMessage(const pp::Var& message) {
}
I copied the example from the bottom of this Google page https://developer.chrome.com/native-client/devguide/coding/message-system
and tried something simple like this
virtual void HandleMessage(const pp::Var& message) {
pp::VarDictionary dictionary;
pp::VarArray an_array;
an_array.Set(0, pp::Var("string0"));
an_array.Set(1, pp::Var("string1"));
dictionary.Set(pp::Var("param_array"), an_array);
PostMessage(dictionary);
}
but when I compile the code i get the error message for the pp::VarDictionary dictionary; however no problem with the pp::VarArray an_array;
I'm using this Makefile from Google
# Copyright (c) 2013 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# GNU Make based build file.  For details on GNU Make see:
# http://www.gnu.org/software/make/manual/make.html
#
#
# Get pepper directory for toolchain and includes.
#
# If NACL_SDK_ROOT is not set, then assume it can be found three directories up.
#
THIS_MAKEFILE := $(abspath $(lastword $(MAKEFILE_LIST)))
NACL_SDK_ROOT ?= $(abspath $(dir $(THIS_MAKEFILE))../..)
# Project Build flags
WARNINGS := -Wno-long-long -Wall -Wswitch-enum -pedantic -Werror
CXXFLAGS := -pthread -std=gnu++98 $(WARNINGS)
#
# Compute tool paths
#
GETOS := python $(NACL_SDK_ROOT)/tools/getos.py
OSHELPERS = python $(NACL_SDK_ROOT)/tools/oshelpers.py
OSNAME := $(shell $(GETOS))
RM := $(OSHELPERS) rm
PNACL_TC_PATH := $(abspath $(NACL_SDK_ROOT)/toolchain/$(OSNAME)_pnacl)
PNACL_CXX := $(PNACL_TC_PATH)/bin/pnacl-clang++
PNACL_FINALIZE := $(PNACL_TC_PATH)/bin/pnacl-finalize
CXXFLAGS := -I$(NACL_SDK_ROOT)/include
LDFLAGS := -L$(NACL_SDK_ROOT)/lib/pnacl/Release -lppapi_cpp -lppapi -ljsoncpp
#
# Disable DOS PATH warning when using Cygwin based tools Windows
#
CYGWIN ?= nodosfilewarning
export CYGWIN
# Declare the ALL target first, to make the 'all' target the default build
all: job1.pexe
clean:
$(RM) job1.pexe job1.bc
job1.bc: job1.cc
$(PNACL_CXX) -o $# $< -O2 $(CXXFLAGS) $(LDFLAGS)
job1.pexe: job1.bc
$(PNACL_FINALIZE) -o $# $<
#
# Makefile target to run the SDK's simple HTTP server and serve this example.
#
HTTPD_PY := python $(NACL_SDK_ROOT)/tools/httpd.py
.PHONY: serve
serve: all
$(HTTPD_PY) -C $(CURDIR)
You need to add the header file for var_dictionary.h that is where the class you are trying to access is declared.
Source code for that header is available here