Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run C++ application in Android SHELL

I want to run hello world written on C++ and compiled with Android toolchain 9, but I faced with issue: by default I have no permissions to launch it and I can't change permissions using chmod`.

I used Android 2.3.3 - Api Level 10

Application was compiled by cross compiler for API level 9

Procedure:

Compile application:

~/toolchain_andr9/bin/ arm-linux-androideabi-g++ helloworld.cpp

Then send application to SDCARD on the emulator:

>adb push a.out /mnt/sdcard

then go to SHELL and try to run a.out:

>adb shell
> 
>/mnt/sdcard/a.out

And result is:

>`/mnt/sdcard/a.out: permission denied`

command ls -l shows rights for a.out:

>`----rwxr-x system   sdcard_rw   863656 2012-04-12 22:42 a.out`

I tried to change permissions:

>chmod 777 /mnt/sdcard/a.out

But rights don't change:

>`----rwxr-x system   sdcard_rw   863656 2012-04-12 22:42 a.out`

I think I have left some important thing using android.

Could anybody help me and give me a way how to run application in `Android SHELL?

Thanks a lot.

P.S. sorry for my English =)

like image 507
Alex Wih Avatar asked Apr 12 '12 23:04

Alex Wih


People also ask

Can I run C program in Android?

Download the NDK and build toolsThe Android Native Development Kit (NDK): a toolset that allows you to use C and C++ code with Android, and provides platform libraries that allow you to manage native activities and access physical device components, such as sensors and touch input.

How do I open a .C file on Android?

To Install and Use C/C++ compiler in Termux (in Termux clang is the C/C++ compiler) , Download & Install Termux from : Play Store. After Installing execute this command pkg install clang. After Successfully installing clang you can compile C/C++ scripts.

How can I convert my C application to Android app?

Solution 1. You can use the design and GUI of your app and the internal model code if it is well crafted. But you have to re create the GUI in the native Android IDE and than transfer your code into the handler functions. If you want to use one code base for different target than may Xamarin an interesting tool.


1 Answers

By default, the SD card is mounted with option noexec, that disallows the execution of any file on the card, no matter what it's permissions(even -rwxrwxrwx), so you need to move the file to another location and then execute it.

The easiest is to move the file to /data/local/tmp/ and execute it using the full path (usual POSIX PATH semantics).

> adb push a.out /data/local/tmp/a.out
> adb shell
> chmod 755 /data/local/tmp/a.out
> /data/local/tmp/a.out

This does not require root access and survives reboot.

like image 138
Samveen Avatar answered Nov 16 '22 02:11

Samveen