Using in custom c++ library
Closed this issue · 2 comments
Hey,
This looks awesome, however I'm struggling to use it in my custom C++ library that utilises OpenCV. Currently I'm wrapping my C++ Library using Boost Python and would like to use this but I cannot get it working.
I cloned the repo and copied the pyboostcvconverter.hpp, pyboost_cv2_converter.cpp and pyboost_cv3_converter.cpp into my project directory. I then also added the following code into my cpp file where I'm creating the BOOST_PYTHON_MODULE:
using namespace pbcvt;
#if (PY_VERSION_HEX >= 0x03000000)
static void *init_ar(){
#else
static void init_ar(){
#endif
Py_Initialize();
import_array();
return NUMPY_IMPORT_ARRAY_RETVAL;
}
As well as within the actual BOOST_PYTHON_MODULE code:
init_ar();
to_python_converter<cv::Mat,pbcvt::matToNDArrayBoostConverter>();
matFromNDArrayBoostConverter();
I also added to my project's CMakeLists.txt the following lines:
set(PYTHON_OPTIONS "2.X" "3.X")
set(PYTHON_DESIRED_VERSION "2.X" CACHE STRING "Choose which python version to use, options are: ${PYTHON_OPTIONS}.")
set_property(CACHE PYTHON_DESIRED_VERSION PROPERTY STRINGS ${PYTHON_OPTIONS})
However, I get the following compilation errors:
In file included from /src/py_interface/pyboost_cv2_converter.cpp:14:0:
/include/py_interface/pyboostcvconverter.hpp:26:18: warning: ‘pbcvt::opencv_error’ defined but not used [-Wunused-variable]
static PyObject* opencv_error = 0;
^
/include/py_interface/pyboostcvconverter.hpp:44:12: warning: ‘int pbcvt::failmsg(const char*, ...)’ declared ‘static’ but never defined [-Wunused-function]
static int failmsg(const char *fmt, ...);
^
[ 5%] Building CXX object src/CMakeFiles/DMIL.dir/py_interface/pyboost_cv3_converter.cpp.o
[ 8%] Linking CXX shared library ../lib/libDMIL.so
[ 47%] Built target DMIL
[ 50%] Linking CXX executable ../../test/c++/bin/KeyValueType_Tests
../lib/libDMIL.so.0.1.0: undefined reference to `pbcvt_ARRAY_API'
So the library compiles fine and so does the boost python module/wrapping. However, when I go to use the C++ version of the library in some test code it says has an undefined reference to 'pbcvt_ARRAY_API'.
If someone could include some basic instructions on how to use your project within a c++ library it would be greatly appreciated.
Cheers.
@jmount1992, sorry you're having trouble!
check out the top python_module.cpp file in the original code, you'll find the definition for pbcvt_ARRAY_API on top, and you'll also find it in all the .cpp files that are used in the module as well, under the #define NO_IMPORT_ARRAY
. I believe it's a requirement in this usage pattern to define a unique symbol for any C++ API that uses numpy arrays (like yours), and please make certain you have the other defines as well.
Let me know if this solves your issue :)
@jmount1992 , haven't heard from you in awhile, I hope you got this fixed. If not, please feel free to reopen.