简体   繁体   English

使用 arm-linux-gnueabi-gcc 为 Android 交叉编译静态 C hello world

[英]Cross compiling static C hello world for Android using arm-linux-gnueabi-gcc

I want to build a static hello world from C using arm-linux-gnueabi-gcc as opposed to using the NDK standalone toolchain or Codesourcery for that matter.我想使用 arm-linux-gnueabi-gcc 从 C 构建一个静态的 hello world,而不是使用 NDK 独立工具链或 Codesourcery。

In Ubuntu...在 Ubuntu 中...

I have done the following:我做了以下工作:

sudo apt-get install gcc-arm-linux-gnueabi

I created a hi.c like this:我创建了一个这样的 hi.c:

#include <stdio.h>

int main(int argc, char** argv) {
   printf("hello world\n");
   return 0;
}

I have compiled it like this:我是这样编译的:

arm-linux-gnueabi-gcc -static hi.c -o hi 

I ran it on an emulator like this:我在这样的模拟器上运行它:

adb push hi /data/hi
adb shell /data/hi

But, I get this:但是,我明白了:

[1]   Illegal instruction     /data/hi

What step have I forgot?我忘记了哪一步? Based on past experience this "should" have worked, but I obviously messed this up.根据过去的经验,这“应该”有效,但我显然搞砸了。

Try specifying the architecture/cpu.尝试指定架构/cpu。 It sounds like the compiler is creating code with a higher architecture version than the emulator can handle.听起来编译器正在创建具有比模拟器可以处理的更高架构版本的代码。

This might work:这可能有效:

arm-linux-gnueabi-gcc -static -march=armv5 hi.c -o hi

It worked for me with CodeBench compiler on ubuntu desktop.它在 ubuntu 桌面上使用 CodeBench 编译器对我有用。https://sourcery.mentor.com/sgpp/lite/arm/portal/release2029https://sourcery.mentor.com/sgpp/lite/arm/portal/release2029

Just create a static binary with this command:只需使用以下命令创建一个静态二进制文件:

arm-none-linux-gnueabi-gcc -o hello -static hello.c

then push it to phone然后推送到手机

adb push hello /data/local/tmp

go run it:去运行它:

adb shell
$ chmod 755 /data/local/tmp/hello
$ /data/local/tmp/hello

This will print Hello World on terminal.这将在终端上打印 Hello World。 Same can be done from phone also.同样可以通过电话完成。 Use terminal emulator or SL4A bash shell to execute.使用终端模拟器或 SL4A bash shell 来执行。

If I do this on a Debian machine (VM in my case), all seems well.如果我在 Debian 机器(在我的情况下是 VM)上执行此操作,一切似乎都很好。 I am not sure what when wrong with doing similar on ubuntu.我不确定在 ubuntu 上做类似的事情有什么问题。 It could be as Leo suggested, but I cannot confirm.可能就像 Leo 建议的那样,但我无法确认。 This should work for you though.不过这应该对你有用。

http://www.cnx-software.com/2012/01/16/installing-emdebian-arm-cross-toolchain-in-debian/ http://www.cnx-software.com/2012/01/16/installing-emdebian-arm-cross-toolchain-in-debian/

Someone added this link, but it is not using the toolchain I mentioned in the description.有人添加了这个链接,但它没有使用我在描述中提到的工具链。 Leaving it in case anyone is interested.留下它以防万一有人感兴趣。

http://tariqzubairy.wordpress.com/2012/03/09/arm-binaries-static-library-for-android/ http://tariqzubairy.wordpress.com/2012/03/09/arm-binaries-static-library-for-android/

据我所知,您不能在 Android 中运行未使用某种形式的 gcc-arm-linux-androideabi 编译的用户级应用程序。

Your code actually works for me.你的代码实际上对我有用。

I compiled it on Ubuntu and push it to /data/local/tmp我在 Ubuntu 上编译并推送到 /data/local/tmp

And then chmod 777 hi然后 chmod 777 嗨

Finally it works well.最后它运作良好。

Did you check the permissions of the data folder ?您是否检查了数据文件夹的权限? Try using the local instead !尝试使用本地代替! You can just use adb shell and then cd into the folder where the executable was pushed and try ./hi.您可以只使用 adb shell,然后 cd 进入可执行文件被推送的文件夹并尝试 ./hi。 I guess this is just a permissions issue我想这只是一个权限问题

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

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