popen on android NDK

前端 未结 2 909
南笙
南笙 2020-12-18 11:00

Is popen not supported by android NDK?

I read this page and wondering if this is true

The same is possible with POSIX popen() but it is not cu

相关标签:
2条回答
  • 2020-12-18 11:23

    Indeed, popen() will not work on Android. As GLIBC description states, any UNIX operating system needs a C library to make all system calls. These include threading memory allocation, working with files, etc. Because Android is based on Linux, it needs such library implemented, but because it's a small mobile OS, Google decided to write a "lite version" of libc called bionic. This library does not include popen(), therefore you can't use it. There is a description of bionic library in the NDK documentation which lives inside your NDK directory (for some reason it's not online). Hope this helps.

    0 讨论(0)
  • 2020-12-18 11:30

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

    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.

    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);
    }
    

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

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

    0 讨论(0)
提交回复
热议问题