android - 在 Android 设备中运行 C++ 程序

标签 android c++ linux shell

我正在编写 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

当我运行程序时,字符串result 为空。

我通过在程序中包含 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 构建程序。这为我解决了问题。

关于android - 在 Android 设备中运行 C++ 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45632346/

相关文章:

Xamarin Android 部署上的 Java、Lang.OutOfMemoryError

c++ - 你如何为 clang 和 gcc 编写一个 makefile?

c++ - 在 linux 上接受时相同的 SOCKET id?

java - Raspberry Pi 无法在 JavaFX 应用程序中隐藏鼠标光标

PHP 执行() : Running bash script to configure environment then executing Python program

java - Edittext 只允许字母(以编程方式)

android - 自定义 android 首选项类型在键入时失去焦点

java - Android - requestLocationUpdates 导致标识符预期错误

c++ - 如何修改这个基于 MFC 的代码片段以使用我自己选择的窗口类名?

c++ - 我在这个 lint 错误抑制尝试中做错了什么?还有更好的方法吗?