ndk-build BUILD_EXECUTABLE 产生的文件不能被识别为executable

fyjin99 2016-06-22 08:18:07

本人想通过ndk-build编译一个可执行文件直接push到手机上以root执行,照着网上的方法做了一下执行时发现编译出来的文件并不能被识别为可执行文件。

Android.mk file:
LOCAL_PATH:= $(call my-dir) # Get the local path of the project.
include $(CLEAR_VARS) # Clear all the variables with a prefix "LOCAL_"

LOCAL_SRC_FILES:=hello.cpp # Indicate the source code.
LOCAL_MODULE:= hello # The name of the binary.
LOCAL_ARM_MODE := arm
include $(BUILD_EXECUTABLE) # Tell ndk-build that we want to build a native executable.


Application.mk file:
APP_ABI := armeabi-v7a # Define the target architecture to be ARM.
APP_STL := gnustl_static
#APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions # This is the place you enable exception.
APP_PLATFORM = android-19


source file (hello.cpp):
#include <iostream>

int main(int argc, char* argv[])
{
std::cout<<"Hello from world!"<<std::endl;
for(int i=0; i<argc; ++i)
std::cout<<"Arg "<<i<<" is: "<<argv[i]<<std::endl;
return 0;
}


编译过程:
[armeabi-v7a] Compile++ arm : hello <= hello.cpp
[armeabi-v7a] Executable : hello
[armeabi-v7a] Install : hello => libs/armeabi-v7a/hello


push到手机后root执行的错误: /system/bin/sh: ./hello: not executable: 32-bit ELF file

在Linux下用file命令查看:
file libs/armeabi-v7a/hello
libs/armeabi-v7a/hello: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /system/bin/linker, stripped

请各位大神帮看下为什么会出现这种情况。谢谢。
...全文
1075 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
网易云捕 2016-06-23
  • 打赏
  • 举报
回复
引用 7 楼 fyjin99 的回复:
[quote=引用 6 楼 crash163 的回复:] 手机系统版本是多少? 执行时命令行是什么? 我试了,这个编译出来的hello, 可以在我的手机上运行。android4.4. adb push ./hello /data/local/tmp/hello adb shell cd /data/local/tmp chmod 777 hello ./hello 执行成功
谢谢,我因为手机不能root所以平时一般都是用emulator。在手机上测试的确可以,看来是AVD的问题。为什么这个在模拟器上不行呢?[/quote] 是在bluestack?genymotion?这类模拟器默认只支持x86指令,而上面的hello是arm指令集。 如果是android sdk自带的模拟器,去掉LOCAL_ARM_MODE := arm再试下吧。
网易云捕 2016-06-22
  • 打赏
  • 举报
回复
手机系统版本是多少? 执行时命令行是什么? 我试了,这个编译出来的hello, 可以在我的手机上运行。android4.4. adb push ./hello /data/local/tmp/hello adb shell cd /data/local/tmp chmod 777 hello ./hello 执行成功
fyjin99 2016-06-22
  • 打赏
  • 举报
回复
但是push到手机后root执行的错误: /system/bin/sh: ./hello: not executable: 32-bit ELF file
网易云捕 2016-06-22
  • 打赏
  • 举报
回复
如果是这个问题,这个大可以放心。 android系统的linker与linux的linker的实现是不一样的。 就是说,这个编译出来的hello不可以在linux上执行,但是不代表在android上不可以执行。
fyjin99 2016-06-22
  • 打赏
  • 举报
回复
引用 2 楼 crash163 的回复:
lz各个文件都是正确的,我猜大概的原因是,编译时指定的系统版本或者指令集与手机不一致。 你可以试着这样改一下: 1、将Application.mk中的这句 APP_PLATFORM = android-19 改为 APP_PLATFORM = android-9 2、若还不能运行,将这句 APP_ABI := armeabi-v7a 改为 APP_ABI := armeabi 同时,将Android.mk中的这句 LOCAL_ARM_MODE := arm 去掉
这个不是在手机上运行时出问题,通过readelf命令可以看出该文件在编译出来之后就不是一个可执行文件。readelf命令是我在编译系统(Linux)下执行的。
网易云捕 2016-06-22
  • 打赏
  • 举报
回复
lz各个文件都是正确的,我猜大概的原因是,编译时指定的系统版本或者指令集与手机不一致。 你可以试着这样改一下: 1、将Application.mk中的这句 APP_PLATFORM = android-19 改为 APP_PLATFORM = android-9 2、若还不能运行,将这句 APP_ABI := armeabi-v7a 改为 APP_ABI := armeabi 同时,将Android.mk中的这句 LOCAL_ARM_MODE := arm 去掉
fyjin99 2016-06-22
  • 打赏
  • 举报
回复
用readelf查看: readelf --file-header libs/armeabi-v7a/hello ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: DYN (Shared object file) Machine: ARM Version: 0x1 Entry point address: 0x3898 Start of program headers: 52 (bytes into file) Start of section headers: 205324 (bytes into file) Flags: 0x5000200, Version5 EABI, soft-float ABI Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 8 Size of section headers: 40 (bytes) Number of section headers: 26 Section header string table index: 25
fyjin99 2016-06-22
  • 打赏
  • 举报
回复
引用 6 楼 crash163 的回复:
手机系统版本是多少? 执行时命令行是什么? 我试了,这个编译出来的hello, 可以在我的手机上运行。android4.4. adb push ./hello /data/local/tmp/hello adb shell cd /data/local/tmp chmod 777 hello ./hello 执行成功
谢谢,我因为手机不能root所以平时一般都是用emulator。在手机上测试的确可以,看来是AVD的问题。为什么这个在模拟器上不行呢?

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