36

Q1) I want to run a simple c program on android emulator.I am using windows xp os on my machine. I have installed sdk, jdk, eclipse for android development and succeeded running android application on AVD.

Q2) I just need to know is there any way to run a C program(without any java) on AVD. On my machine I have installed arm and using that I have compiled a C program.

Q3) I also want to know is it possible to push the compiled binary into android device or AVD and run using the terminal of the android device or AVD?

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
user1125898
  • 361
  • 1
  • 3
  • 3

4 Answers4

21

You can compile your C programs with an ARM cross-compiler:

arm-linux-gnueabi-gcc -static -march=armv7-a test.c -o test

Then you can push your compiled binary file to somewhere (don't push it in to the SD card):

adb push test /data/local/tmp/test
cat
  • 3,888
  • 5
  • 32
  • 61
Ashkan
  • 1,865
  • 16
  • 13
  • 1
    I got `Fatal: Kernel too old. Segmentation fault`. My ubuntu has kernel 3.2.0-38 while Android usually runs on 2.6.xx Do you know how to force the compiler to statically link the binary with an older glibc? – Max Apr 12 '13 at 08:07
  • @TranSonHai: you should use Android NDK and its `bionic` runtime library instead of the ubuntu tooolchain. – Alex Cohn Feb 28 '14 at 13:37
  • 1
    `-march=armv7` should be changed to `-march=armv7-a`. – Javad Nov 21 '14 at 07:25
  • What about other architectures, such as Intel CPUs ? – André Fratelli Oct 22 '15 at 08:31
  • 1
    you should use android ndk toolchain and find the correct compiler for your architecture: http://developer.android.com/ndk/guides/standalone_toolchain.html – Ashkan Nov 01 '15 at 17:42
  • Still helpful in 2016, +1 :D – cat Feb 29 '16 at 23:31
  • I'm try compile badblocks for a armv7, could someone help me, see my question in xda https://forum.xda-developers.com/general/help/compile-badblocks-tools-armv7-t3558431 – Milor123 Feb 17 '17 at 22:18
8

if you have installed NDK succesfully then start with it sample application

http://developer.android.com/sdk/ndk/overview.html#samples

if you are interested another ways of this then may this will help

http://shareprogrammingtips.blogspot.com/2018/07/cross-compile-cc-based-programs-and-run.html

I also want to know is it possible to push the compiled binary into android device or AVD and run using the terminal of the android device or AVD?

here you can see NestedVM

NestedVM provides binary translation for Java Bytecode. This is done by having GCC compile to a MIPS binary which is then translated to a Java class file. Hence any application written in C, C++, Fortran, or any other language supported by GCC can be run in 100% pure Java with no source changes.


Example: Cross compile Hello world C program and run it on android

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
5

You need to download the Native Development Kit.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 2
    I have done it already..but I haven't found anything useful what to do next? – user1125898 Jan 02 '12 at 08:37
  • @user1125898 - What do you mean you haven't found anything useful? The NDK is exactly for the purpose of writing native code in C/C++. Scroll to the bottom of the link I provided and you'll see a section on getting started, as well as a link to the NDK discussion group. – Ted Hopp Jan 02 '12 at 21:39
0
#include <stdio.h>

int main() {
    int rows, i, j;
    
    // Read the number of rows from the user
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    
    // Pattern 1
    printf("\nPattern 1:\n");
    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }
    
    // Pattern 2
    printf("\nPattern 2:\n");
    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }
    
    // Pattern 3
    printf("\nPattern 3:\n");
    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }
    
    // Pattern 4
    printf("\nPattern 4:\n");
    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }
    
    // Pattern 5
    printf("\nPattern 5:\n");
    for (i = 1; i <= rows; i++) {
        if (i % 2 != 0) {
            for (j = 1; j <= rows; j++) {
                printf("*");
            }
        } else {
            printf("*");
        }
        printf("\n");
    }
    
    // Pattern 6
    printf("\nPattern 6:\n");
    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        for (j = 1; j <= rows - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= rows - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }
    
    // Pattern 7
    printf("\nPattern 7:\n");
    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }
    
    // Pattern 8
    printf("\nPattern 8:\n");
    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        for (j = 1; j <= rows - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= rows - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }
    
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 03 '23 at 07:43
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Aug 24 '23 at 00:14