searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

在Linux安装Android NDK并编译示例代码

2024-06-24 09:35:37
8
0

NDK install

$ mkdir -p ~/ndk
$ cd ~/ndk/
$ curl -O ****//dl.google.com/android/repository/android-ndk-r19c-linux-x86_64.zip
$ unzip android-ndk-r19c-linux-x86_64.zip
$ export ANDROID_NDK=/home/test/ndk/android-ndk-r19c

Sample

$ mkdir hello-ndk
$ cd hello-ndk/
  • main.cpp
$ cat << EOF > main.cpp
#include <iostream>

int main(int argc, char *argv[])
{
   std::cout << "Hello ndk!" << std::endl;
   return 0;
}
EOF
  • CMakeLists.txt
$ cat  << EOF > CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project (hello_cmake)

add_executable(hello_cmake main.cpp)
EOF
$ tree
.
├── CMakeLists.txt
└── main.cpp

Build for linux executable

$ mkdir build-linux
$ cd build-linux
$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/test/github/hello-ndk/build-linux
$ make
Scanning dependencies of target hello_cmake
[ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.o
[100%] Linking CXX executable hello_cmake
[100%] Built target hello_cmake
$ ./hello_cmake
Hello ndk!

Build for android executable

$ mkdir build-android
$ cd build-android
$ cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI="arm64-v8a" -DANDROID_NDK=$ANDROID_NDK -DANDROID_PLATFORM=android-21 ..
-- Check for working C compiler: /home/test/ndk/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- Check for working C compiler: /home/test/ndk/android-ndk-r19c/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
-- Check for working CXX compiler: /home/test/ndk/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++
-- Check for working CXX compiler: /home/test/ndk/android-ndk-r19c/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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/test/github/hello-ndk/build-android

$ make 
Scanning dependencies of target hello_cmake
[ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.o
[100%] Linking CXX executable hello_cmake
[100%] Built target hello_cmake
$ ./hello_cmake
-bash: ./hello_cmake: cannot execute binary file: Exec format error

Run in android

$ adb push build-android/hello_cmake /data/local/tmp
# cd /data/local/tmp/
/data/local/tmp # chmod +x hello_cmake ; ./hello_cmake
Hello ndk!
/data/local/tmp # file hello_cmake
hello_cmake: ELF shared object, 64-bit LSB arm64, dynamic (/system/bin/linker64)

Diff

file

$ file build-linux/hello_cmake
build-linux/hello_cmake: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=8e568fb9403912403a9daeb3a85cc58b8dad46f7, not stripped

$ file build-android/hello_cmake
build-android/hello_cmake: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[sha1]=322bed0b22b9c27619b3b3a128322993667a5e7a, with debug_info, not stripped

readelf -h

$ readelf -h build-linux/hello_cmake
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x7b0
  Start of program headers:          64 (bytes into file)
  Start of section headers:          7064 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         9
  Size of section headers:           64 (bytes)
  Number of section headers:         29
  Section header string table index: 28
$ readelf -h build-android/hello_cmake
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           AArch64
  Version:                           0x1
  Entry point address:               0xdf84
  Start of program headers:          64 (bytes into file)
  Start of section headers:          5142376 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         9
  Size of section headers:           64 (bytes)
  Number of section headers:         39
  Section header string table index: 36
0条评论
0 / 1000
h****n
13文章数
0粉丝数
h****n
13 文章 | 0 粉丝
原创

在Linux安装Android NDK并编译示例代码

2024-06-24 09:35:37
8
0

NDK install

$ mkdir -p ~/ndk
$ cd ~/ndk/
$ curl -O ****//dl.google.com/android/repository/android-ndk-r19c-linux-x86_64.zip
$ unzip android-ndk-r19c-linux-x86_64.zip
$ export ANDROID_NDK=/home/test/ndk/android-ndk-r19c

Sample

$ mkdir hello-ndk
$ cd hello-ndk/
  • main.cpp
$ cat << EOF > main.cpp
#include <iostream>

int main(int argc, char *argv[])
{
   std::cout << "Hello ndk!" << std::endl;
   return 0;
}
EOF
  • CMakeLists.txt
$ cat  << EOF > CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project (hello_cmake)

add_executable(hello_cmake main.cpp)
EOF
$ tree
.
├── CMakeLists.txt
└── main.cpp

Build for linux executable

$ mkdir build-linux
$ cd build-linux
$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/test/github/hello-ndk/build-linux
$ make
Scanning dependencies of target hello_cmake
[ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.o
[100%] Linking CXX executable hello_cmake
[100%] Built target hello_cmake
$ ./hello_cmake
Hello ndk!

Build for android executable

$ mkdir build-android
$ cd build-android
$ cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI="arm64-v8a" -DANDROID_NDK=$ANDROID_NDK -DANDROID_PLATFORM=android-21 ..
-- Check for working C compiler: /home/test/ndk/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- Check for working C compiler: /home/test/ndk/android-ndk-r19c/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
-- Check for working CXX compiler: /home/test/ndk/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++
-- Check for working CXX compiler: /home/test/ndk/android-ndk-r19c/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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/test/github/hello-ndk/build-android

$ make 
Scanning dependencies of target hello_cmake
[ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.o
[100%] Linking CXX executable hello_cmake
[100%] Built target hello_cmake
$ ./hello_cmake
-bash: ./hello_cmake: cannot execute binary file: Exec format error

Run in android

$ adb push build-android/hello_cmake /data/local/tmp
# cd /data/local/tmp/
/data/local/tmp # chmod +x hello_cmake ; ./hello_cmake
Hello ndk!
/data/local/tmp # file hello_cmake
hello_cmake: ELF shared object, 64-bit LSB arm64, dynamic (/system/bin/linker64)

Diff

file

$ file build-linux/hello_cmake
build-linux/hello_cmake: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=8e568fb9403912403a9daeb3a85cc58b8dad46f7, not stripped

$ file build-android/hello_cmake
build-android/hello_cmake: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[sha1]=322bed0b22b9c27619b3b3a128322993667a5e7a, with debug_info, not stripped

readelf -h

$ readelf -h build-linux/hello_cmake
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x7b0
  Start of program headers:          64 (bytes into file)
  Start of section headers:          7064 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         9
  Size of section headers:           64 (bytes)
  Number of section headers:         29
  Section header string table index: 28
$ readelf -h build-android/hello_cmake
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           AArch64
  Version:                           0x1
  Entry point address:               0xdf84
  Start of program headers:          64 (bytes into file)
  Start of section headers:          5142376 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         9
  Size of section headers:           64 (bytes)
  Number of section headers:         39
  Section header string table index: 36
文章来自个人专栏
教程
10 文章 | 1 订阅
0条评论
0 / 1000
请输入你的评论
0
0