including stdlib.h 
Author Message
 including stdlib.h

i just came across something that seems really weird to me.  i'm
on a solaris box running SunOS 5.5 with gcc 2.6.3.  i've got a program
that's doing some stuff with random numbers and uses srand48, time, and
drand48.  when i compile with -Wall i get a message saying these functions
are implicitly declared - this is borne out in the code - they all seem
to return 0s.  but i have included <stdlib.h> and pasting the entries from
there directly into the file seems to fix the problem.  perhaps more
strangely - i can use malloc w/out any problems!  anyway, i've gotten
lots of good words from this group before so i thought i'd try again:
anyone know what's going on here?  i won't post code unless asked....
--



Tue, 12 Feb 2002 03:00:00 GMT  
 including stdlib.h
follow-up to my last post:  i looked in stdlib.h and these functions
are guarded by :
#if defined(__EXTENSIONS__) || \
        (__STDC__ == 0 && !defined(_POSIX_C_SOURCE)) || \
        (defined(_XOPEN_SOURCE) && (_XOPEN_VERSION - 0 == 4))

so i guess i should change my question:  does anyone know what the
general rules are for when you should define these things or what
exactly should be defined?  i ran into a similar problem earlier
trying to use fdopen....  at some level it allmost seems like the
compiler should be doing this for me; i checked gcc options though
and it doesn't have anything like this.  
--



Tue, 12 Feb 2002 03:00:00 GMT  
 including stdlib.h
Paraphrase from the first post:

Compiling code that uses srand48(), time(), and drand48(), with
gcc's option to complain about calls to functions that have not
been declared, complains about these calls, even though the code
does include <stdlib.h>.


Quote:

>follow-up to my last post:  i looked in stdlib.h and these functions
>are guarded by :
>#if defined(__EXTENSIONS__) || \
>        (__STDC__ == 0 && !defined(_POSIX_C_SOURCE)) || \
>        (defined(_XOPEN_SOURCE) && (_XOPEN_VERSION - 0 == 4))

The problems are: (a) srand48() and drand48() are not ANSI C
functions; (b) time() is an ANSI C function but its prototype
declaration is in <time.h>.

Quote:
>so i guess i should change my question:  does anyone know what the
>general rules are for when you should define these things or what
>exactly should be defined?  i ran into a similar problem earlier
>trying to use fdopen....

The fdopen() function is also not an ANSI C function.

Quote:
>at some level it allmost seems like the compiler should be doing
>this for me ...

If you want the compiler to handle only "pure ANSI C", you are
doing the right thing already.

If you want to compile "impure" C, or use some add-on dialect such
as "POSIX C", well, this is the wrong newsgroup for that.  The
comp.unix.programmer and comp.unix.solaris groups would be better
places.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc


--



Wed, 13 Feb 2002 03:00:00 GMT  
 including stdlib.h

wrote in comp.lang.c.moderated:

Quote:
> follow-up to my last post:  i looked in stdlib.h and these functions
> are guarded by :
> #if defined(__EXTENSIONS__) || \
>         (__STDC__ == 0 && !defined(_POSIX_C_SOURCE)) || \
>         (defined(_XOPEN_SOURCE) && (_XOPEN_VERSION - 0 == 4))

> so i guess i should change my question:  does anyone know what the
> general rules are for when you should define these things or what
> exactly should be defined?  i ran into a similar problem earlier
> trying to use fdopen....  at some level it allmost seems like the
> compiler should be doing this for me; i checked gcc options though
> and it doesn't have anything like this.  

The problem is that with one exception, the functions you are asking
about in this post and the first one are not standard C.  time() is an
ANSI/ISO standard function, but it is prototyped in <time.h> and not
<stdlib.h>.

As for srand48(), drand48(), and fdopen(), they literally are not part
of the C language but are compiler and OS specific extensions.
fdopen() is part of the POSIX standard, the others aren't part of any
standard at all.

You are most likely compiling with some switch for ANSI conformance,
which causes the guards like the one you posted to make the
non-standard extensions disappear.


what command line switches you should be using.

Jack Klein
--
Home: http://home.att.net/~jackklein
--



Wed, 13 Feb 2002 03:00:00 GMT  
 including stdlib.h

Quote:

> follow-up to my last post:  i looked in stdlib.h and these functions
> are guarded by :
> #if defined(__EXTENSIONS__) || \
>         (__STDC__ == 0 && !defined(_POSIX_C_SOURCE)) || \
>         (defined(_XOPEN_SOURCE) && (_XOPEN_VERSION - 0 == 4))

Read the gnu-libc manual and you will see what is expected under what
conditions

Regards
Friedrich
--



Wed, 13 Feb 2002 03:00:00 GMT  
 including stdlib.h


o.k. - thanks for all the responses from everyone.
i'll go pester another newgroup :)

Quote:
> On Fri, 27 Aug 1999 16:34:06 GMT,


Quote:
> wrote in comp.lang.c.moderated:

> > follow-up to my last post:  i looked in

stdlib.h and these functions
Quote:
> > are guarded by :
> > #if defined(__EXTENSIONS__) || \
> >         (__STDC__ == 0 &&

!defined(_POSIX_C_SOURCE)) || \
Quote:
> >         (defined(_XOPEN_SOURCE) &&

(_XOPEN_VERSION - 0 == 4))
Quote:

> > so i guess i should change my question:  does

anyone know what the
Quote:
> > general rules are for when you should define

these things or what
Quote:
> > exactly should be defined?  i ran into a

similar problem earlier
Quote:
> > trying to use fdopen....  at some level it

allmost seems like the
Quote:
> > compiler should be doing this for me; i

checked gcc options though
Quote:
> > and it doesn't have anything like this.

> The problem is that with one exception, the

functions you are asking
Quote:
> about in this post and the first one are not

standard C.  time() is an
Quote:
> ANSI/ISO standard function, but it is prototyped
in <time.h> and not
> <stdlib.h>.

> As for srand48(), drand48(), and fdopen(), they

literally are not part
Quote:
> of the C language but are compiler and OS

specific extensions.
Quote:
> fdopen() is part of the POSIX standard, the

others aren't part of any
Quote:
> standard at all.

> You are most likely compiling with some switch

for ANSI conformance,
Quote:
> which causes the guards like the one you posted
to make the
> non-standard extensions disappear.

> You really need to ask in one of the


Quote:
> what command line switches you should be using.

> Jack Klein
> --
> Home: http://home.att.net/~jackklein
> --
> comp.lang.c.moderated - moderation address:


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
--



Sat, 16 Feb 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Preprocessor # 1 "/usr/include/stdlib.h" 1 3

2. got 'strrchr' undefined even I include stdlib.h

3. When do you #include <stdlib.h>?

4. Missing includes in /usr/include/linux/*.h and /usr/include/asm.h

5. ltoa in stdlib.h

6. RAND_MAX & stdlib.h

7. Problems with strtol() from <stdlib.h>

8. how to deal with stdlib.h

9. stdlib.h

10. malloc() type mismatch warnings despite #include <stdlib.h>

11. stdlib.h

12. malloc.h vs stdlib.h

 

 
Powered by phpBB® Forum Software