user1125898
user1125898

Reputation: 371

How to compile and run a C/C++ program on the Android system

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?

Upvotes: 37

Views: 128384

Answers (4)

Saheli Rana
Saheli Rana

Reputation: 1

#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;
}

Upvotes: 0

Jeegar Patel
Jeegar Patel

Reputation: 27230

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

Upvotes: 8

Ashkan
Ashkan

Reputation: 1893

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

Upvotes: 22

Ted Hopp
Ted Hopp

Reputation: 234847

You need to download the Native Development Kit.

Upvotes: 5

Related Questions