我正在编写C++代码以在android设备中执行“top”命令。这是我使用的代码。

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 shell中获得所需输出的原因可能是什么?

最佳答案

使用命令构建程序时

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

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

09-25 17:19
查看更多