0

I have seen all the posts about undefined symbols when loading a c++ module from python. What I haven't really seen is undefined symbol once loaded and used.

Here is the python:

import ctypes

libname = "/path/to/library/libtest.so"
c_lib = ctypes.CDLL(libname)

print(c_lib.check("str"))

Here is the C++:

test.h

#include <string>

bool check(const std::string& str);

static std::string mode;

test.cc

bool check(const std::string& str) {
  return mode == str;
}

I am using bazel 5.0 on Ubuntu 18.04.

I assume this could be recreated using raw gcc but I am not too familiar with it.

I also tried Export C++ function to python using ctypes: undefined symbol by putting everything in an extern C and ended up with my environment variables being undefined symbols while loading. Updated example accordingly.

  • Aren't you missing `extern "C"` somewhere? I doubt `ctypes` understands C++ name mangling. Edit: I wouldn't expect it to understand `std::string` either. In short, either write your library with a C API, or use another approach to call a C++ API from Python. – Thomas Mar 08 '22 at 15:22
  • Oh I see what you mean. I need to rewrite this to look like C. I'll update it and then get back to you! Thanks! – Andrew Katson Mar 08 '22 at 15:24
  • Okay that seems to have worked. I just needed to rewrite it all so it looks like C! Thank you! – Andrew Katson Mar 08 '22 at 15:32

0 Answers0