I want to call c language through android, call assembly language through c language, but it appears
undefined reference to `MyASMTest (int, int) '
The specific error is like this Execution failed for task ': app: externalNativeBuildDebug'
this is my native-lib.cpp
#include <jni.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define __x86_64__
#ifndef __x86_64__
static int __attribute__((naked, pure)) MyASMTest(int a, int b)
{
#ifndef __arm__
asm("add w0, w0, w1");
asm("ret");
#else
asm(".thumb");
asm(".syntax unified");
asm("sub r0, r0, r1");
asm("add r0, r0, #1");
asm("bx lr");
#endif
}
#else
extern int MyASMTest(int a, int b);
#endif
extern "C"
JNIEXPORT JNICALL jstring Java_com_example_cpptest_MainActivity_stringFromJNI (JNIEnv *env, jobject instance)
{
char strBuf[128] = {' '};
sprintf(strBuf, "Hello from C! ASM test result: %d", MyASMTest(4,6));
return env->NewStringUTF(strBuf);
}
this is my CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
enable_language(ASM_NASM)
if(${ANDROID_ABI} STREQUAL "x86_64")
set(asm_SRCS src/main/cpp/test.asm)
endif()
add_library(
native-lib
SHARED
native-lib.cpp )
find_library(
log-lib
log )
target_link_libraries(
native-lib
${log-lib} )
this is my gradle
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.cpptest"
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
arguments "-DANDROID_ARM_NEON=TRUE"
cppFlags ""
abiFilters 'x86_64', 'armeabi-v7a', 'arm64-v8a'
}
}
dataBinding{
enabled true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}
this is my test.asm
global MyASMTest
section .text
MyASMTest:
sub edi, esi
mov eax, edi
ret
This is my error message
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing process C:\Users\user\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe with arguments {-C F:\Users\user\Desktop\aaaaaaaaaaaaaaaaaaa\CppTest\app\.cxx\cmake\debug\arm64-v8a native-lib}
ninja: Entering directory `F:\Users\user\Desktop\aaaaaaaaaaaaaaaaaaa\CppTest\app\.cxx\cmake\debug\arm64-v8a'
[1/2] Building CXX object CMakeFiles/native-lib.dir/native-lib.cpp.o
[2/2] Linking CXX shared library F:\Users\user\Desktop\aaaaaaaaaaaaaaaaaaa\CppTest\app\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so
FAILED: F:/Users/user/Desktop/aaaaaaaaaaaaaaaaaaa/CppTest/app/build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so
cmd.exe /C "cd . && C:\Users\user\AppData\Local\Android\Sdk\ndk\21.0.6113669\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android24 --gcc-toolchain=C:/Users/user/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/user/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 -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,--no-undefined -Qunused-arguments -shared -Wl,-soname,libnative-lib.so -o F:\Users\user\Desktop\aaaaaaaaaaaaaaaaaaa\CppTest\app\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o -llog -latomic -lm && cd ."
CMakeFiles/native-lib.dir/native-lib.cpp.o: In function `Java_com_example_cpptest_MainActivity_stringFromJNI':
F:/Users/user/Desktop/aaaaaaaaaaaaaaaaaaa/CppTest/app/src/main/cpp/native-lib.cpp:76: undefined reference to `MyASMTest(int, int)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I want to test how to use assembly in Android, but I found that I am not familiar with arm32 assembly, so I want to switch to x86 assembly, so I used # defind__x86_64__ above. If #defind x86_64 software is removed, the software can be successfully executed. So I want to know how to make my software successfully execute x86 assembly code, because I am more familiar with x86 than arm32