Title: error: undefined reference to 'cv75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0String75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0llocate()'
Tags: android;c++;opencv;cmake;android-ndk
Question: I try to add opencv lib to my native android project. In order to do it I added path to opencv to my CMake file
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
cmake_minimum_required(VERSION 3.4.1)
set(pathToOpenCv D:\\OpenCV-android-sdk)
set(CMAKE VERBOSE MAKEFILE on)
set(CMAKE CXX FLAGS &quot;${CMAKE_CXX_FLAGS} -std=gnu++11&quot;)
set(PARSER_TET_DIR ../../../co_FileCodec)
set(PARSER__DIR_ENGINE ../../../co_FileCodec_Engine/co_DecoderEngine)
set(PARSER_GWS_DIR ../../../GWStdLib)
include_directories(${pathToOpenCv}/sdk/native/jni/include)
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION
${pathToOpenCv}/sdk/native/libs/${CMAKE_ANDROID_ARCH_ABI}/libopencv_java3.so)
set(decoder_engine_source
src/main/cpp/co_DecoderEngineAndroidApi.cpp
src/main/cpp/texture_decoder/texture_codec.cpp
src/main/cpp/util/util.cpp
)
add_library( # Sets the name of the library.
co_decoder_engine_android_lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${decoder_engine_source} )
# libcodec
set(_codec_source
${PARSER__DIR}/co_Decoder/co_MtrDecoder.cpp
${PARSER__DIR}/co_Decoder/co_Decoder.cpp
)
add_library(libcodec SHARED ${_codec_source})
include_directories(src/main/cpp)
include_directories(${PARSER__DIR})
include_directories(${PARSER__DIR}/co_Decoder)
include_directories(${PARSER__DIR}/co_Shared)
include_directories(${PARSER_GWS_DIR})
include_directories(${PARSER__DIR_ENGINE})
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_compile_options(co_decoder_engine_android_lib PRIVATE
&quot;$<$<CONFIG:RELEASE&gt;:-O3&gt;&quot;
&quot;$<$<CONFIG:DEBUG&gt;:-O0&gt;&quot;)
target_compile_options(libcodec PRIVATE
&quot;$<$<CONFIG:RELEASE&gt;:-O3&gt;&quot;
&quot;$<$<CONFIG:DEBUG&gt;:-O0&gt;&quot;)
target_link_libraries( # Specifies the target library.
co_decoder_engine_android_lib
mediandk
android
lib_opencv
libcodec
${log-lib} )
and then I try to add this include #include &quot;opencv2/opencv.hpp&quot; to my .cpp file
but I get such error
ninja: Entering directory `D:\co_repo\com_main\co_Infrastructure\Tier1.0\co_FileCodec_Engine\DecoderEngineBuilder\co_decoder_engine_android\.cxx\cmake\debug\x86'
[1/3] Building CXX object CMakeFiles/libcodec.dir/01a7033fdca6735bc52cb367da99f647/co_DecoderEngine/co_DecoderStream.cpp.o
[2/3] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\x86\liblibcodec.so
FAILED: ../../../../build/intermediates/cmake/debug/obj/x86/liblibcodec.so
cmd.exe /C &quot;cd . &amp;&amp; C:\Users\track\AppData\Local\Android\Sdk\ndk\20.0.5594570\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=i686-none-linux-android24 --gcc-toolchain=C:/Users/track/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/track/AppData/Local/Android/Sdk/ndk/20.0.5594570/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security  -std=c++17 -Wall -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -shared -Wl,-soname,liblibcodec.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\x86\liblibcodec.so CMakeFiles/libcodec.dir/e17dc0da4ce9741e6542b9ad403a181e/co_FileCodec/co_Decoder/co_MtrDecoder.cpp.o CMakeFiles/libcodec.dir/e17dc0da4ce9741e6542b9ad403a181e/co_FileCodec/co_Decoder/co_Decoder.cpp.o CMakeFiles/libcodec.dir/a4eab3dee2189f67213cbef1e5027e5a/co_Decoder/co_DecoderImpl.cpp.o CMakeFiles/libcodec.dir/a4eab3dee2189f67213cbef1e5027e5a/co_Decoder/co_ChunkBuffers.cpp.o CMakeFiles/libcodec.dir/e17dc0da4ce9741e6542b9ad403a181e/co_FileCodec/co_Shared/co_Texture.cpp.o CMakeFiles/libcodec.dir/D_/co_repo/com_main/co_Infrastructure/Tier1.0/GWStdLib/GWS_Basics.cpp.o CMakeFiles/libcodec.dir/01a7033fdca6735bc52cb367da99f647/co_DecoderEngine/co_DecoderStream.cpp.o  -latomic -lm &amp;&amp; cd .&quot;
D:/OpenCV-android-sdk/sdk/native/jni/include\opencv2/core/cvstd.hpp:648: error: undefined reference to 'cv75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0String75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0llocate()'
D:/OpenCV-android-sdk/sdk/native/jni/include\opencv2/core/cvstd.hpp:656: error: undefined reference to 'cv75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0String75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0llocate()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I have already checked everything, what am I doing wrong?


Here is another answer: If nm (dumpbin.exe for Windows) sees cv75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0String75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0llocate() in libopencv_java3.so:
$ nm -CD libopencv_java3.so | grep cv75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0String75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0llocate
00000000003e3fa0 T cv75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0String75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0llocate()
It means that something wrong with your wrapping lib_opencv stuff. Let's try to test it with real absolute path to libopencv_java3.so (copy it from your file manager program) and add it directly to target_link_libraries(co_decoder_engine_android_lib ...) and compile it for selected ABI.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:10/08/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><c++><opencv><cmake><android-ndk>', 'Title': "error: undefined reference to 'cv::String::deallocate()'", 'ViewCount': 1591, 'id': 64265776, 'user_AboutMe': '<p>I work as a programmer in one great company. I carry out a serious project on Android, a huge amount of valuable experience, constant research of new technologies and implementation of non-trivial tasks.</p>\n<p>I mastered a lot of technology libraries.</p>\n<p>You are welcome to Visit my web-page:<br />\n<a href="https://alextimowenko.wixsite.com/android" rel="nofollow noreferrer">https://alextimowenko.wixsite.com/android</a></p>\n', 'user_AccountId': 7378264.0, 'user_DisplayName': 'Sirop4ik', 'user_Reputation': 3954.0, 'user_Views': 2132.0, 'user_id': 5709159.0}}