0

Working on a code that enables Python to call a C++ code compiled into shared library file (.so file) via Python's ctypes module, using the standard ctypes.CDLL method. The C++ code performs the numerical calculations and the Python code mainly serves as a controller and performs data analysis. Everything worked fine. However, as soon as I included jsoncpp library in C++ code, Python started to complain about undefined symbol errors such as undefined symbol: _ZN4Json5ValueaSES0_ etc. I used jsoncpp library in C++ mainly to export data to a JSON file. Having searched the internet for a while, it appears this hasn't been discussed that much. Any idea how to handle this?

CristiFati
  • 38,250
  • 9
  • 50
  • 87
X...
  • 173
  • 1
  • 2
  • 12
  • How do you compile the C++ code? – Sean Francis N. Ballais Apr 22 '20 at 15:52
  • With a -ljsoncpp flag. The C++ code compiles successfully and delivers the desired JSON file. As soon as I called it from Python, the error occured. – X... Apr 22 '20 at 15:59
  • What platform and compiler are you using? – Sean Francis N. Ballais Apr 22 '20 at 16:05
  • It is on Ubuntu 16.04. ```CC=g++``` ```CFLAGS=-I. -std=c++11``` ```$(CC) -c -Wall -fPIC -ljsoncpp testcode.cpp -o testcode.o $(CFLAGS)``` ```$(CC) -shared -o testcode.so testcode.o```. Version of g++ is ```5.4.0 20160609```. The Python version is 3.7 and the ```.so``` file is called Python via ```ctypes.CDLL("./testcode.so")```. When C++ is not using Jsoncpp, Python call executes normally. (Sorry, can't format codes in comment). – X... Apr 22 '20 at 16:34
  • `-ljsoncpp` is a linker flag, so it's required in the 2nd command (if present it's ignored in the 1st). Anyway the question has nothing to do with *Python* (and related). – CristiFati Apr 22 '20 at 21:47
  • @ CristiFati. Before I figured out this linker flag issue, I didn't know if the problem had anything to do with Python. – X... Apr 23 '20 at 06:31

1 Answers1

1

Well, looks like the solution is to also include -ljsoncpp flag while compiling towards the shared .so file:

 $(CC) -shared -o testcode.so testcode.o -ljsoncpp
X...
  • 173
  • 1
  • 2
  • 12