2017-08-11 144 views
1

我正在写一个C++代码来执行android设备中的“top”命令。这是我使用的代码。在Android设备上运行C++程序

using namespace std; 

int main() 
{  
    char buffer[1024]; 
    string result; 
    system("top -n 1 | head -n 4 | tail -n 3"); 
    FILE *memcpu= popen("top -n 1 | head -n 4 | tail -n 3","r"); 
     while (!feof(memcpu)) { 
      if (fgets(buffer, 1024, memcpu) != NULL) 
       result+=buffer; 
     } 
     cout<<"result you need\n"<<result; 
} 

我想在adb设备中运行此文件。因此,我用命令

arm-linux-gnueabi-g++ -static -march=armv7-a name.cpp -o test 

构建程序当我运行该程序,该字符串结果是空的。

我通过在程序中包含system("top -n 1");行来测试该程序。但是我没有从adb shell获得任何输出(空字符串)。

我使用g ++编译相同的程序,并在linux pc中运行。那时我正在得到正确的输出。什么可能是我没有得到所需的android设备的adb外壳输出的原因?

+0

你是用'JNI'还是其他方式运行这个'C++'代码? – Sma

+0

我不使用JNI。基本上我只是建立这个程序到设备兼容的可执行文件,并使用'adb push'命令将此可执行文件放到system/bin文件夹中。然后我进入adb shell并从'bin'执行程序。 – deepz

+0

您的测试设备上是否有3个程序(顶部,头部和尾部)? –

回答

0

当你建立一个使用命令

arm-linux-gnueabi-g++ -static -march=armv7-a name.cpp -o test 

创建静态二进制程序。为了链接android中的库,该程序必须使用android ndk build构建。这解决了我的问题。

相关问题