gpt4 book ai didi

java - Android 尝试运行 unix 可执行文件 : syntax error: '__TEXT' unexpected

转载 作者:太空宇宙 更新时间:2023-11-04 11:24:19 25 4
gpt4 key购买 nike

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

#include <iostream>
using namespace std;

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

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

(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。

最佳答案

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

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

但这在这个特定的星座中不起作用。

关于java - Android 尝试运行 unix 可执行文件 : syntax error: '__TEXT' unexpected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44548009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com