Android NDK CMakeLists.txt: error: undefined reference to: - android

I've looked around but I've only been able to find solutions that use Android.mk and Application.mk. Am I right in thinking that by using CMakeLists.txt I don't need either of those files? This is my first venture into using the Android NDK.
I've based my CMakeLists.txt off of the SuperpoweredExample and I have called my library nativelib. It seems the .cpp file cannot pick up the references in the header file. Am I doing something wrong? It builds fine but it just throws these errors on compilation.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
nativelib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
G:/Git/NDKTest/app/src/main/jni/nativelib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
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 )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
nativelib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// Below here is copy and pasted from the SuperpoweredExample
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
set(
PATH_TO_SUPERPOWERED
"C:/Users/j/Downloads/Superpowered/Superpowered"
)
message(${ANDROID_ABI})
file(GLOB CPP_FILES "*.cpp")
add_library(
SuperpoweredExample
SHARED
${CPP_FILES}
${PATH_TO_SUPERPOWERED}/AndroidIO/SuperpoweredAndroidAudioIO.cpp
)
include_directories(src/main/jni)
include_directories(${PATH_TO_SUPERPOWERED})
target_link_libraries(
SuperpoweredExample
log
android
OpenSLES
${PATH_TO_SUPERPOWERED}/libSuperpoweredAndroid${ANDROID_ABI}.a
nativelib
)
Stacktrace:
Error:FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing 'G:\sdk1\cmake\3.6.3155560\bin\cmake.exe' with arguments {--build G:\Git\NDKTest\app\.externalNativeBuild\cmake\debug\x86 --target nativelib}
[1/2] Building CXX object CMakeFiles/nativelib.dir/nativelib.cpp.o
[2/2] Linking CXX shared library G:\Git\NDKTest\app\build\intermediates\cmake\debug\obj\x86\libnativelib.so
FAILED: cmd.exe /C "cd . && G:\sdk1\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe -target i686-none-linux-android -gcc-toolchain G:/sdk1/ndk-bundle/toolchains/x86-4.9/prebuilt/windows-x86_64 --sysroot=G:/sdk1/ndk-bundle/platforms/android-16/arch-x86 -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -fsigned-char -Inull -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnativelib.so -o G:\Git\NDKTest\app\build\intermediates\cmake\debug\obj\x86\libnativelib.so CMakeFiles/nativelib.dir/nativelib.cpp.o -llog -lm "G:/sdk1/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86/libgnustl_static.a" && cd ."
G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:16: error: undefined reference to 'SuperpoweredRoll::SuperpoweredRoll(unsigned int)'
G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:17: error: undefined reference to 'SuperpoweredFilter::SuperpoweredFilter(SuperpoweredFilterType, unsigned int)'
G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:18: error: undefined reference to 'SuperpoweredFlanger::SuperpoweredFlanger(unsigned int)'
G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:19: error: undefined reference to 'SuperpoweredRecorder::SuperpoweredRecorder(char const*, unsigned int, unsigned int)'
G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:21: error: undefined reference to 'SuperpoweredAndroidAudioIO::SuperpoweredAndroidAudioIO(int, int, bool, bool, bool (*)(void*, short*, int, int), void*, int, int, int)'
G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:28: error: undefined reference to 'SuperpoweredRecorder::stop()'
G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:30: error: undefined reference to 'SuperpoweredRecorder::start(char const*)'
G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:37: error: undefined reference to 'SuperpoweredRecorder::process(float*, float*, unsigned int)'
G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:38: error: undefined reference to 'SuperpoweredFloatToShortInt(float*, short*, unsigned int, unsigned int)'
G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:43: error: undefined reference to 'SuperpoweredAndroidAudioIO::~SuperpoweredAndroidAudioIO()'
G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:44: error: undefined reference to 'SuperpoweredRecorder::~SuperpoweredRecorder()'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Changed name from native-lib to nativelib, same error.
I have a feeling the error is around the target_link_libraries() of CMakeLists.txt. I am trying to link my NativeLib code with the existing SuperpoweredExample code.

I think linking your "nativelib" happens before linking the Superpowered example, therefore your linker can't find the Superpowered parts used by "nativelib".

Related

