I am trying to build C++ NDK lib inside Android Studio.I have an external lib called json and the Clang compiler faild on localeconv.
json.hpp:11867: error: undefined reference to 'localeconv'
The locale.h header exists and located inside ndk dir sysroot/usr/include.
My toolchain looks like this:
Gradle: (Showing only the part relevant to NDK)
externalNativeBuild {
cmake {
arguments "-DANDROID_PLATFORM_LEVEL=${platformVersion}",
'-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_static'
}
ndk {
abiFilters 'armeabi-v7a'
}
}
Cmake
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -frtti -fexceptions -Wall")
//'native-lib' is the final .so that's packaged into apk
target_link_libraries(native-lib
OPENAL
FREETYPE
android
EGL
${OPENGL_LIB}
log
m
z
atomic
gnustl_static
)
And here is the linker command line:
[1/1] Linking CXX shared library
........\build\intermediates\cmake\debug\obj\armeabi-v7a\libnative-lib.so
FAILED: cmd.exe /C "cd . &&
D:\Android\android-sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe
--target=armv7-none-linux-androideabi --gcc-toolchain=D:/Android/android-sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
--sysroot=D:/Android/android-sdk/ndk-bundle/sysroot -fPIC -isystem D:/Android/android-sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi
-D__ANDROID_API__=19 -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 -std=c++11 -frtti -fexceptions -Wall -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a --sysroot D:/Android/android-sdk/ndk-bundle/platforms/android-19/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,libnative-lib.so
The complete error:
"D:/Android/android-sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a"
&& cd ." D:\XXXXX\XXXXXX\windows....\thirdparty\json/json.hpp:11867:
error: undefined reference to 'localeconv' clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed. :app:externalNativeBuildDebug
FAILED
The target SDK is 21.Minimum SDK is 19.NDK Version: 15.0.4075724
The same code base is compiled fine in Visual Studio Android project which uses the same toolchain.
The answer is - NDK version for SDK 19 doesn't implement the whole C++11 standard in the STL. locale.h header has stubs for localeconv() method, but the library doesn't implement it.The closest Android SDK that implement localeconv() is SDK 21.This is implicitly stated in the header <locale.h>
struct lconv* localeconv(void) __INTRODUCED_IN(21) __VERSIONER_NO_GUARD;
#if __ANDROID_API__ >= 21
locale_t duplocale(locale_t) __INTRODUCED_IN(21);
void freelocale(locale_t) __INTRODUCED_IN(21);
locale_t newlocale(int, const char*, locale_t) __INTRODUCED_IN(21);
#endif /* __ANDROID_API__ >= 21 */
char* setlocale(int, const char*);
#if __ANDROID_API__ >= 21
locale_t uselocale(locale_t) __INTRODUCED_IN(21);
#endif /* __ANDROID_API__ >= 21 */
#define LC_GLOBAL_LOCALE __BIONIC_CAST(reinterpret_cast, locale_t, -1L)
__END_DECLS
#endif /* _LOCALE_H_ */
For me, trying to use Lua 5.4.0, I have to define the API level to 21
Cmake file as:
set(ANDROID_NATIVE_API_LEVEL 21)
I had a similar problem, and unfortunaletly changing API level couldn't work for me --I was working with an old tablet which runs API16. You can try Crystax NDK or you can write an ugly function that mimics the localeconv(). The latter had solved my problem since I was building Lua for Android Terminal Emulator for hobby.
Related
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.
Problem Description:
I am trying to compile OpenCV 4.5.1 on Ubuntu 20.04 LTS for Android (aarch64) with OpenCL support (since the stock OpenCV Android SDK does not have OpenCL support activated). I largely followed this tutorial covering the cross-compilation of OpenCV.
When trying to configure the build, Cmake seems to be unable to correctly create/locate all the necessary folders and thus throws an error (see cmake output below).
I suspect that it might have something to do with Cmake struggling to recognise the ant installation correctly (as indicated by the missing version number following the ant path in the Cmake output).
I have set the environment variables for the ANT executable as well as the Java Home folder through the Cmake build flags (see Cmake command below).
When looking at the error messages at the bottom of the Cmake output, it becomes clear that there is some problem with creating the necessary Android and/or Java folders. Upon inspection of modules/java/android_sdk/CMakeLists.txt (see extract below) I noticed that the variable OPENCV_JAVA_DIR should probably be set at some earlier stage in the cmake configuration. However, when running my command pasted below, this variable remains empty and hence cmake cannot access the folders since the path is not correct.
These are the values of the variables when running my cmake configuration command:
-- OPENCV_JAVA_DIR = /opencv
-- ANDROID_EXECUTABLE = FALSE
-- ANDROID_BUILD_BASE_DIR =
-- OpenCV_BINARY_DIR= /home/flavio/Downloads/opencv/build_manual
Is there some configuration error in my Cmake buildflags or something entirely different that I am doing wrong?
Thanks for you help!
Related Questions
Cmake cannot find ant
Cmake cannot recognise Java
Ant for Java not showing as installed eventhough it is
I have tried all the proposed changes, so far with no success. Reinstallation of ant did not help, neither creating a symlink to the executable store at /usr/share/ant/bin/ant in /usr/bin.
Cmake Command used (in build folder inside the downloaded opencv-source):
cmake \
-DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk/21.0.6113669/build/cmake/android.toolchain.cmake \
-DANDROID_TOOLCHAIN=clang++ \
-DANDROID_ABI=arm64-v8a \
-DANDROID_TARGET_SDK_VERSION=26 \
-DANDROID_MIN_SDK_VERSION=24 \
-DBUILD_JAVA=ON -DBUILD_FAT_JAVA_LIB=ON DBUILD_ZLIB=ON \
-D CMAKE_BUILD_TYPE=Release \
-D ANDROID_NATIVE_API_LEVEL=24 \
-DANDROID_COMPILE_SDK_VERSION=26 \
-D ANDROID_STL=c++_shared \
-D WITH_CUDA=OFF \
-D WITH_MATLAB=OFF \
-D BUILD_ANDROID_EXAMPLES=OFF -D BUILD_DOCS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=ON -D BUILD_SHARED_LIBS=OFF -DBUILD_ANDROID_PROJECTS=OFF -DBUILD_JNI=ON \
-DWITH_OPENCL=ON -DWITH_OPENGL=ON \
-DANT_HOME=/usr/share/ant -DJAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 -DANT_EXECUTABLE=/bin/ant \
-DSTRIP=/home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip \
..
Extract from modules/java/android_sdk/CMakeList.txt:
project(${the_module}_android)
if(ANDROID_EXECUTABLE)
set(OPENCV_JAVA_DIR "${OpenCV_BINARY_DIR}/android_sdk" CACHE INTERNAL "")
else() # gradle
set(OPENCV_JAVA_DIR "${ANDROID_BUILD_BASE_DIR}/opencv" CACHE INTERNAL "")
endif()
set(OPENCV_ANDROID_LIB_DIR "${OPENCV_JAVA_DIR}" CACHE INTERNAL "") # for OpenCV samples
message (STATUS "OPENCV_JAVA_DIR = ${OPENCV_JAVA_DIR}")
message (STATUS "ANDROID_BUILD_BASE_DIR = ${ANDROID_BUILD_BASE_DIR}")
message (STATUS "OpenCV_BINARY_DIR= ${OpenCV_BINARY_DIR}")
file(REMOVE_RECURSE "${OPENCV_JAVA_DIR}")
file(MAKE_DIRECTORY "${OPENCV_JAVA_DIR}/bin")
set(java_src_dir "${OPENCV_JAVA_DIR}/src")
file(MAKE_DIRECTORY "${java_src_dir}")
Cmake terminal output
-- Check for working CXX compiler: /home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++
-- Check for working CXX compiler: /home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- Check for working C compiler: /home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Detected processor: aarch64
CMake Warning at cmake/OpenCVUtils.cmake:752 (message):
Unexpected option: WITH_OPENGL (=ON)
Condition: IF (NOT;ANDROID;AND;NOT;WINRT)
Call Stack (most recent call first):
CMakeLists.txt:308 (OCV_OPTION)
-- Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.18", minimum required is "2.7")
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3.2")
-- Looking for ccache - not found
<---------- Performed Tests have been cut out from this output to meet character-limit of stackoverflow questions ----------->
-- ANDROID: TRUE
-- Update variable ANDROID_SDK_ROOT from environment: /home/flavio/Android/Sdk
-- Update variable ANDROID_HOME from environment: /home/flavio/Android/Sdk
Android: Projects builds are DISABLED
-- Looking for dlerror in dl
-- Looking for dlerror in dl - found
-- ADE: Download: v0.1.1f.zip
-- Allocator metrics storage type: 'int'
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.sse2.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.sse3.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.ssse3.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.sse4_1.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.sse4_2.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.avx.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.avx2.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.avx512_skx.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin256.avx2.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin256.avx512_skx.cpp
-- Excluding from source files list: <BUILD>/modules/core/test/test_intrin512.avx512_skx.cpp
-- Excluding from source files list: modules/imgproc/src/corner.avx.cpp
-- Excluding from source files list: modules/imgproc/src/imgwarp.avx2.cpp
-- Excluding from source files list: modules/imgproc/src/imgwarp.sse4_1.cpp
-- Excluding from source files list: modules/imgproc/src/resize.avx2.cpp
-- Excluding from source files list: modules/imgproc/src/resize.sse4_1.cpp
-- Registering hook 'INIT_MODULE_SOURCES_opencv_dnn': /home/flavio/Downloads/opencv/modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake
-- opencv_dnn: filter out cuda4dnn source code
-- Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx.cpp
-- Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx2.cpp
-- Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx512_skx.cpp
-- Excluding from source files list: modules/features2d/src/fast.avx2.cpp
-- OPENCV_JAVA_DIR = /opencv
-- ANDROID_BUILD_BASE_DIR =
-- OpenCV_BINARY_DIR= /home/flavio/Downloads/opencv/build_manual
CMake Error at modules/java/android_sdk/CMakeLists.txt:15 (file):
file problem creating directory: /opencv/bin
CMake Error at modules/java/android_sdk/CMakeLists.txt:17 (file):
file problem creating directory: /opencv/src
CMake Error: Could not open file for write in copy operation /opencv/AndroidManifest.xml.tmp
CMake Error: : System Error: No such file or directory
CMake Error at modules/java/android_sdk/CMakeLists.txt:121 (configure_file):
configure_file Problem configuring file
CMake Error: Could not open file for write in copy operation /opencv/build.gradle.tmp
CMake Error: : System Error: No such file or directory
CMake Error at modules/java/android_sdk/CMakeLists.txt:121 (configure_file):
configure_file Problem configuring file
CMake Error: Could not open file for write in copy operation /opencv/res/values/attrs.xml.tmp
CMake Error: : System Error: No such file or directory
CMake Error at modules/java/android_sdk/CMakeLists.txt:121 (configure_file):
configure_file Problem configuring file
CMake Error: Could not open file for write in copy operation /opencv/libcxx_helper/CMakeLists.txt.tmp
CMake Error: : System Error: No such file or directory
CMake Error at modules/java/android_sdk/CMakeLists.txt:133 (configure_file):
configure_file Problem configuring file
CMake Error: Could not open file for write in copy operation /opencv/libcxx_helper/dummy.cpp.tmp
CMake Error: : System Error: No such file or directory
CMake Error at modules/java/android_sdk/CMakeLists.txt:133 (configure_file):
configure_file Problem configuring file
CMake Error: Could not open file for write in copy operation /opencv/build.gradle.tmp
CMake Error: : System Error: No such file or directory
CMake Error at modules/java/android_sdk/CMakeLists.txt:160 (configure_file):
configure_file Problem configuring file
-- Android OpenCV Manager is ignored
--
-- General configuration for OpenCV 4.5.1 =====================================
-- Version control: unknown
--
-- Platform:
-- Timestamp: 2022-03-20T09:57:50Z
-- Host: Linux 5.13.0-35-generic x86_64
-- Target: Android 1 aarch64
-- CMake: 3.16.3
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: Release
--
-- CPU/HW features:
-- Baseline: NEON FP16
--
-- C/C++:
-- Built as dynamic libs?: NO
-- C++ standard: 11
-- C++ Compiler: /home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ (ver 9.0)
-- C++ flags (Release): -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O0 -DDEBUG -D_DEBUG
-- C Compiler: /home/flavio/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- C flags (Release): -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG -DNDEBUG
-- C flags (Debug): -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,--as-needed
-- Linker flags (Debug): -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,--as-needed
-- ccache: NO
-- Precompiled headers: NO
-- Extra dependencies: ade dl m log
-- 3rdparty dependencies: libcpufeatures ittnotify libprotobuf zlib libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf quirc tegra_hal
--
-- OpenCV modules:
-- To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc java ml objdetect photo stitching ts video videoio
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: python2 python3
-- Applications: tests
-- Documentation: NO
-- Non-free algorithms: NO
--
-- Android NDK: /home/flavio/Android/Sdk/ndk/21.0.6113669 (ver 21.0.6113669)
-- Android ABI: arm64-v8a
-- NDK toolchain: aarch64-linux-android-4.9
-- STL type: c++_shared
-- Native API level: 24
-- Android SDK: not used, projects are not built
--
-- GUI:
-- OpenGL support: NO
--
-- Media I/O:
-- ZLib: build (ver 1.2.11)
-- JPEG: build-libjpeg-turbo (ver 2.0.6-62)
-- WEBP: build (ver encoder: 0x020f)
-- PNG: build (ver 1.6.37)
-- TIFF: build (ver 42 - 4.0.10)
-- JPEG 2000: build (ver 2.3.1)
-- OpenEXR: build (ver 2.3.0)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Custom HAL: YES (carotene (ver 0.0.1))
-- Protobuf: build (3.5.1)
--
-- OpenCL: YES (no extra features)
-- Include path: /home/flavio/Downloads/opencv/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python (for build): /usr/bin/python2.7
--
-- Java: export all functions
-- ant: /bin/ant (ver )
-- Java wrappers: YES
-- Java tests: NO
--
-- Install to: /home/flavio/Downloads/opencv/build_manual/install
-- -----------------------------------------------------------------
--
-- Configuring incomplete, errors occurred!
See also "/home/flavio/Downloads/opencv/build_manual/CMakeFiles/CMakeOutput.log".
See also "/home/flavio/Downloads/opencv/build_manual/CMakeFiles/CMakeError.log".
I had similar errors, so I fixed it by turning on -DBUILD_ANDROID_PROJECTS=ON and fix all incompatible "SDK TOOL version" or "gradle versions". Then gradle can set all necessary paths like ANDROID_BUILD_BASE_DIR in modules/java/android_sdk/CMakeLists.txt:15.
For example to update SDK Tools: Open android studio > setting > system settings > Android SDK > SDK Tools > update Android SDK build tools and NDK(side by side)
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.
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.
In our application want to integrate OpenCV c/c++ library. I have successfully integrate OpenCV library but when try to build using android NDK r12b getting below build error.
Build command failed.
Error while executing process C:\Users\Dell\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\cmake.exe with arguments {--build D:\Project\WorkSpace\AndroidStudio\Sample\Sample2NativeSample\app\.externalNativeBuild\cmake\debug\x86 --target native-lib}
[0/1] Re-running CMake...
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/.externalNativeBuild/cmake/debug/x86
[1/150] Building CXX object CMakeFiles/native-lib.dir/src/features/RVUPHistogramFeature.cpp.o
FAILED: CMakeFiles/native-lib.dir/src/features/RVUPHistogramFeature.cpp.o
D:\Project\Android\android-ndk-r12b\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe -target i686-none-linux-android -gcc-toolchain D:/Project/Android/android-ndk-r12b/toolchains/x86-4.9/prebuilt/windows-x86_64 --sysroot=D:/Project/Android/android-ndk-r12b/platforms/android-15/arch-x86 -Dnative_lib_EXPORTS -ID:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/include/algorithm -ID:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/include/deps/msvc/sys -ID:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/include/features -ID:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/include/opencv -ID:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/include/opencv2/imgproc -ID:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/include/opencv2/core -ID:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/include/opencv2/ml -ID:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/include/opencv2 -ID:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/include/prediction -ID:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/include/wsq -ID:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/include -isystem D:/Project/Android/android-ndk-r12b/sources/cxx-stl/gnu-libstdc++/4.9/include -isystem D:/Project/Android/android-ndk-r12b/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86/include -isystem D:/Project/Android/android-ndk-r12b/sources/cxx-stl/gnu-libstdc++/4.9/include/backward -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -fexceptions -frtti -frtti -fexceptions -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/native-lib.dir/src/features/RVUPHistogramFeature.cpp.o -MF CMakeFiles\native-lib.dir\src\features\RVUPHistogramFeature.cpp.o.d -o CMakeFiles/native-lib.dir/src/features/RVUPHistogramFeature.cpp.o -c D:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/src/features/RVUPHistogramFeature.cpp
In file included from D:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/src/features/RVUPHistogramFeature.cpp:1:
In file included from D:/Project/WorkSpace/AndroidStudio/Sample/Sample2NativeSample/app/src/main/cpp/include/features\RVUPHistogramFeature.h:4:
D:/Project/Android/android-ndk-r12b/platforms/android-15/arch-x86/usr/include\stdio.h:257:37: `*error: unknown type name '__va_list'; did you mean 'va_list'?*`
int vfprintf(FILE *, const char *, __va_list);
^
D:\Project\Android\android-ndk-r12b\toolchains\llvm\prebuilt\windows-x86_64\bin\..\lib64\clang\3.8.256229\include\stdarg.h:30:27: note: 'va_list' declared here
typedef __builtin_va_list va_list;
Here, check my CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
include_directories(include/algorithm
include/deps/msvc/sys
include/features
include/opencv
include/opencv2/imgproc
include/opencv2/core
include/opencv2/ml
include/opencv2
include/prediction
include)
file(GLOB_RECURSE SOURCES
"src/features/*.cpp"
"src/opencv/core/*.cpp"
"src/opencv/core/*.hpp"
"src/opencv/imgproc/*.cpp"
"src/opencv/imgproc/*.h"
"src/opencv/ml/*.cpp"
"src/opencv/ml/*.hpp"
"src/prediction/*.cpp"
"src/*.cpp"
"src/*.c")
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp
${SOURCES})
find_library( # Sets the name of the path variable.
log-lib
log)
find_package(ZLIB)
target_link_libraries( # Specifies the target library.
native-lib
${log-lib}
${ZLIB_LIBRARIES})
Also check my gradle cmake declare.
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
arguments '-DANDROID_TOOLCHAIN=clang',
'-DANDROID_STL=gnustl_static',
'-DANDROID_CPP_FEATURES=exceptions rtti',
'-DLOCAL_ALLOW_UNDEFINED_SYMBOLS = true'
abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
}
}
I have try many solutions and also try used latest NDK r20 but still getting this error. How can I solved this error?
It's a compiler-specific thing.
As much as I'm aware most compilers would use va_list.