views:

1505

answers:

5

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. There must be some trick I am missing?

Thanks Stackoverflow!

Andy

A: 

I use rand() and srand(). BTW: Did you forget a header or two? At least the second warning tells me so.

Try including math.h. (Just remembered we always had issues with math library and had to actually force link it with -lm).

dirkgently
I had to down vote this response since correct header's were included (both stdlib and math). The trick was defining the macro. Thanks for the response!
SetJmp
I didn't see you had mentioned math.h
dirkgently
A: 

I'm not getting this error, so it's likely something fairly specific to your code. It'd help if you posted

  1. your sources, or a stripped down version of them that reproduce the issue, and
  2. your compiler version, and
  3. your operating system.

Having said that, my gcc does not recognize -fgnu89-inline, so we're definitely on different versions.

FWIW, isfinite() is part of math.h - do you include that as well?

Thanks for your response! The problem has been solved (see above). math.h was included and my gcc was:xcalibur:t[1]$ gcc --versiongcc (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu3)It is curious that you did not get this error.
SetJmp
Not so curious, I'm on a BSD-derivate.
+5  A: 

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
Johannes Schaub - litb
+1  A: 

Yes, there is a trick you are missing: you can use -std=gnu99 instead of -std=c99.

-std=c99 #defines __STRICT_ANSI__, which /usr/include/features.h interprets as "do not enable anything outside the C standard by default" (without it, you get at least both _SVID_SOURCE and _BSD_SOURCE). -std=gnu99, on the other hand, means "C99 plus GNU extensions" (the gcc default is currently -std=gnu89, its C89 equivalent, which is why you needed to specify something to get the new C99 features).

As an alternative, you can enable the feature test macros (as mentioned in @litb's answer). Looking at /usr/include/stdlib.h in my system, it expects one of __USE_SVID, __USE_XOPEN_EXTENDED, or __USE_BSD. /usr/include/features.h tells me that the feature test macros which would enable these are:

  • _SVID_SOURCE (enables __USE_SVID)
  • _BSD_SOURCE (enables __USE_BSD)
  • _XOPEN_SOURCE with a value of at least 500 (enables __USE_XOPEN_EXTENDED)
  • _XOPEN_SOURCE_EXTENDED (also enables __USE_XOPEN_EXTENDED)
  • _GNU_SOURCE (enables everything, including the four feature test macros above)

For new programs where you are not too concerned about potential name collisions with new functions from future standards, using both -std=gnu99 and -D_GNU_SOURCE is a good idea. It allows you to use all the new standard features and GNU extensions, which combined with some sort of fallback (for instance, autoconf-style feature tests) gives the most flexibility.

References:

CesarB
A: 

I've created random numbers using gcc in CodeBlocks under Ubuntu 9.10 (with compiler options: -std=gnu99 -D_GNU_SOURCE) So this worked for me:

This is my code I had played with:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
enum computer {keyboard, CPU, screen, printer};
int main(void)
{
  enum computer comp;
  time_t casovac;
  comp = CPU;
  srand(&casovac);
  printf("%d", rand());
  return 0;
}

This was only idea, of course you can accomplish it by other ways ;-) [To install CodeBlocks use: sudo apt-get install build-essential and then sudo apt-get install codeblocks]

sick_fox