I'm trying to use Vulkan on Android so I had installed android ndk. I saw that in Android Studio default native C++, Vulkan is included. But whenever I build I get the following error:
C:/Users/a18/AndroidStudioProjects/vulkanSMD/app/src/main/cpp/native-lib.cpp:34:
error: undefined reference to 'vkCreateInstance' clang++: error:
linker command failed with exit code 1 (use -v to see invocation)
I've tried including libvulkan.so and also tried dlopen("libvulkan.so", flags) but it is of no use.
I'm trying to get Vulkan working on android. The below code creates a vulkan instance.
#include <jni.h>
#include <string>
#include <vector>
#define VK_USE_PLATFORM_ANDROID_KHR 1
#include <vulkan/vulkan.h>
std::vector<const char *>extensions;
std::vector<const char *>layers;
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_vulkansmd_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
extensions = std::vector<const char *>({
"VK_EXT_debug_report",
"VK_KHR_surface",
});
layers = std::vector<const char *>({"VK_LAYER_LUNARG_standard_validation"});
VkInstanceCreateInfo createInfo = {};
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Vulkan is hard";
appInfo.applicationVersion = VK_VERSION_1_0;
appInfo.engineVersion = VK_VERSION_1_0;
appInfo.apiVersion = VK_VERSION_1_0 ;
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
createInfo.ppEnabledExtensionNames = extensions.data();
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
createInfo.ppEnabledLayerNames = layers.data();
createInfo.enabledLayerCount = static_cast<uint32_t>(layers.size());
VkInstance instance;
vkCreateInstance(&createInfo, nullptr, &instance);
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
While building however I get the following error
Build command failed. Error while executing process
C:\Users\a18\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\cmake.exe
with arguments {--build
C:\Users\a18\AndroidStudioProjects\vulkanSMD\app.externalNativeBuild\cmake\debug\x86_64
--target native-lib} [1/2] Building CXX object CMakeFiles/native-lib.dir/native-lib.cpp.o [2/2] Linking CXX shared
library
C:\Users\a18\AndroidStudioProjects\vulkanSMD\app\build\intermediates\cmake\debug\obj\x86_64\libnative-lib.so
FAILED:
C:/Users/a18/AndroidStudioProjects/vulkanSMD/app/build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so
cmd.exe /C "cd . &&
C:\Users\a18\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe
--target=x86_64-none-linux-android26 --gcc-toolchain=C:/Users/a18/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64
--sysroot=C:/Users/a18/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 -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,libnative-lib.so -o C:\Users\a18\AndroidStudioProjects\vulkanSMD\app\build\intermediates\cmake\debug\obj\x86_64\libnative-lib.so
CMakeFiles/native-lib.dir/native-lib.cpp.o -llog -latomic -lm && cd
."
C:/Users/a18/AndroidStudioProjects/vulkanSMD/app/src/main/cpp/native-lib.cpp:34:
error: undefined reference to 'vkCreateInstance' clang++: error:
linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Related
I'm trying to build a VINS app on android platform, and I'm facing some library linking error. Seems it could not find gomp, but it's included in the target target_link_libraries in the CMakeLists.txt.
Could anyone provide me any hints on solving this problem? Many thanks!
Execution failed for task ':app:buildCMakeRelease'.
> Build command failed.
Error while executing process /Users/sue/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /Users/sue/Documents/Vins-On-Android-master4/Vins-Droidcv310/app/.cxx/Release/l4h4jp5k/arm64-v8a native-lib}
ninja: Entering directory `/Users/sue/Documents/Vins-On-Android-master4/Vins-Droidcv310/app/.cxx/Release/l4h4jp5k/arm64-v8a'
[1/1] Linking CXX shared library ../../../../build/intermediates/cxx/Release/l4h4jp5k/obj/arm64-v8a/libnative-lib.so
FAILED: ../../../../build/intermediates/cxx/Release/l4h4jp5k/obj/arm64-v8a/libnative-lib.so
: && /Users/sue/Library/Android/sdk/ndk/18.1.5063045/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/Users/sue/Library/Android/sdk/ndk/18.1.5063045/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/sue/Library/Android/sdk/ndk/18.1.5063045/sysroot -fPIC -isystem /Users/sue/Library/Android/sdk/ndk/18.1.5063045/sysroot/usr/include/aarch64-linux-android -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -frtti -fexceptions -std=c++0x -O2 -DNDEBUG -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot /Users/sue/Library/Android/sdk/ndk/18.1.5063045/platforms/android-21/arch-arm64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -L/Users/sue/Library/Android/sdk/ndk/18.1.5063045/sources/cxx-stl/llvm-libc++/libs/arm64-v8a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ../../../../build/intermediates/cxx/Release/l4h4jp5k/obj/arm64-v8a/libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/estimator_node.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/parameters.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/estimator.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/feature_manager.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/factor/pose_local_parameterization.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/factor/projection_factor.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/factor/marginalization_factor.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/utility/utility.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/utility/visualization.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/initial/solve_5pts.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/initial/initial_aligment.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/initial/initial_sfm.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/initial/initial_ex_rotation.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/loop-closure/ThirdParty/DBoW/BowVector.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/loop-closure/ThirdParty/DBoW/FBrief.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/loop-closure/ThirdParty/DBoW/FeatureVector.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/loop-closure/ThirdParty/DBoW/QueryResults.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/loop-closure/ThirdParty/DBoW/ScoringObject.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/loop-closure/ThirdParty/DUtils/Random.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/loop-closure/ThirdParty/DUtils/Timestamp.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/loop-closure/ThirdParty/DVision/BRIEF.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/loop-closure/ThirdParty/VocabularyBinary.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/loop-closure/loop_closure.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/loop-closure/keyframe.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/loop-closure/keyframe_database.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/feature_tracker/feature_tracker.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/camera_model/src/chessboard/Chessboard.cc.o CMakeFiles/native-lib.dir/src/main/cpp/src/camera_model/src/calib/CameraCalibration.cc.o CMakeFiles/native-lib.dir/src/main/cpp/src/camera_model/src/camera_models/Camera.cc.o CMakeFiles/native-lib.dir/src/main/cpp/src/camera_model/src/camera_models/CameraFactory.cc.o CMakeFiles/native-lib.dir/src/main/cpp/src/camera_model/src/camera_models/CostFunctionFactory.cc.o CMakeFiles/native-lib.dir/src/main/cpp/src/camera_model/src/camera_models/PinholeCamera.cc.o CMakeFiles/native-lib.dir/src/main/cpp/src/camera_model/src/camera_models/CataCamera.cc.o CMakeFiles/native-lib.dir/src/main/cpp/src/camera_model/src/camera_models/EquidistantCamera.cc.o CMakeFiles/native-lib.dir/src/main/cpp/src/camera_model/src/camera_models/ScaramuzzaCamera.cc.o CMakeFiles/native-lib.dir/src/main/cpp/src/camera_model/src/sparse_graph/Transform.cc.o CMakeFiles/native-lib.dir/src/main/cpp/src/camera_model/src/gpl/gpl.cc.o CMakeFiles/native-lib.dir/src/main/cpp/src/camera_model/src/gpl/EigenQuaternionParameterization.cc.o CMakeFiles/native-lib.dir/src/main/cpp/src/a2ir/log_util.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/a2ir/phone_sensor.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/a2ir/timer.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/draw_result/delaunay.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/draw_result/draw_result.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/src/vins_estimator/src/draw_result/triangle.cpp.o ../../../../src/main/jniLibs/arm64-v8a/libopencv_java3.so ../../../../src/main/jniLibs/arm64-v8a/libopencv_calib3d.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_core.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_features2d.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_flann.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_highgui.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_imgcodecs.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_imgproc.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_ml.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_objdetect.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_photo.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_shape.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_stitching.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_superres.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_ts.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_video.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_videoio.a ../../../../src/main/jniLibs/arm64-v8a/libopencv_videostab.a ../../../../src/main/jniLibs/arm64-v8a/libceres.a ../../../../src/main/jniLibs/arm64-v8a/libboost_filesystem-gcc-mt-1_53.a ../../../../src/main/jniLibs/arm64-v8a/libboost_program_options-gcc-mt-1_53.a ../../../../src/main/jniLibs/arm64-v8a/libboost_system-gcc-mt-1_53.a -landroid -llog -lgomp -latomic -lm "/Users/sue/Library/Android/sdk/ndk/18.1.5063045/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so" && :
/Users/sue/Library/Android/sdk/ndk/18.1.5063045/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: cannot find -lgomp
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I downloaded a libceres.a from others and put into the jinLibs in the project, then I'm facing the error of Linking CXX shared library.
I'm trying to get byte data of png file for GLES texture.
I have my png file in app\source\main\assets.
Texture::Texture(const char *path) {
AAsset* asset = loadAsset(path);
AImageDecoder* decoder;
int result = AImageDecoder_createFromAAsset(asset, &decoder);
if(result != ANDROID_IMAGE_DECODER_SUCCESS){
__android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor create decode FAIL. path: &s", path);
}
const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
this->width = AImageDecoderHeaderInfo_getWidth(info);
this->height = AImageDecoderHeaderInfo_getHeight(info);
size_t stride = AImageDecoder_getMinimumStride(decoder);
size_t size = height * stride;
void* pixels = malloc(size);
result = AImageDecoder_decodeImage(decoder, pixels, stride, size);
if(result != ANDROID_IMAGE_DECODER_SUCCESS){
__android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor decode image FAIL. path: &s", path);
}
//GLES create texture and bind data(pixels)
free(pixels);
AAsset_close(asset);
}
but I got following build errors:
Build command failed.
Error while executing process C:\Users\jaewo\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe with arguments {-C C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\.cxx\cmake\debug\x86 native-lib}
ninja: Entering directory `C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\.cxx\cmake\debug\x86'
[1/2] Building CXX object CMakeFiles/native-lib.dir/Texture.cpp.o
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:20:122: warning: data argument not used by format string [-Wformat-extra-args]
__android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor create decode FAIL. path: &s", path);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:32:121: warning: data argument not used by format string [-Wformat-extra-args]
__android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor decode image FAIL. path: &s", path);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
2 warnings generated.
[2/2] Linking CXX shared library C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\build\intermediates\cmake\debug\obj\x86\libnative-lib.so
FAILED: C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/build/intermediates/cmake/debug/obj/x86/libnative-lib.so
cmd.exe /C "cd . && C:\Users\jaewo\AppData\Local\Android\Sdk\ndk\21.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=i686-none-linux-android30 --gcc-toolchain=C:/Users/jaewo/AppData/Local/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/jaewo/AppData/Local/Android/Sdk/ndk/21.4.7075529/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 -Wformat -Werror=format-security -std=c++17 -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,--no-undefined -Qunused-arguments -shared -Wl,-soname,libnative-lib.so -o C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\build\intermediates\cmake\debug\obj\x86\libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o CMakeFiles/native-lib.dir/AssetManager.cpp.o CMakeFiles/native-lib.dir/Texture.cpp.o -llog -landroid -lGLESv3 box2d/libbox2d.a -latomic -lm && cd ."
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:18: error: undefined reference to 'AImageDecoder_createFromAAsset'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:23: error: undefined reference to 'AImageDecoder_getHeaderInfo'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:24: error: undefined reference to 'AImageDecoderHeaderInfo_getWidth'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:25: error: undefined reference to 'AImageDecoderHeaderInfo_getHeight'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:27: error: undefined reference to 'AImageDecoder_getMinimumStride'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:30: error: undefined reference to 'AImageDecoder_decodeImage'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:35: error: undefined reference to 'AImageDecoder_delete'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
in app build.gradle file, I set minSDKVersion to 30.
I think I should link a library about ImageDecoder or do something in my CMakeLists.txt.
but I'm not sure.
target_link_libraries(
native-lib
log
android
GLESv3
box2d)
You need to link jnigraphics.
For example:
target_link_libraries(
native-lib
log
android
GLESv3
box2d
jnigraphics)
There's an example at the image-decoder sample project.
I am trying to build a native library in my Android Studio project that uses the std::bad_function_call function.
Here's the CMakeLists.txt file I use to build my native module:
cmake_minimum_required(VERSION 3.4.1)
include_directories(src/main/cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -D_LIBCPP_ABI_UNSTABLE")
add_library(
MyLib
SHARED
src/main/cpp/test.cpp
)
and my test.cpp contains the following code:
void SomeClass::SomeMethod() {
std::function<int()> f = nullptr;
try {
f();
} catch (const std::bad_function_call& e) {
}
}
with the corresponding build.gradle section:
externalNativeBuild {
cmake {
arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_STL=c++_static", "-DANDROID_NDK_ROOT=${android.ndkDirectory}"
}
}
If I pass the _LIBCPP_ABI_UNSTABLE flag to the compiler I get a link error:
Execution failed for task ':runtime:externalNativeBuildRelease'.
> Build command failed.
Error while executing process C:\android-sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build C:\work\android-runtime\test-app\runtime\.externalNativeBuild\cmake\release\armeabi-v7a --target MyLib}
[1/1] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\release\obj\armeabi-v7a\libMyLib.so
FAILED: cmd.exe /C "cd . && C:\android-sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi17 --gcc-toolchain=C:/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 -fPIC --sysroot C:/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mfpu=vfpv3-d16 -fno-addrsig -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -stdlib=libc++ -fno-rtti -D_LIBCPP_ABI_UNSTABLE -Oz -DNDEBUG -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,libMyLib.so -o ..\..\..\..\build\intermediates\cmake\release\obj\armeabi-v7a\libMyLib.so CMakeFiles/MyLib.dir/src/main/cpp/test.cpp.o ../../../../src/main/libs/armeabi-v7a/abc.a -llog -latomic -lm && cd ."
C:/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/functional:0: error: undefined reference to 'vtable for std::__ndk1::bad_function_call'
C:/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin\ld: the vtable symbol may be undefined because the class is missing its key function
C:/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/functional:0: error: undefined reference to 'std::__ndk1::bad_function_call::~bad_function_call()'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
If I remove the _LIBCPP_ABI_UNSTABLE flag:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
then the project compiles fine.
So my question is what would be the proper way to enable the libc++ unstable abi and use the bad_function_call method inside my native module?
Note: I am using Android NDK 19.
I've been trying to cross-compile ffmpeg for different Android cpu archs for couple of days now and I have finally succeeded in the task. But now that I need to integrate these pre-built .so files in my project I'm facing errors which have me baffled.
This is the CMakeLists.txt which I'm using:
cmake_minimum_required(VERSION 3.4.1)
# convert SDK path to forward slashes on Windows
file(TO_CMAKE_PATH ${PATH_TO_SUPERPOWERED} PATH_TO_SUPERPOWERED)
set(CMAKE_VERBOSE_MAKEFILE on)
include_directories(src/main/cpp)
include_directories(${PATH_TO_SUPERPOWERED})
include_directories(${PATH_TO_FFMPEG}/${ANDROID_ABI}/include)
add_library(
avutil
SHARED
${PATH_TO_FFMPEG}/${ANDROID_ABI}/lib/libavutil.so
)
set_target_properties(avutil PROPERTIES LINKER_LANGUAGE CXX)
add_library(
avformat
SHARED
${PATH_TO_FFMPEG}/${ANDROID_ABI}/lib/libavformat.so
)
set_target_properties(avformat PROPERTIES LINKER_LANGUAGE CXX)
add_library(
avcodec
SHARED
${PATH_TO_FFMPEG}/${ANDROID_ABI}/lib/libavcodec.so
)
set_target_properties(avcodec PROPERTIES LINKER_LANGUAGE CXX)
add_library(
Canto
SHARED
src/main/cpp/Dubsmash.cpp
src/main/cpp/Karaoke.cpp
src/main/cpp/Singing.cpp
src/main/cpp/EditDubsmash.cpp
${PATH_TO_SUPERPOWERED}/AndroidIO/SuperpoweredAndroidAudioIO.cpp
)
# link the native library against the following libraries
target_link_libraries(
Canto
avutil
avformat
avcodec
${PATH_TO_SUPERPOWERED}/libSuperpoweredAndroid${ANDROID_ABI}.a
OpenSLES
log
android
)
And this is the source of the file I'm getting errors on:
#include <jni.h>
#include <android/log.h>
#include <string>
// unrelated includes
#include <libavformat/avformat.h>
#include <libavutil/dict.h>
#define log_write __android_log_write
#define log_print __android_log_print
// unrelated code parts
int print_file_data(const char *filePath) {
AVFormatContext *fmt_ctx = NULL;
AVDictionaryEntry *tag = NULL;
int ret;
if ((ret = avformat_open_input(&fmt_ctx, filePath, NULL, NULL)))
return ret;
while ((tag = av_dict_get(fmt_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
log_print(ANDROID_LOG_DEBUG, "%s=%s\n", tag->key, tag->value);
avformat_close_input(&fmt_ctx);
return 0;
}
And finally the errors themselves:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing process /home/hamed/dev/android-tools/android-sdk-linux/cmake/3.6.4111459/bin/cmake with arguments {--build /home/hamed/dev/projects/canto/Canto/app/.externalNativeBuild/cmake/debug/x86 --target Canto}
[1/1] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/x86/libCanto.so
FAILED: : && /home/hamed/dev/android-tools/android-sdk-linux/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=i686-none-linux-android16 --gcc-toolchain=/home/hamed/dev/android-tools/android-sdk-linux/ndk-bundle/toolchains/x86-4.9/prebuilt/linux-x86_64 --sysroot=/home/hamed/dev/android-tools/android-sdk-linux/ndk-bundle/sysroot -fPIC -isystem /home/hamed/dev/android-tools/android-sdk-linux/ndk-bundle/sysroot/usr/include/i686-linux-android -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -fsigned-char -I/home/hamed/dev/projects/canto/Canto/app/../Superpowered -I/home/hamed/dev/projects/canto/Canto/app/../ffmpeg -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot /home/hamed/dev/android-tools/android-sdk-linux/ndk-bundle/platforms/android-16/arch-x86 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -L/home/hamed/dev/android-tools/android-sdk-linux/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/x86 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libCanto.so -o ../../../../build/intermediates/cmake/debug/obj/x86/libCanto.so CMakeFiles/Canto.dir/src/main/cpp/Dubsmash.cpp.o CMakeFiles/Canto.dir/src/main/cpp/Karaoke.cpp.o CMakeFiles/Canto.dir/src/main/cpp/Singing.cpp.o CMakeFiles/Canto.dir/src/main/cpp/EditDubsmash.cpp.o CMakeFiles/Canto.dir/home/hamed/dev/projects/canto/Canto/Superpowered/AndroidIO/SuperpoweredAndroidAudioIO.cpp.o ../../../../build/intermediates/cmake/debug/obj/x86/libavutil.so ../../../../build/intermediates/cmake/debug/obj/x86/libavformat.so ../../../../../Superpowered/libSuperpoweredAndroidx86.a -lOpenSLES -llog -landroid -latomic -lm "/home/hamed/dev/android-tools/android-sdk-linux/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/x86/libc++_static.a" "/home/hamed/dev/android-tools/android-sdk-linux/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/x86/libc++abi.a" "/home/hamed/dev/android-tools/android-sdk-linux/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/x86/libandroid_support.a" && :
/home/hamed/dev/projects/canto/Canto/app/src/main/cpp/Dubsmash.cpp:295: error: undefined reference to 'avformat_open_input(AVFormatContext**, char const*, AVInputFormat*, AVDictionary**)'
/home/hamed/dev/projects/canto/Canto/app/src/main/cpp/Dubsmash.cpp:298: error: undefined reference to 'av_dict_get(AVDictionary const*, char const*, AVDictionaryEntry const*, int)'
/home/hamed/dev/projects/canto/Canto/app/src/main/cpp/Dubsmash.cpp:301: error: undefined reference to 'avformat_close_input(AVFormatContext**)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Obviously the linker can not find the needed libraries, but why?!
UPDATE
This is the result of running nm on libavutil.so:
0000000000000510 t atexit
0000000000000500 t __atexit_handler_wrapper
0000000000002008 A __bss_start
U __cxa_atexit
U __cxa_finalize
0000000000002000 d __dso_handle
0000000000001dd8 d _DYNAMIC
0000000000002008 A _edata
00000000000004e0 t __emutls_unregister_key
0000000000002008 A _end
00000000000005e8 r __FRAME_END__
0000000000001fd8 d _GLOBAL_OFFSET_TABLE_
0000000000000258 r ndk_build_number
0000000000000218 r ndk_version
0000000000000200 r note_android_ident
0000000000000214 r note_data
0000000000000298 r note_end
000000000000020c r note_name
00000000000004d0 t __on_dlclose
00000000000004f0 t __on_dlclose_late
and on libavformat:
0000000000000520 t atexit
0000000000000510 t __atexit_handler_wrapper
0000000000002008 A __bss_start
U __cxa_atexit
U __cxa_finalize
0000000000002000 d __dso_handle
0000000000001dd8 d _DYNAMIC
0000000000002008 A _edata
00000000000004f0 t __emutls_unregister_key
0000000000002008 A _end
00000000000005f8 r __FRAME_END__
0000000000001fd8 d _GLOBAL_OFFSET_TABLE_
0000000000000258 r ndk_build_number
0000000000000218 r ndk_version
0000000000000200 r note_android_ident
0000000000000214 r note_data
0000000000000298 r note_end
000000000000020c r note_name
00000000000004e0 t __on_dlclose
0000000000000500 t __on_dlclose_late
UPDATE 2
I applied #szatmary suggestion and finally managed to to build the apk file but upon reaching the System.loadLibrary call an exception occurs indicating that the linker can not find libavutil.so.56. I tried changing the address of the library in the cmake file to actually contain the version numbered lib file to no avail:
java.lang.UnsatisfiedLinkError: dlopen failed: library "libavutil.so.56" not found
at java.lang.Runtime.loadLibrary0(Runtime.java:1016)
at java.lang.System.loadLibrary(System.java:1657)
at com.hmomeni.canto.activities.EditActivity.<init>(EditActivity.kt:26)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
ffmpeg is written in C, It links different than C++ due to name mangling.
remove the set_target_properties LINKER_LANGUAGE CXX stuff
set_target_properties(avcodec PROPERTIES LINKER_LANGUAGE CXX)
and wrap the lib includes
extern "C" {
#include <libavformat/avformat.h>
#include <libavutil/dict.h>
}
When I open a new project that support C++ in Android Studio, It only outputs "Hello C++",And I didn't modify any code. I have tried to change the version of Android Studio and SDK. But it always show the same error.
My development platform: Windows 10
Now version: Android Studio 3.1.2
Thank you very much
Error While Building:
Build command failed.
Error while executing process C:\Users\15829\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {-HC:\Users\15829\AndroidStudioProjects\MyApplication\app -BC:\Users\15829\AndroidStudioProjects\MyApplication\app\.externalNativeBuild\cmake\debug\armeabi-v7a -DANDROID_ABI=armeabi-v7a -DANDROID_PLATFORM=android-15 -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\Users\15829\AndroidStudioProjects\MyApplication\app\build\intermediates\cmake\debug\obj\armeabi-v7a -DCMAKE_BUILD_TYPE=Debug -DANDROID_NDK=C:\Users\15829\AppData\Local\Android\Sdk\ndk-bundle -DCMAKE_CXX_FLAGS= -DCMAKE_TOOLCHAIN_FILE=C:\Users\15829\AppData\Local\Android\Sdk\ndk-bundle\build\cmake\android.toolchain.cmake -DCMAKE_MAKE_PROGRAM=C:\Users\15829\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\ninja.exe -GAndroid Gradle - Ninja}
-- Check for working C compiler: C:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: C:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe -- broken
/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe" is not able to compile a simple test program.
Open File
It fails with the following output:
Change Dir: C:/Users/15829/AndroidStudioProjects/MyApplication/app/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeTmp
Run Build
Command:"C:\Users\15829\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\ninja.exe"
"cmTC_a2146"
[1/2] Building C object CMakeFiles/cmTC_a2146.dir/testCCompiler.c.o
[2/2] Linking C executable cmTC_a2146
FAILED: cmd.exe /C "cd . &&
C:\Users\15829\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe
--target=armv7-none-linux-androideabi
--gcc-toolchain=C:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
--sysroot=C:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/sysroot
-isystem
C:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi
-D__ANDROID_API__=15 -g -DANDROID -ffunction-sections -funwind-tables
-fstack-protector-strong -no-canonical-prefixes -march=armv7-a
-mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,--noexecstack -Wformat
-Werror=format-security -Wl,--exclude-libs,libgcc.a
-Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot
C:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/platforms/android-15/arch-arm
-Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings
-Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a
-LC:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a
-Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro
-Wl,-z,now -Wl,--gc-sections -Wl,-z,nocopyreloc
CMakeFiles/cmTC_a2146.dir/testCCompiler.c.o -o cmTC_a2146 -latomic -lm &&
cd ."
error: no such file or directory:
'CMakeFiles/cmTC_a2146.dir/testCCompiler.c.o'
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt
-- Configuring incomplete, errors occurred!
See also "C:/Users/15829/AndroidStudioProjects/MyApplication/app/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeOutput.log".
See also "C:/Users/15829/AndroidStudioProjects/MyApplication/app/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeError.log".
Build command failed.
Error while executing process C:\Users\15829\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {-HC:\Users\15829\AndroidStudioProjects\MyApplication\app -BC:\Users\15829\AndroidStudioProjects\MyApplication\app\.externalNativeBuild\cmake\release\armeabi-v7a -DANDROID_ABI=armeabi-v7a -DANDROID_PLATFORM=android-15 -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\Users\15829\AndroidStudioProjects\MyApplication\app\build\intermediates\cmake\release\obj\armeabi-v7a -DCMAKE_BUILD_TYPE=Release -DANDROID_NDK=C:\Users\15829\AppData\Local\Android\Sdk\ndk-bundle -DCMAKE_CXX_FLAGS= -DCMAKE_TOOLCHAIN_FILE=C:\Users\15829\AppData\Local\Android\Sdk\ndk-bundle\build\cmake\android.toolchain.cmake -DCMAKE_MAKE_PROGRAM=C:\Users\15829\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\ninja.exe -GAndroid Gradle - Ninja}
-- Check for working C compiler: C:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: C:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe -- broken
/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe" is not able to compile a simple test program.
Open File
It fails with the following output:
Change Dir: C:/Users/15829/AndroidStudioProjects/MyApplication/app/.externalNativeBuild/cmake/release/armeabi-v7a/CMakeFiles/CMakeTmp
Run Build
Command:"C:\Users\15829\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\ninja.exe"
"cmTC_e2e33"
[1/2] Building C object CMakeFiles/cmTC_e2e33.dir/testCCompiler.c.o
[2/2] Linking C executable cmTC_e2e33
FAILED: cmd.exe /C "cd . &&
C:\Users\15829\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe
--target=armv7-none-linux-androideabi
--gcc-toolchain=C:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
--sysroot=C:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/sysroot
-isystem
C:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi
-D__ANDROID_API__=15 -g -DANDROID -ffunction-sections -funwind-tables
-fstack-protector-strong -no-canonical-prefixes -march=armv7-a
-mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,--noexecstack -Wformat
-Werror=format-security -Wl,--exclude-libs,libgcc.a
-Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot
C:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/platforms/android-15/arch-arm
-Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings
-Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a
-LC:/Users/15829/AppData/Local/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a
-Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro
-Wl,-z,now -Wl,--gc-sections -Wl,-z,nocopyreloc
CMakeFiles/cmTC_e2e33.dir/testCCompiler.c.o -o cmTC_e2e33 -latomic -lm &&
cd ."
error: no such file or directory:
'CMakeFiles/cmTC_e2e33.dir/testCCompiler.c.o'
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt
-- Configuring incomplete, errors occurred!
See also "C:/Users/15829/AndroidStudioProjects/MyApplication/app/.externalNativeBuild/cmake/release/armeabi-v7a/CMakeFiles/CMakeOutput.log".
See also "C:/Users/15829/AndroidStudioProjects/MyApplication/app/.externalNativeBuild/cmake/release/armeabi-v7a/CMakeFiles/CMakeError.log".
Try to add this to your build.gradle:
externalNativeBuild {
cmake {
arguments '-DANDROID_TOOLCHAIN=clang' --> gcc
}
}