Android OpenCV Linking CXX shared library fails - android

I had a working Android Studio installation where I was programming an app with native C++ code and OpenCV. I stopped for kind of one year the development and resumed it a few days ago.
Since Android Studio was complaining that there were updates available I decided to update everything (Gradle, CMake, NDK...), and this was my error. Now the build stops at the linking phase:
Build command failed.
Error while executing process C:\Users\Marco\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build C:\Users\Marco\AndroidStudioProjects\MyProject\app\.externalNativeBuild\cmake\debug\x86_64 --target CppFilter}
[1/2] Building CXX object CMakeFiles/CppFilter.dir/src/main/cpp/CppFilter.cpp.o
[2/2] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\x86_64\libCppFilter.so
FAILED: cmd.exe /C "cd . && C:\Users\Marco\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=x86_64-none-linux-android21 --gcc-toolchain=C:/Users/Marco/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/Marco/AppData/Local/Android/Sdk/ndk-bundle/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 -stdlib=libc++ -O3 -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 -Wl,-z,relro -Wl,-z,now -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -shared -Wl,-soname,libCppFilter.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\x86_64\libCppFilter.so #CMakeFiles/CppFilter.rsp && cd ."
D:/Downloads/opencv-3.3.1-android-sdk/OpenCV-android-sdk/sdk/native/libs/x86_64/libopencv_core.a(system.cpp.o):system.cpp:function cv::ipp::IPPInitSingleton::IPPInitSingleton(): error: undefined reference to 'std::cerr'
D:/Downloads/opencv-3.3.1-android-sdk/OpenCV-android-sdk/sdk/native/libs/x86_64/libopencv_core.a(system.cpp.o):system.cpp:function cv::ipp::IPPInitSingleton::IPPInitSingleton(): error: undefined reference to 'std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)'
D:/Downloads/opencv-3.3.1-android-sdk/OpenCV-android-sdk/sdk/native/libs/x86_64/libopencv_core.a(system.cpp.o):system.cpp:function cv::ipp::IPPInitSingleton::IPPInitSingleton(): error: undefined reference to 'std::ostream::put(char)'
D:/Downloads/opencv-3.3.1-android-sdk/OpenCV-android-sdk/sdk/native/libs/x86_64/libopencv_core.a(system.cpp.o):system.cpp:function cv::ipp::IPPInitSingleton::IPPInitSingleton(): error: undefined reference to 'std::ostream::flush()'
... many more of the same type ...
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I basically followed the default setup steps explained, for example:
https://android.jlelse.eu/a-beginners-guide-to-setting-up-opencv-android-library-on-android-studio-19794e220f3c
Can OpenCV for Android leverage the standard C++ Support to get native build support on Android Studio 2.2 for Windows?
Any ideas about what can be the cause of this error?
Actual version of Android Studio: 3.4, former: 2.something

Apparently it was due to the OpenCV version (3.3.1) being compiled in a way that was not compatible with newer versions of NDK or the Android SDK Build tools, check for example: Why am I still getting undefined reference errors linking a static library with CMake in Android NDK?
I downgraded NDK and Android SDK Build tools. Now it's working.

Related

Android native library linker stops working after gradle update

