Ask Your Question

C++ compilation issue migrating from 3.4.3 to 4.1.1 on Android

asked 2019-10-16 11:12:21 -0500

Renaud gravatar image

updated 2019-10-17 04:04:52 -0500

I am upgrading an Android JNI project from OpenCV 3.4.3 to 4.1.1. When building an issue came out :

error: undefined reference to 'cv::dnn::dnn4_v20190621::Net::setInput(cv::_InputArray const&, std::string const&, double, cv::Scalar_<double> const&)'

Indeed when I look inside, the name of the function is

void cv::dnn::dnn4_v20190621::Net::setInput(cv::InputArray, const cv::String &, double, const cv::Scalar &)

So the .so don't have the extras '_' characters : _InputArray vs InputArray and Scalar_ vs Scalar

Any idea where the pb comes from ? Others OpenCV modules seems okay but not the DNN module.

I am using CMake 3.6.0-rc2 on a Mac. My CMakeLists.txt contains the following to link by target with the SHARED OpenCV Java library:

cmake_minimum_required(VERSION 3.6)

add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/

Executable : /Users/renaud/Library/Android/sdk/cmake/3.6.4111459/bin/cmake
arguments : 
-DCMAKE_CXX_FLAGS=-frtti -fexceptions -std=c++11
-GAndroid Gradle - Ninja
jvmArgs :


I rebuilt the Java SDK Manually from source to ensure 'include' directory is sync with ''. Same issue.

These are the symbols found in the .so :

nm -D -C jniLibs/armeabi-v7a/ | grep "setInput"
00109bf4 T Java_org_opencv_dnn_Model_setInputCrop_10
00109d78 T Java_org_opencv_dnn_Model_setInputMean_10
0010a5f8 T Java_org_opencv_dnn_Model_setInputParams_10
0010a6d8 T Java_org_opencv_dnn_Model_setInputParams_11
0010a7b4 T Java_org_opencv_dnn_Model_setInputParams_12
0010a888 T Java_org_opencv_dnn_Model_setInputParams_13
0010a948 T Java_org_opencv_dnn_Model_setInputParams_14
0010a9f8 T Java_org_opencv_dnn_Model_setInputParams_15
00109f14 T Java_org_opencv_dnn_Model_setInputScale_10
0010a094 T Java_org_opencv_dnn_Model_setInputSize_10
0010a228 T Java_org_opencv_dnn_Model_setInputSize_11
0010a3a8 T Java_org_opencv_dnn_Model_setInputSwapRB_10
0010d020 T Java_org_opencv_dnn_Net_setInput_10
0010d1ac T Java_org_opencv_dnn_Net_setInput_11
0010d320 T Java_org_opencv_dnn_Net_setInput_12
0010d498 T Java_org_opencv_dnn_Net_setInput_13
0010d57c T Java_org_opencv_dnn_Net_setInputsNames_10
0054c628 T cv::dnn::dnn4_v20190902::Net::setInputsNames(std::__ndk1::vector<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::allocator<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > > > const&)
0054c69c T cv::dnn::dnn4_v20190902::Net::setInput(cv::_InputArray const&, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&, double, cv::Scalar_<double> const&)
005a2830 T cv::dnn::dnn4_v20190902::Model::setInputCrop(bool)
005a2804 T cv::dnn::dnn4_v20190902::Model::setInputMean(cv::Scalar_<double> const&)
005a27f0 T cv::dnn::dnn4_v20190902::Model::setInputSize(cv::Size_<int> const&)
005a27fc T cv::dnn::dnn4_v20190902::Model::setInputSize(int, int)
005a2828 T cv::dnn::dnn4_v20190902::Model::setInputScale(double)
005a2840 T cv::dnn::dnn4_v20190902::Model::setInputParams(double, cv::Size_<int> const&, cv::Scalar_<double> const&, bool, bool)
005a2838 T cv::dnn::dnn4_v20190902::Model::setInputSwapRB(bool)
edit retag flag offensive close merge delete


typedef const _InputArray& InputArray;

and Scalar is Scalar_<double> like Rect is Rect_<int>

berak gravatar imageberak ( 2019-10-16 11:36:57 -0500 )edit

So why do I get the "undefined reference" error, do you think the cause is somewhere else ?

Renaud gravatar imageRenaud ( 2019-10-17 03:44:34 -0500 )edit

no, ofc. not. above are already quotes from the opencv src code.

i'm only trying to hint, that your suspicions are on the wrong track

berak gravatar imageberak ( 2019-10-17 03:47:02 -0500 )edit

cv::String changed to std::string with opencv4, clang vs gcc and incompatible c++ std libraries, idk, not an android dev here.



uhh, both, really ? idk which one is correct, but you have to use the same, that the opencv libs were built against.

berak gravatar imageberak ( 2019-10-17 03:59:58 -0500 )edit

std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > -- i'm unsure, but having doubts that this is a correct std::string

berak gravatar imageberak ( 2019-10-17 04:18:32 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-10-17 04:22:53 -0500

Renaud gravatar image

updated 2019-10-17 04:27:24 -0500

Hi @berak, you are right I just came to the same conclusion on my side from this post Changing the STL in the build.gradle is solving the issue ! I had :

arguments "-DANDROID_STL=c++_shared", "-DANDROID_STL=gnustl_shared"

Now I removed the reference to gnustl.

externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions -std=c++11"
                arguments "-DANDROID_STL=c++_shared"

I am a new user and needs 48 hours before being allowed to answer my own issue. I suggest you post the answer.

edit flag offensive delete link more


hope you don't mind me making this an answer (that's what mod fu is for !), your receipe will for sure be helpful to others !!

berak gravatar imageberak ( 2019-10-17 04:29:00 -0500 )edit

Question Tools

1 follower


Asked: 2019-10-16 11:12:21 -0500

Seen: 558 times

Last updated: Oct 17 '19