3

I have a program written in C with a standard main() function.

Is it possible to run this program on Android, e.g. launching it from my application or from one of the many shell applications? If so, what steps do I need to take to achieve this? Can I just compile the application with the NDK?

I know there are some answered questions on how to link C functions to Android code, mainly pointing out to NDK tutorials, but none of them was, at least for me, clear. It seems that NDK is all about using C functions, not running a fully C compiled program. Is this correct?

Dekel
  • 192
  • 4
  • 12

2 Answers2

5

All you need to do is compile it for the correct architecture (usually ARM) using a cross compiler (how you get it depends on your linux distribution). Then you can execute it from your (Java-based) like any other excecutable:

Process process = Runtime.getRuntime().exec(commandLine);

See Any way to run shell commands on android programmatically? for more information about the "executing" part.

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

NDK includes all tools necessary to build an executable from C sources, available for download for Linux, Mac, and Windows. The magic enchantment you should use is include $(BUILD_EXECUTABLE)

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Digged the net to find out how can I compile a simple c file using the ndk-toolchain. So far i got to the point where i need to execute the command arm-linux-androideabi-gcc under the linux shell for compiling my code. So far i was not succesfull using it, since i get compilation error: "defs.h: No such file or directory" – Dekel Sep 20 '12 at 19:01
  • Only take into account that the article is pretty ancient. New releases of NDK allow and even encourage the following build technique: go to your project's directory and run **ndk-build**. The resulting executable or shared library will be placed to **./libs** directory. – Alex Cohn Sep 20 '12 at 21:24
  • thank you very much Alex. Can you (or anyone else) explain more about how to use the "ndk-build" command? my intention is simply to compile a c program to be used in an android app, with Runtime.getRuntime().exec(commandLine); – Dekel Sep 20 '12 at 22:30
  • This http://www.packtpub.com/article/creating-compiling-deploying-native-projects-android-ndk is probably the best introduction I saw. – Alex Cohn Sep 21 '12 at 01:16
  • If you have specific problems, just ask less general questions. – Alex Cohn Sep 21 '12 at 01:18
  • Here's the way to do it: – Dekel Sep 23 '12 at 12:46
  • A working example can be found in NDK samples in $NDK_ROOT/samples/test-libstdc++ (or see a mirror: http://mirror.yongbok.net/pub/pub/linux/android/repository/development/ndk/samples/test-libstdc%2b%2b) – Alex Cohn Sep 23 '12 at 15:28