My project stops working after updating gradle.
I'm using a C library in Android though JNI.
Using Cmake to compile and join the libraries
Before, it compiled and linked without a problem, but now there is an error at link time when building the project.
It throw this error:
/home/my_user/Documents/Proyects/optandroid/app/src/main/cpp/kotlin-jni.c:152: error: undefined reference to 'find_best_order'
This is an extended error message that android studio also gives after trying to build the project:
> Task :app:externalNativeBuildDebug FAILED
Build multiple targets main_armeabi-v7a this_library_armeabi-v7a process_control_armeabi-v7a
ninja: Entering directory `/home/my_user/Documents/Proyects/optandroid/app/.cxx/cmake/debug/armeabi-v7a'
[1/1] Linking C shared library /home/my_user/Documents/Proyects/optandroid/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libthis_library.so
FAILED: /home/my_user/Documents/Proyects/optandroid/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libthis_library.so
: && /home/my_user/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=armv7-none-linux-androideabi19 --gcc-toolchain=/home/my_user/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/my_user/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-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.a -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,libthis_library.so -o /home/my_user/Documents/Proyects/optandroid/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libthis_library.so CMakeFiles/this_library.dir/kotlin-jni.c.o used_library/src/libmodule_used_library.a /home/my_user/Documents/Proyects/optandroid/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libprocess_control.so -latomic -lm && :
/home/my_user/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: used_library/src/libmodule_used_library.a: member at 3324 is not an ELF object
/home/my_user/Documents/Proyects/optandroid/app/src/main/cpp/kotlin-jni.c:152: error: undefined reference to 'find_best_order'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing process ninja with arguments {-C /home/my_user/Documents/Proyects/optandroid/app/.cxx/cmake/debug/armeabi-v7a main this_library process_control}
ninja: Entering directory `/home/my_user/Documents/Proyects/optandroid/app/.cxx/cmake/debug/armeabi-v7a'
[1/1] Linking C shared library /home/my_user/Documents/Proyects/optandroid/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libthis_library.so
FAILED: /home/my_user/Documents/Proyects/optandroid/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libthis_library.so
: && /home/my_user/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=armv7-none-linux-androideabi19 --gcc-toolchain=/home/my_user/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/my_user/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-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.a -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,libthis_library.so -o /home/my_user/Documents/Proyects/optandroid/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libthis_library.so CMakeFiles/this_library.dir/kotlin-jni.c.o used_library/src/libmodule_used_library.a /home/my_user/Documents/Proyects/optandroid/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libprocess_control.so -latomic -lm && :
/home/my_user/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: used_library/src/libmodule_used_library.a: member at 3324 is not an ELF object
/home/my_user/Documents/Proyects/optandroid/app/src/main/cpp/kotlin-jni.c:152: error: undefined reference to 'find_best_order'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
This is the CmakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(Optandroid)
set(CMAKE_C_STANDARD 99)
add_subdirectory(used_library)
add_library(this_library
SHARED
kotlin-jni.c
)
# Include libraries needed for kotlin-jni lib
target_link_libraries(this_library
process_library
android
log)
used_library is the library directory, it builds the library process_library, and this_library it is what I use for JNI.
This is a pure C project, nothing of C++, so it is not the problem of C++ changing the names of functions.
I corroborated that the problem starts by updating gradle in the project by taking an older proyect which use this library in exactly the same way, and just making the update, it works before, but has the exactly same error after.
Thanks in advance.
At the end the problem was the NDK version. I just updated NDK to the newest version and started worked like before.

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 Native: CMake linking error: undefined reference to GL functions - even though EGL & GLESv3 included and linked