ld: Unknown symbol error when trying to run a C++ library - incorrect CMakeLists.txt settings [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed last month.
I've downloaded an external c++ library for use inside a Flutter program via FFI. I've set up the FFI correctly, but I'm getting an error whenever I have the use the function from the external library. Here's the function:
#include <iostream>
#include "aoo/include/aoo/aoo.h"
#include "aoo/include/aoo/aoo_server.h"
#include "aoo/include/aoo/aoo_net.h"
extern "C" void initAoo(){
aoo_initialize(0);
std::cout << "Called from init AOO!" << std::endl;
}
When I compile and run to an android device, I get this error:
* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing process /Users/zacharyhaslam/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /Users/zacharyhaslam/FlutterApplications/state_management_example/android/app/.cxx/cmake/debug/armeabi-v7a add}
ninja: Entering directory `/Users/zacharyhaslam/FlutterApplications/state_management_example/android/app/.cxx/cmake/debug/armeabi-v7a'
[1/2] Building CXX object CMakeFiles/add.dir/cpp/add.cpp.o
[2/2] Linking CXX shared library /Users/zacharyhaslam/FlutterApplications/state_management_example/build/app/intermediates/cmake/debug/obj/armeabi-v7a/libadd.so
FAILED: /Users/zacharyhaslam/FlutterApplications/state_management_example/build/app/intermediates/cmake/debug/obj/armeabi-v7a/libadd.so
: && /Users/zacharyhaslam/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=armv7-none-linux-androideabi19 --sysroot=/Users/zacharyhaslam/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -fno-limit-debug-info -static-libstdc++ -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--fatal-warnings -Wl,--gc-sections -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libadd.so -o /Users/zacharyhaslam/FlutterApplications/state_management_example/build/app/intermediates/cmake/debug/obj/armeabi-v7a/libadd.so CMakeFiles/add.dir/cpp/add.cpp.o -latomic -lm && :
ld: error: undefined symbol: aoo_initialize
>>> referenced by add.cpp:11 (../../../../../cpp/add.cpp:11)
>>> CMakeFiles/add.dir/cpp/add.cpp.o:(initAoo)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed
It says ld: error: undefined symbol: aoo_initialize. From my research, I think the problem must be from the way that I set up the compilation settings, so here's my CMakeLists.txt:
cmake_minimum_required(VERSION 3.10.2)
add_library( add
SHARED
cpp/add.cpp
)
include_directories(cpp/aoo/include/aoo/aoo.h)
add_library(aoo cpp/aoo/include/aoo/aoo.h)
set_target_properties(aoo PROPERTIES LINKER_LANGUAGE CXX)
What is wrong with CMakeLists.txt? Am I properly including the files for compilation?
The basic problem is that aoo is not built. You'll need to take care of that.
I assume that your program is the target "add" and the source above is in cpp/add.cpp
Basically need to setup the aoo library correctly and then tell CMake to link your program against it.
add_library(aoo)
target_include_directories(aoo PUBLIC aoo/include)
target_sources(aoo PRIVATE aoo/src/library-source1.cc
aoo/src/library-source2.cc
...)
add_library(add SHARED)
target_sources(add PRIVATE cpp/add.cpp)
target_link_libraries(add PRIVATE aoo)
You will need to find out how aoo needs to be built though.
However, if you setup it well, you can #include <aoo/aoo.h> as CMake will also tell the compiler where to find the includes.
If aoo provides CMake integration, it may also be possible to just use that.

Read the format of a static library to know how to import it on MacOS

I have a big library that uses the NDK to run some native code.
With the new version I need to replace a library that was build from the source code with static libraries that are provided as a set of prebuilt .a libraries.
I have configured the project to import these libraries like this:
cmake_minimum_required (VERSION 3.10)
set(CMAKE_CXX_STANDARD 14)
add_definitions(-DNO_USE_SPLICE)
# Provide build env vars for http.c now that we are using shared jitter_client repo
add_definitions(-Wdeprecated-declarations -DVERSION="android" -DREVISION="1" -DPROGRAMNAME="sksdk" -DBUILDLINK="android" -DBUILDTIME="built_at_TODO")
add_definitions(-D_Exit=exit)
add_definitions(-D__UAPI_DEF_IF_IFNAMSIZ=1)
include_directories(../../external/cpp
../../external/cpp/libcompany/src
src/main/cpp)
add_library( # Specifies the name of the library.
mynativelib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/cpp_to_jobj/common_convert_cpp_to_jobj.cpp
src/main/cpp/cpp_to_jobj/thread_utils.cpp
src/main/cpp/cpp_to_jobj/utf8_scrubber.cpp
src/main/cpp/jni_utils.cpp
src/main/cpp/logging_utils.cpp
src/main/cpp/android_remote_logging.c
src/main/cpp/executor.cpp)
add_library(curl STATIC IMPORTED)
set_target_properties(curl
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/curl/${ANDROID_ABI}/libcurl.so)
target_compile_definitions(
mynativelib
PRIVATE
ANDROID
)
target_link_libraries(
mynativelib
curl
${CMAKE_SOURCE_DIR}/libs/static/${ANDROID_ABI}/libmystaticlib.a
... other libs here. These are fine ...
)
When I build the library I get the error:
../../../../libs/static/arm64-v8a/libmystaticlib.a: error adding symbols: File format not recognized
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
Here the relevant part of the stacktrace:
Caused by: Build command failed.
Error while executing process /Users/rrr/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /Users/rrr/myproject/p12345/p12345-android-sdk/sdk/p12345module/.cxx/cmake/release/arm64-v8a mynativelib}
ninja: Entering directory `/Users/rrr/myproject/p12345/p12345-android-sdk/sdk/p12345module/.cxx/cmake/release/arm64-v8a'
[1/1] Linking CXX shared library ../../../../build/intermediates/cmake/release/obj/arm64-v8a/libmynativelib.so
FAILED: ../../../../build/intermediates/cmake/release/obj/arm64-v8a/libmynativelib.so
: && /Users/rrr/Library/Android/sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/Users/rrr/Library/Android/sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64 --sysroot=/Users/rrr/Library/Android/sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -O2 -DNDEBUG -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libmynativelib.so -o ../../../../build/intermediates/cmake/release/obj/arm64-v8a/libmynativelib.so CMakeFiles/mynativelib.dir/src/main/cpp/cpp_to_jobj/common_convert_cpp_to_jobj.cpp.o CMakeFiles/mynativelib.dir/src/main/cpp/cpp_to_jobj/thread_utils.cpp.o CMakeFiles/mynativelib.dir/src/main/cpp/cpp_to_jobj/utf8_scrubber.cpp.o CMakeFiles/mynativelib.dir/src/main/cpp/jni_utils.cpp.o CMakeFiles/mynativelib.dir/src/main/cpp/logging_utils.cpp.o CMakeFiles/mynativelib.dir/src/main/cpp/android_remote_logging.c.o CMakeFiles/mynativelib.dir/src/main/cpp/test_executor.cpp.o -L/Users/rrr/myproject/p12345/p12345-android-sdk/sdk/p12345module/libs/routerAgent/shared/arm64-v8a ../../../../libs/curl/arm64-v8a/libcurl.so ../../../../libs/routerAgent/static/arm64-v8a/libmystaticlib.a -lavformat -lavcodec -lavutil -ljansson ../../../../libs/routerAgent/static/arm64-v8a/libabctytt_wrapper.a ../../../../libs/routerAgent/static/arm64-v8a/libp12345_logging.a ../../../../libs/routerAgent/static/arm64-v8a/libabctytt.a ../../../../libs/routerAgent/static/arm64-v8a/libabcwebfetcher.a ../../../../libs/routerAgent/static/arm64-v8a/libabctargetconfig_core.a ../../../../libs/routerAgent/static/arm64-v8a/libabctargetconfig_config_parsing.a ../../../../libs/routerAgent/static/arm64-v8a/libabctargetconfig_common.a ../../../../libs/routerAgent/static/arm64-v8a/libp12345_latency.a ../../../../libs/routerAgent/static/arm64-v8a/libabc_sk.a ../../../../libs/routerAgent/static/arm64-v8a/libp12345_http.a ../../../../libs/routerAgent/static/arm64-v8a/libp12345_thread.a -latomic -lm && :
../../../../libs/routerAgent/static/arm64-v8a/libmystaticlib.a: error adding symbols: File format not recognized
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Please notice that I'm not sure whether the version of cmake that I'm using is the same as the one used to build the libraries.
I'm using the the NDK: 21.1.6352462
To investigate the issue I tried some tools that people suggested in similar questions.
With ar I get an error:
ar -x libmystaticlib.a
ar: /: Is a directory
ar: adaptive.c.o/: No such file or directory
ar: coro.c.o/: No such file or directory
ar: curlops.c.o/: No such file or directory
ar: entities.c.o/: No such file or directory
ar: exception.c.o/: No such file or directory
ar: getinfo.c.o/: No such file or directory
ar: helper.c.o/: No such file or directory
ar: metrics.c.o/: No such file or directory
ar: mm_parser.c.o/: No such file or directory
I have also tried with file and objdump that both do not return any information.
Is there a way to know what goes wrong?

