2017-06-14 65 views
1

試圖運行一個簡單的HelloWorld的Unix可執行文件:Android的嘗試運行UNIX可執行文件:語法錯誤:「__TEXT」意外

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

+0

[爲Android外殼構建可執行文件](https://stackoverflow.com/a/35275134/3290339) – Onik

回答

0

答案是,編譯器不是要使用的正確編譯器。如果你想在另一臺設備上運行它,你可以在那裏編譯它,或者使用一些交叉編譯器。

現在的問題是:哪個編譯器可以工作?我發現這個建議(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 

但是,這不會在這個特定星座工作。

相關問題