I am trying to update a GitHub repo, which is a basic OpenGL rendered cube, to a current Android version and project structure. The CMake script seems to be missing something, which I can't figure out. Is it a simple issue of lines in the wrong order?
System: Arch Linux - Kernel 5.0.9, Android Studio 3.5 Canary 13, CMake 3.14.3, Ninja 1.9.0
I created a new Native project in Android Studio and filled in the configurations with the old data. You can see the progress here. I am stuck at trying to link the GL function libraries to the .cpp files, as the terminal output tells me there are undefined references to certain gl... functions.
Terminal output:
Build command failed.
Error while executing process /usr/bin/ninja with arguments {-C /home/snobo/projects/AndroidNativeExample/app/.cxx/cmake/debug/armeabi-v7a nativeegl}
ninja: Entering directory `/home/snobo/projects/AndroidNativeExample/app/.cxx/cmake/debug/armeabi-v7a'
[1/2] Building CXX object CMakeFiles/nativeegl.dir/renderer.cpp.o
[2/2] Linking CXX shared library /home/snobo/projects/AndroidNativeExample/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnativeegl.so
FAILED: /home/snobo/projects/AndroidNativeExample/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnativeegl.so
: && /home/snobo/Android/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi28 --gcc-toolchain=/home/snobo/Android/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/snobo/Android/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mfpu=vfpv3-d16 -fno-addrsig -march=armv7-a -mthumb -mfpu=neon -Wa,--noexecstack -Wformat -Werror=format-security -stdlib=libc++ -std=c++14 -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,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnativeegl.so -o /home/snobo/projects/AndroidNativeExample/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnativeegl.so CMakeFiles/nativeegl.dir/jniapi.cpp.o CMakeFiles/nativeegl.dir/renderer.cpp.o /home/snobo/Android/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/28/liblog.so -landroid -lGLESv3 -lEGL -latomic -lm && :
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:230: error: undefined reference to 'glShadeModel'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:236: error: undefined reference to 'glMatrixMode'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:237: error: undefined reference to 'glLoadIdentity'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:238: error: undefined reference to 'glFrustumf'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:262: error: undefined reference to 'glMatrixMode'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:263: error: undefined reference to 'glLoadIdentity'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:264: error: undefined reference to 'glTranslatef'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:265: error: undefined reference to 'glRotatef'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:266: error: undefined reference to 'glRotatef'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:268: error: undefined reference to 'glEnableClientState'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:269: error: undefined reference to 'glEnableClientState'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:272: error: undefined reference to 'glVertexPointer'
/home/snobo/projects/AndroidNativeExample/app/src/main/jni/renderer.cpp:273: error: undefined reference to 'glColorPointer'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Here is one of the methods that includes the errors from above:
void Renderer::drawFrame()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -3.0f);
glRotatef(_angle, 0, 1, 0);
glRotatef(_angle*0.25f, 1, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glFrontFace(GL_CW);
glVertexPointer(3, GL_FIXED, 0, vertices);
glColorPointer(4, GL_FIXED, 0, colors);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
_angle += 1.2f;
}
CMakeLists.txt:
project(AndroidNativeExample C CXX)
cmake_minimum_required(VERSION 3.4.1)
add_library(
nativeegl
SHARED
jniapi.cpp renderer.cpp)
include_directories(src/main/cpp/include/)
find_library( # Sets the name of the path variable.
log-lib
log)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
target_link_libraries(nativeegl Threads::Threads)
add_library(app-glue
STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
target_link_libraries(nativeegl ${app-glue})
target_link_libraries(nativeegl ${log-lib})
target_link_libraries(nativeegl android)
target_link_libraries(nativeegl GLESv3)
target_link_libraries(nativeegl EGL)
To my knowledge (which indeed is quite limited) this should link all necessary libraries and not result in an error. But somehow a few function references are undefined.
Please let me know if you have any ideas, I will try each and give feedback.
The missing functions glShadeModel etc are OpenGL functions and are not available in OpenGL ES.
As your project managed to compile (and failed when linking) you must be including OpenGL headers from another source instead of the OpenGL ES header from the Android NDK.

Android Studio Cmake: "The C compiler clang.exe is not able to compile a simple test program

64 bit Windows 10. Android Studio 2.3.3.
I'm trying to use JNI to link an existing C++ project to Android Studio. I've also tried to load all of the provided NDK-JNI sample projects. Everything with JNI/Cmake is unable to Gradle Sync, failing with the same error:
Build command failed.
Error while executing process C:\Users\Alex\AppData\Local\Android\Sdk\cmake\3.6.3155560\bin\cmake.exe with arguments {-HC:\Users\Alex\Desktop\android-ndk\hello-jni\app\src\main\cpp -BC:\Users\Alex\Desktop\android-ndk\hello-jni\app\.externalNativeBuild\cmake\armDebug\armeabi -GAndroid Gradle - Ninja -DANDROID_ABI=armeabi -DANDROID_NDK=C:\Users\Alex\AppData\Local\Android\Sdk\ndk-bundle -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\Users\Alex\Desktop\android-ndk\hello-jni\app\build\intermediates\cmake\arm\debug\obj\armeabi -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=C:\Users\Alex\AppData\Local\Android\Sdk\cmake\3.6.3155560\bin\ninja.exe -DCMAKE_TOOLCHAIN_FILE=C:\Users\Alex\AppData\Local\Android\Sdk\ndk-bundle\build\cmake\android.toolchain.cmake -DANDROID_PLATFORM=android-23 -DANDROID_TOOLCHAIN=clang}
-- Check for working C compiler: C:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: C:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe -- broken
CMake Error at C:/Users/Alex/AppData/Local/Android/sdk/cmake/3.6.3155560/share/cmake-3.6/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler
"C:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/Alex/Desktop/android-ndk/hello-jni/app/.externalNativeBuild/cmake/armDebug/armeabi/CMakeFiles/CMakeTmp
Run Build
Command:"C:\Users\Alex\AppData\Local\Android\Sdk\cmake\3.6.3155560\bin\ninja.exe"
"cmTC_accda"
[1/2] Building C object CMakeFiles/cmTC_accda.dir/testCCompiler.c.o
[2/2] Linking C executable cmTC_accda
FAILED: cmd.exe /C "cd . &&
C:\Users\Alex\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe
--target=armv5te-none-linux-androideabi
--gcc-toolchain=C:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
--sysroot=C:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/sysroot
-isystem
C:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi
-D__ANDROID_API__=23 -g -DANDROID -ffunction-sections -funwind-tables
-fstack-protector-strong -no-canonical-prefixes -march=armv5te
-Werror=format-security -Wl,--exclude-libs,libgcc.a --sysroot
C:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/platforms/android-23/arch-arm64
-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,--gc-sections -Wl,-z,nocopyreloc -pie -fPIE
CMakeFiles/cmTC_f48b1.dir/testCCompiler.c.o -o cmTC_f48b1 -lm && cd ."
The system cannot find the path specified.
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
I have all necessary SDK tools installed. I've tried using GCC instead of clang. Thanks for your help!
The main error message is:
The system cannot find the path specified.
Note that above some places use / as path separator:
"C:/Users/Alex/AppData/Local/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"
and have run successfully. And this line:
C:\Users\Alex\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe
Seems to have failed. I guess the problem is the \. Try find where this path is defined, maybe in the IDE options, configuration files or Makefiles, then modify it may solve the problem.
Its because of clang version mismatch
check clang version you have and what minimum clang version Android Application needed!

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

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".

Categories

Resources