【问题标题】:Android trying to run an unix executable: syntax error: '__TEXT' unexpectedAndroid 试图运行 unix 可执行文件:语法错误:'__TEXT' 意外
【发布时间】:2017-06-14 14:45:12
【问题描述】:

在尝试运行简单的 HelloWorld Unix 可执行文件时:

#include <iostream>
using namespace std;

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

(通过 g++ HelloWorld.cpp -o HelloWorld 编译(在 Mac 上)。该程序在我的 Mac 上使用 ./HelloWorld 并让它在 Java 环境中运行:

(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");
        }
    }
}

在我的 Android 应用程序的 java 代码中,我首先将可执行文件复制到 getBaseContext().getDataDir(),这工作正常。要更改我使用以下权限的权限:

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

并尝试通过以下方式运行程序:

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

注意,我使用以下函数:

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());
}

如前所述,这将导致 TextView 内出现以下错误(在 Pixel_XL_API_25 上测试):

syntax error: '__TEXT' unexpected

希望你能帮我找出这个问题的原因。提前致谢。

编辑: 如果你想知道我为什么要使用 Unix 可执行文件来处理这些简单的事情:这只是为了测试。实际上,我想运行其他更复杂的程序/库,这些程序/库很难通过 ndk 使用,因为这个库没有 cmake,只有“正常”的 make。

【问题讨论】:

标签: java android executable native-executable


【解决方案1】:

答案是,编译器不是正确使用的编译器。如果你想在另一台设备上运行它,你可以在那里编译它或使用一些交叉编译器,我猜。

现在的问题是:哪个编译器可以工作?我找到了这个建议(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

但这不适用于这个特定的星座。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 2015-02-06
    相关资源
    最近更新 更多