简体   繁体   English

在Android上运行C代码而无需编译整个树

[英]Run C code on Android without compiling the entire tree

I have a C code which I want to add to the Android C libraries. 我有一个C代码,想添加到Android C库中。

I can build and execute the coed when I am compiling the entire android tree. 当我编译整个Android树时,我可以构建并执行coed。
I would like to be able to add the code to an existing compiled android environment. 我希望能够将代码添加到现有的已编译android环境中。 ie To use a built device and just push my application and execute it there. 即要使用内置设备并仅将我的应用程序推送并在那里执行它。

I succeeded to compile my code on windows platform for linux using the NDK and to push it using adb push command. 我成功使用NDK在Windows平台上为Linux编译了我的代码,并使用adb push命令将其adb push

When trying to execute using the adb execute 尝试使用adb execute

I get the following error: 我收到以下错误:

[Android] not executable: magic=7F45 [Android]无法执行:magic = 7F45

Magic 7F45 means the binary is a linux ELF and is not compiled to the ARM arch (you can find out what arch using file ). Magic 7F45表示二进制文件是Linux ELF ,并且未编译为ARM架构(您可以使用file找出哪个架构)。 You need to create the standalone toolchain using the NDK and then use it to build the correct binaries. 您需要使用NDK创建独立的工具链,然后使用它来构建正确的二进制文件。

Here are the main details of how you do this courtesy of the android dev site : 以下是您如何通过android dev网站进行此操作的主要详细信息:

The NDK allows you to create a "customized" toolchain installation to make life easier. NDK允许您创建“自定义”工具链安装,以简化工作。 For example, consider the following command: 例如,考虑以下命令:

$NDK/build/tools/make-standalone-toolchain.sh --platform=android-5 --install-dir=/tmp/my-android-toolchain 

This will create a directory named /tmp/my-android-toolchain containing a copy of the android-5/arch-arm sysroot, and of the toolchain binaries. 这将创建一个名为/ tmp / my-android-toolchain的目录,其中包含android-5 / arch-arm sysroot和工具链二进制文件的副本。

Note that by default, the ARM-based toolchain will be selected by the script. 请注意,默认情况下,脚本将选择基于ARM的工具链。
Use the '--arch=x86' option to specify the x86-based one, 使用'--arch = x86'选项指定基于x86的选项,
use the '--arch=mips' option to specify the MIPS-based one, or alternatively '--toolchain='. 使用“ --arch = mips”选项来指定基于MIPS的选项,或者使用“ --toolchain =”。

You can later use it directly with something like: 您以后可以将其直接用于以下内容:

export PATH=/tmp/my-android-toolchain/bin:$PATH 
export CC=arm-linux-androideabi-gcc

The environment variables shown above allow you to make use of configure or make to build 3rd party code for the arch you specified. 上面显示的环境变量使您可以使用configuremake来为指定的拱门构建第三方代码。 You may of course just use the compiler directly. 您当然可以直接使用编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM