Ubuntu – Using arc4random on Ubuntu 14.04 trusty

ccompilinglibraries

I am currently working with a C++ program where I wish to use arc4random function. Every time I compile, I receive a not declared in scope message for arc4random. I understand that this is an error in my included libraries in my program, and yet I have included both stdlib.h and stdio.h which I believe are the libraries it uses? I have installed 'sudo apt-get install libpcl-all', as well as, 'sudo apt-get install build-essential'. I have scoured the web, but am still unsure what I need at this point. I am still very new at Ubuntu, so any help with this problem would be appreciated.

Best Answer

The arc4random function is a BSD utility that is not part of the standard C library on Ubuntu. To use it, I think that you will need to install the libbsd-dev package, and then include the BSD version of the header explicitly using

#include <bsd/stdlib.h>

You will then need to link your executable with the libbsd library by adding -lbsd to your compiler command line, for example

g++ -o prog -Wall prog.cpp -lbsd
Related Question