error: undefined reference to 'cv::String::deallocate()'

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 "${CMAKE_CXX_FLAGS} -std=gnu++11")
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
"$<$<CONFIG:RELEASE>:-O3>"
"$<$<CONFIG:DEBUG>:-O0>")
target_compile_options(libcodec PRIVATE
"$<$<CONFIG:RELEASE>:-O3>"
"$<$<CONFIG:DEBUG>:-O0>")
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 "opencv2/opencv.hpp" 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 "cd . && 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 && cd ."
D:/OpenCV-android-sdk/sdk/native/jni/include\opencv2/core/cvstd.hpp:648: error: undefined reference to 'cv::String::deallocate()'
D:/OpenCV-android-sdk/sdk/native/jni/include\opencv2/core/cvstd.hpp:656: error: undefined reference to 'cv::String::deallocate()'
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?
If nm (dumpbin.exe for Windows) sees cv::String::deallocate() in libopencv_java3.so:
$ nm -CD libopencv_java3.so | grep cv::String::deallocate
00000000003e3fa0 T cv::String::deallocate()
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.

Using Qt codes in Android application

I have an Android application in Android Studio and I want to use Qt C++ codes in my app. I have used pure C++ codes in my android app using cmake and NDK recently and I could interact with my pure C++ and now I want to use Qt C++ codes.
To do this, I have installed Qt with Android capability. After that I copied Qt5.12.7/5.12.7/android_arm64_v8a/lib/*.so and includes file to my Android App /etc/ folder and I created a simple main.cpp and CMakeLists.txt files as follow:
main.cpp
#include <QString>
#include <QDebug>
int main(int argc, char *argv[])
{
QString test="HELLO WORLD!";
qDebug()<<test;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
include_directories(etc/headerFile)
link_directories(etc/libs)
add_library(po-lib SHARED main.cpp)
target_link_libraries(po-lib -lQt5Core)
As seen in the above files, I want to compile a simple Qt C++ code with dependency on libQtCore.so in my Android App, but after compiling the App, the following errors occur:
Build command failed.
Error while executing process C:\Users\User1\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe with arguments {-C D:\WorkProjects\Horizon\Ga\po\.cxx\cmake\debug\armeabi-v7a po-lib}
ninja: Entering directory `D:\WorkProjects\Horizon\Ga\po\.cxx\cmake\debug\armeabi-v7a'
[1/2] Building CXX object CMakeFiles/po-lib.dir/main.cpp.o
[2/2] Linking CXX shared library D:\WorkProjects\Horizon\Ga\po\build\intermediates\cmake\debug\obj\armeabi-v7a\libpo-lib.so
FAILED: D:/WorkProjects/Horizon/Ga/po/build/intermediates/cmake/debug/obj/armeabi-v7a/libpo-lib.so
cmd.exe /C "cd . && C:\Users\User1\AppData\Local\Android\Sdk\ndk\21.0.6113669\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi21 --gcc-toolchain=C:/Users/User1/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/User1/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libpo-lib.so -o D:\WorkProjects\Horizon\Ga\po\build\intermediates\cmake\debug\obj\armeabi-v7a\libpo-lib.so CMakeFiles/po-lib.dir/main.cpp.o -LD:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/libs_a -LD:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/libs_so -lQt5Core -latomic -lm && cd ."
C:/Users/User1/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin\ld: warning: skipping incompatible D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/libs_so/libQt5Core.so while searching for Qt5Core
C:/Users/User1/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin\ld: error: cannot find -lQt5Core
D:/WorkProjects/Horizon/Ga/po/src/main/cpp/main.cpp:25: error: undefined reference to 'QMessageLogger::debug() const'
D:/WorkProjects/Horizon/Ga/po/src/main/cpp/main.cpp:25: error: undefined reference to 'QDebug::~QDebug()'
D:/WorkProjects/Horizon/Ga/po/src/main/cpp/main.cpp:25: error: undefined reference to 'QDebug::~QDebug()'
D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qstring.h:700: error: undefined reference to 'QString::fromAscii_helper(char const*, int)'
D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qdebug.h:155: error: undefined reference to 'QDebug::putString(QChar const*, unsigned int)'
D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qdebug.h:125: error: undefined reference to 'QTextStream::operator<<(char)'
D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qarraydata.h:59: error: undefined reference to 'qt_assert(char const*, char const*, int)'
D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qarraydata.h:239: error: undefined reference to 'QArrayData::deallocate(QArrayData*, unsigned int, unsigned int)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I have used both arm64_v8a and armv7 libs in Qt folders but the same errors appear. How can I solve this linker error (incompatible lib)?
Your Qt libraries don't have the same ABI as the binaries you are building (armeabi-v7a). This can be even seen in the error message:
/../../arm-linux-androideabi/bin\ld: warning: skipping incompatible D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/libs_so/libQt5Core.so while searching for Qt5Core
To properly run it either install armeabi-v7a version of Qt and make cmake find it, rather than the arm64-v8a (hard-coding the path is a temporary way to do it).
If you are using NDK's cmake toolchain to compile, you can pass ANDROID_ABI variable to cmake while configuring by -DANDROID_ABI=arm64-v8a.

Android Studio CMakeLists error: undefined reference to

I have this project
I am tring to build C's sources with CMakeLists :
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
rtmp-jni
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${CMAKE_SOURCE_DIR}/src/main/cpp/rtmp-jni.c)
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../jnisource"
"${CMAKE_CURRENT_SOURCE_DIR}/../jnisource/includes"
"${CMAKE_CURRENT_SOURCE_DIR}/../jnisource/includes/openssl"
"${CMAKE_CURRENT_SOURCE_DIR}/../jnisource/librtmp")
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
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 )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
rtmp-jni
# Links the target library to the log library
# included in the NDK.
${log-lib} )
When i build my APK, i have this error :
Build command failed.
Error while executing process /Users/kevinabrioux/android-sdks/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target rtmp-jni}
[1/1] Linking C shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/librtmp-jni.so
FAILED: : && /Users/kevinabrioux/android-sdks/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang --target=armv7-none-linux-androideabi --gcc-toolchain=/Users/kevinabrioux/android-sdks/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/kevinabrioux/android-sdks/ndk-bundle/sysroot -fPIC -isystem /Users/kevinabrioux/android-sdks/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a --sysroot /Users/kevinabrioux/android-sdks/ndk-bundle/platforms/android-21/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,librtmp-jni.so -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/librtmp-jni.so CMakeFiles/rtmp-jni.dir/src/main/cpp/rtmp-jni.c.o -llog -lm && :
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:14: error: undefined reference to 'RTMP_Alloc'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:20: error: undefined reference to 'RTMP_Init'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:21: error: undefined reference to 'RTMP_SetupURL'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:26: error: undefined reference to 'RTMP_Connect'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:26: error: undefined reference to 'RTMP_ConnectStream'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:35: error: undefined reference to 'RTMP_Close'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:36: error: undefined reference to 'RTMP_Free'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:54: error: undefined reference to 'RTMP_Read'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:75: error: undefined reference to 'RTMP_Pause'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:84: error: undefined reference to 'RTMP_IsConnected'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:93: error: undefined reference to 'RTMP_IsTimedout'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:99: error: undefined reference to 'RTMP_Close'
/Users/kevinabrioux/Documents/Android-video/ReadyPlayerOne/app/src/main/cpp/rtmp-jni.c:100: error: undefined reference to 'RTMP_Free'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
What can i do to make my project to find reference on these objects ?
Your target_link_libraries must include librtmp. You don't need to build it as part of your project. It's perfectly Ok to make it once using the make files that come with this library, and reference it as IMPORTED, like
add_library(shared-lib SHARED IMPORTED)
set_target_properties(shared-lib PROPERTIES IMPORTED_LOCATION
${CMAKE_CURRENT_SOURCE_DIR}/../jnisource/librtmp/lib/${ANDROID_ABI}/librtmp.so)

Categories

Resources