在 android NDK 上弹出 [英] popen on android NDK

查看:26
本文介绍了在 android NDK 上弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

android NDK 不支持 popen 吗?

Is popen not supported by android NDK?

我阅读了 这个页面并想知道这是不是真的

I read this page and wondering if this is true

POSIX popen() 也可以,但目前还不行受仿生支持,因此您不能在 Android JNI 中使用它.相反,你可能可以使用 system() 并将输出通过管道传输到文件,然后之后阅读该文件.看起来Java方法将是如果您将使用 Java 进行渲染,则更清晰.

The same is possible with POSIX popen() but it is not currently supported by bionic, so you can't use that in Android JNI. Instead you can probably use the system() and pipe the output to a file and then read that file afterwards. Looks like the Java approach will be cleaner if you will be doing the rendering in Java.

但我也读到有人建议使用 popen.我自己也试过,但有时我的应用程序崩溃了,我不知道为什么.

But I also read someone suggesting to use popen. I also tried it myself but sometime my app crashes and I dont know why.

popen 在 android ndk 中使用安全吗?

Is popen safe to use in android ndk?

推荐答案

我觉得要看你用的是什么版本的NDK

I think it depends on what version of the NDK you are using

同时在姜饼源代码树中寻找仿生,我确实找到了 popen 的实现.实现可能不是 100% 正确的 libc,但它至少在某种程度上是功能性的.

Also looking in the gingerbread source tree for bionic I did find an implementation of popen. The implementation might not be 100% posix of libc correct, but it is at least functional to some degree.

使用 NDK v6,以下示例编译没有任何问题,并且可以在我的 android 设备上运行.

Using NDK v6, the following example compiles without any problems, and it runs on my android device.

#include <stdio.h>
#include <stdlib.h>
int main()
{
 FILE *fpipe;
 char *command="/system/bin/ps";
 char line[256];
 if ( !(fpipe = (FILE*)popen(command,"r")) ) exit(1)
 while ( fgets( line, sizeof line, fpipe))
 {
   puts(line);
 }
 pclose(fpipe);
}

更新:似乎在使用 vfork() 而不是 fork() 的前 ICS 版本上 popen,并且已知那些 vfork() 会导致堆栈损坏.

UPDATE: Seems than popen on pre ICS versions used vfork() instead of fork(), and those vfork() are known to cause stack corruption.

因此,从 ICS 开始,popen 应该可以保存使用,但在早期的 android 版本上它是可用的,但非常有问题.

So from ICS onward popen should be save to use, but on earlier android versions it is available but verry buggy.

这篇关于在 android NDK 上弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