Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't gcc find the random() interface when -std=c99 is set?

Tags:

c

random

gcc

c99

c89

I do "#include <stdlib.h>" at the top of the source.

Example compilation:

/usr/bin/colorgcc -std=c99 -fgnu89-inline  -g -Wall -I/usr/include -I./ -I../ -I../../ -I../../../ -I../../../../    -O3 -o f8  f8.c
In file included from f8.c:7:
ctype-cmp.c: In function ‘randomized’:
ctype-cmp.c:48: warning: implicit declaration of function ‘random’
ctype-cmp.c: In function ‘main’:
ctype-cmp.c:153: warning: implicit declaration of function ‘srandom’
ais@xcalibur:t$ 

When I turn off -std=c99, the function isfinite() can not be found. So I do want to use -std=c99 for this and other reasons. Is there some trick I'm missing?

like image 911
Setjmp Avatar asked Feb 22 '09 18:02

Setjmp


People also ask

How does random () work in C?

DESCRIPTION The rand() function returns a pseudo-random integer in the range 0 to RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]). The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand().

What library is Rand in C?

The rand function, declared in stdlib. h, returns a random integer in the range 0 to RAND_MAX (inclusive) every time you call it. On machines using the GNU C library RAND_MAX is equal to INT_MAX or 231-1, but it may be as small as 32767.

How do you get a random number between 0 and 1 in C++?

C++ Random Number Between 0 And 1 We can use srand () and rand () function to generate random numbers between 0 and 1. Note that we have to cast the output of rand () function to the decimal value either float or double.


1 Answers

man srandom says that the function is not part of C99 but part of POSIX.

Activate _BSD_SOURCE or _XOPEN_SOURCE >= 500 or any other suitable feature test macro that declares the srandom/random function (see man feature_test_macros and man srandom).

This one has good chances, but you need to figure out the macros that are defined/not defined implicitly thereby too by reading the manpages above.

/usr/bin/colorgcc -std=c99 -D_XOPEN_SOURCE=600 -fgnu89-inline -g -Wall 
    -I/usr/include -I./ -I../ -I../../ -I../../../ -I../../../../ -O3 -o f8  f8.c
like image 168
Johannes Schaub - litb Avatar answered Sep 19 '22 16:09

Johannes Schaub - litb