Android trying to run an unix executable: syntax error: '__TEXT' unexpected

204 Views Asked by At

While trying to run a simple HelloWorld Unix executable:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello World!" << endl;
}

(Compiled through g++ HelloWorld.cpp -o HelloWorld (on Mac). The program works on my Mac by using ./HelloWorld and by letting it run through a Java environment:

(HelloWorld.java -> working)

public class HelloWorld
{
    public static void main(String args[])
    {
        String[] command = new String[]{"/system/bin/chmod", "744",
         "/Developer/Java/HelloWorld" };
        execute(command);

        command = new String[]{"./HelloWorld"};
        execute(command);
    }

    public static void execute(String...command)
    {
        StringBuilder log = new StringBuilder();

        try
        {
            BufferedReader br;
            String line;

            ProcessBuilder builder = new ProcessBuilder(command);
            builder.redirectErrorStream(true);
            Process proc = builder.start();
            int exitVal = proc.waitFor();
            System.out.println("Process exitValue: " + exitVal);
            br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            while ( (line = br.readLine()) != null)
                System.out.println(line + "\n");
        }
        catch (IOException e) {
            log.append("General IOException:\n" + e.getMessage() + "\n");
        }
        catch (InterruptedException e) {
            log.append("Error:\n" + e.getMessage() + "\n");
        }
    }
}

In my java code for the Android app, I first copied the executable to getBaseContext().getDataDir(), this works fine. To change the permissions I'm using the following:

command = new String[]{"/system/bin/chmod", "744",
            getAssetsPath() + "/HelloWorld" };
execute(pv, command);

and trying to run the program through:

command = new String[]{"." + getAssetsPath() + "/HelloWorld"};
terminal(tv, command);

Note, that I use the following functions:

public File getAssetsDir() {
    return getBaseContext().getDataDir();
}

public String getAssetsPath() {
    return getAssetsDir().getAbsolutePath();
}

public void execute(TextView tv, String...command)
{
    tv.setText("Starting Terminal.\n");
    StringBuilder log = new StringBuilder();

    try
    {
        BufferedReader br;
        String line;

        ProcessBuilder builder = new ProcessBuilder(command);
        builder.redirectErrorStream(true);
        Process proc = builder.start();
        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);
        br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        while ( (line = br.readLine()) != null)
            log.append(line + "\n");
    }
    catch (IOException e) {
        log.append("General IOException:\n" + e.getMessage() + "\n");
    }
    catch (InterruptedException e) {
        log.append("Error:\n" + e.getMessage() + "\n");
    }
    tv.setText(log.toString());
}

As already said this will result in the following error inside the TextView (tested on Pixel_XL_API_25):

syntax error: '__TEXT' unexpected

Hope you can help me find the cause of this problem. Thanks in advance.

Edit: If you want to know why I want to use a Unix executable for such simple things: This is just for testing. Actually, I want to run other more complex programs/libraries which will be hard to use through ndk, because there is no cmake for this library, only "normal" make.

1

There are 1 best solutions below

0
On

The answer is, that the compiler isn't the right compiler to use. If you want to run it on another device you have ti compile it there or use some cross compiler, I guess.

The question is now: Which compiler would work? I found this suggestion (How to compile and run a C/C++ program on the Android system):

arm-linux-gnueabi-g++ -static -march=armv7-a HelloWorld.c -o HelloWorld

But that won't work in this specific constellation.