2

I know - there are a billion and a half similar questions. I tried most (even all) of them and none of them helped.

I need to call a c++ 11 function from python.

// test.cpp
#define DLL_PUBLIC extern "C" __attribute__ ((visibility ("default")))
DLL_PUBLIC int init_module(void** module, void** error);

I'm building SO as following:

# Make
gcc -std=c++11 -c -Wall -Werror -fPIC test.cpp
gcc -shared -o libtest.so test.o

Python code:

# test.py
test_lib = CDLL('./libtest.so')

Exception:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    test_lib = CDLL('./libtest.so', mode=1 )
  File "/usr/lib/python3.5/ctypes/__init__.py", line 347, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: ./libtest.so: undefined symbol: _ZTVN10__cxxabiv120__si_class_type_infoE

I already tried to load clib and stdlib before loading my - doesn't help. Is there any generic way to specify that my lib needs to load all dependencies on loading? (as in windows dlls?)

Thanks, and sorry for bothering.

2
  • 2
    Try g++ instead of gcc. Commented Jan 7, 2018 at 10:07
  • @RustyX it is worked... But why? The problem - that native library is a part of another big project which uses gcc for build, and there is no way to change it... Commented Jan 7, 2018 at 14:02

1 Answer 1

2

It seems you're compiling and linking using the gcc command, which defaults to C mode. For C++ mode you should use the g++ command.

For compiling it's not an issue because GCC detects the .cpp extension and switches to C++ mode, but for linking you really need to tell GCC to link in C++ mode otherwise it won't link with the C++ runtime library.

NB: g++ is roughly equivalent to gcc -xc++ -lstdc++ -shared-libgcc. So that might be the solution for you if you really want to use the gcc command. More details can be found in the GCC documentation for compile and link options.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.