It is C problem, but

$ cat test_getentropy.c                  #define _DEFAULT_SOURCE
#include <sys/random.h>
#include <stdio.h>
int main(){                 
    char buf[1];
    int r = getentropy(buf, 1);
    printf("Function %s. Return value test: %d\n", r == 0 ? "succeeded" : "failed", r);
    return 0;                                  }

~/z/C $ clang test_getentropy.c
test_getentropy.c:6:13: error:                       call to undeclared function 'getentropy';      ISO C99 and later do not support implicit
      function declarations
      [-Wimplicit-function-declaration]
    6 |     int r = getentropy(buf, 1);
      |             ^
1 error generated.

~/z/C $ uname -a
Linux localhost 5.4.254-android12-9-g619997ff2210 #1 SMP PREEMPT Fri Feb 7 14:59:01 CST 2025 aarch64 Android
```

Any suggestion is appreciated !

Did you look in those headers to see if they actually define getentropy?

All the quick web search references I saw in 1 minute in the browser says that getentropy is in unistd.h

That was what I was about to say but it didn’t work. atleast for me

#include <unistd.h>
#include <stdio.h>

int main() {
    char buf[1];
    int r = getentropy(buf, 1);
    printf(“Function %s. Return value test: %d\n”, r == 0 ? “succeeded” : “failed”, r);
    return 0;
}
entro.c:7:10: error: call to undeclared function 'getentropy'; ISO C99
      and later do not support implicit function declarations
      [-Wimplicit-function-declaration]
    7 |         int r = getentropy(buf, 1);
      |

well here’s a working version

#include <stdio.h>

int getentropy(void *buf, size_t len);

int main(){
    char buf[1];
    int r = getentropy(buf, 1);
    printf("Function %s. Return value test: %d\n", r == 0 ? "succeeded" : "failed", r);
    printf("%x\n", buf[0]);
    return 0;
}

still not sure in which header the function is located

1 Like

yeah, by adding

int getentropy(void *buf, size_t len);

It works !

The problem is the -std=c99 flag, use -std=gnu99

EDIT: If you don’t pass the flag directly, check the CFLAGS environment variable contents.

2 Likes

The problem is that the compiler couldn’t find that function’s declaration. Sure switching the standard would make the code work in this case but I don’t want code like this to pass the compilation stage

int r = getentropeepee(buf, 1);

also gnu99 will still give that error at compile tine. you gotta go c90 or lower for it to compile

1 Like

Man pages are always useful for this sort of thing. On my linux laptop:

#include <unistd.h>

       int getentropy(size_t length;
                      void buffer[length], size_t length);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       getentropy():
           _DEFAULT_SOURCE

Try adding -D_DEFAULT_SOURCE to your compilation line.

1 Like
$ gcc -std=c90 test_getentropy.c
$ ./a.out
Function succeeded. Return valuetest: 0

Thanks for all your information !