ndk-build 可执行文件未被识别为可执行文件

ndk-build executable file not recognized as executable

我只是尝试为一个简单的 hello 程序做一个 ndk-build。

Android.mk 文件:

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 文件:

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

源文件(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;
}

ndk-build 成功:

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

但是在我将它推送到模拟器并尝试执行之后,我得到了这个错误: /system/bin/sh: ./hello: 不可执行: 32 位 ELF 文件

用文件命令查看,显示为: 文件libs/armeabi-v7a/hello libs/armeabi-v7a/hello:ELF 32 位 LSB 共享对象,ARM,EABI5 版本 1 (SYSV),动态链接,解释器 /system/bin/linker,剥离

使用 readelf 检查: readelf --文件头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

那么,我的设置有什么问题吗?只是想不通为什么。 提前致谢。

似乎是与模拟器有关的问题。将其推送到 Android 设备并且它有效。

一个模拟器只能配置运行一种CPU ABI。您的模拟器可能已设置为 x86。检查以确保您在 Application.mk 中构建的 ABI 与模拟器中配置的 ABI 匹配。例如,如果您的模拟器是 运行ning x86:

确保您的 Application.mk 文件也设置为相同的值:

APP_ABI := x86