I'm trying to link my Python (3.7.6) code to C++ with a pass-back of a boolean from the C++ code to Python. My OS is Ubuntu 16.04. The IDE for the C++ is Visual Studio.
On running the Python wrapper code, I receive the message:
libcppstring.so: undefined symbol: cpp_string
The python code, the *.so file, etc. are all in the same directory. The c++ code was compiled at the terminal prompt with
g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC -o libcppstring.so cpp_string.cpp
My python wrapper code, c++ code, and the header code are very simple. Just want to pass two strings from Python to C++ shared object and return 'true' to Python. Perhaps I have messed up the order of the compile commands.
#---------python wrapper code------------------
import ctypes
import sys
import os
localPath = os.path.dirname(os.path.realpath(__file__))
sys.path.append(localPath)
if __name__ == "__main__":
# Load the shared library into c types.
c_lib = ctypes.CDLL(localPath+"/libcppstring.so")
# Sample data for our call:
string1 = "hello, "
string2 = "world"
c_lib.cpp_string.restype = ctypes.c_bool
answer = c_lib.cpp_string( string1, string2)
print(answer)
cpp code: cpp_string.cpp
#include "cpp_string.h"
bool cpp_string( std::string string1, std::string string2){
/*use this to convert to const char* to use sqlite3 in c */
/*used once the code is working*/
string1.c_str();
return (true);
}
header code: cpp_string.h
#include <string>
#ifdef _MSC_VER
#define EXPORT_SYMBOL __declspec(dllexport)
#else
#define EXPORT_SYMBOL
#endif
EXPORT_SYMBOL bool cpp_string( std::string string1, std::string string2);