使用arm-linux-gnueabi-gcc交叉编译Android的静态C hello世界

时间:2021-03-29 09:11:40

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世界,而不是使用NDK独立工具链或代码源。

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.

我忘记了什么步骤?根据过去的经验,这“应该”有效,但我显然搞砸了。

6 个解决方案

#1


14  

Try specifying the architecture/cpu. It sounds like the compiler is creating code with a higher architecture version than the emulator can handle.

尝试指定架构/ cpu。听起来编译器正在创建具有比模拟器可以处理的更高架构版本的代码。

This might work:

这可能有效:

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

#2


9  

It worked for me with CodeBench compiler on ubuntu desktop. https://sourcery.mentor.com/sgpp/lite/arm/portal/release2029

它在ubuntu桌面上使用CodeBench编译器对我有用。 https://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

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

then, adb push hello /data

然后,adb推你好/数据

go to, adb shell $ ./data/hello This will print Hello World on terminal. Same can be done from phone also. Use terminal emulator or SL4A bash shell to execute.

转到,adb shell $ ./data/hello这将在终端上打印Hello World。同样可以通过手机完成。使用终端模拟器或SL4A bash shell执行。

#3


3  

If I do this on a Debian machine (VM in my case), all seems well. I am not sure what when wrong with doing similar on ubuntu. It could be as Leo suggested, but I cannot confirm. This should work for you though.

如果我在Debian机器上执行此操作(在我的情况下为VM),一切似乎都很好。我不确定在ubuntu上做类似的错误是什么。这可能是Leo建议的,但我无法证实。这应该对你有用。

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/

#4


1  

As far as I know, you cannot run user-land applications within Android that are not compiled with some form of gcc-arm-linux-androideabi.

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

#5


0  

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.

最后效果很好。

#6


-2  

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. I guess this is just a permissions issue

你检查了数据文件夹的权限了吗?尝试使用本地代替!您可以使用adb shell然后cd到推送可执行文件的文件夹中并尝试./hi。我想这只是一个权限问题

#1


14  

Try specifying the architecture/cpu. It sounds like the compiler is creating code with a higher architecture version than the emulator can handle.

尝试指定架构/ cpu。听起来编译器正在创建具有比模拟器可以处理的更高架构版本的代码。

This might work:

这可能有效:

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

#2


9  

It worked for me with CodeBench compiler on ubuntu desktop. https://sourcery.mentor.com/sgpp/lite/arm/portal/release2029

它在ubuntu桌面上使用CodeBench编译器对我有用。 https://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

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

then, adb push hello /data

然后,adb推你好/数据

go to, adb shell $ ./data/hello This will print Hello World on terminal. Same can be done from phone also. Use terminal emulator or SL4A bash shell to execute.

转到,adb shell $ ./data/hello这将在终端上打印Hello World。同样可以通过手机完成。使用终端模拟器或SL4A bash shell执行。

#3


3  

If I do this on a Debian machine (VM in my case), all seems well. I am not sure what when wrong with doing similar on ubuntu. It could be as Leo suggested, but I cannot confirm. This should work for you though.

如果我在Debian机器上执行此操作(在我的情况下为VM),一切似乎都很好。我不确定在ubuntu上做类似的错误是什么。这可能是Leo建议的,但我无法证实。这应该对你有用。

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/

#4


1  

As far as I know, you cannot run user-land applications within Android that are not compiled with some form of gcc-arm-linux-androideabi.

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

#5


0  

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.

最后效果很好。

#6


-2  

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. I guess this is just a permissions issue

你检查了数据文件夹的权限了吗?尝试使用本地代替!您可以使用adb shell然后cd到推送可执行文件的文件夹中并尝试./hi。我想这只是一个权限问题