I am trying to compile a C program on a Linux system. I have an
#include statement for
When I compile the program with
gcc as follows:
gcc -std=c99 -g -o progfoo progfoo.c progbar.c
I get warnings about
Implicit declaration of function [srand48, drand48, bzero, or close].
Compiling instead as:
gcc -g -o progfoo progfoo.c progbar.c
doesn't give me the warnings, but it does yell about my use of
for loops (which was the rationale for adding
-std=c99 in the first place).
man srand48 mentions including
<stdlib.h>, which I have, I'm unsure what else the problem could be. The
for loops aren't essential to anything (they were just to save time in initializing an array) so I have no problem removing them, but before I do I'd like to confirm whether the
c99 standard is superseding some aspect of my
gcc 4.1.2-50 (Red Hat).
Can -std=c99 prevent my #includes from working properly?</blockquote>
No, but they may show up limitations in your <em>knowledge</em> of how they work :-)<hr />
While the functions
[sd]rand48 have a prototype in
stdlib.h, they're inside an
#ifdef, at least on my system:
#if defined __USE_SVID || defined __USE_XOPEN
So you will probably have to explicitly set one of those macros.
However, before you try it, be aware that it doesn't work. That's because all this stuff is controlled with
gcc's <a href="http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html" rel="nofollow">feature test macros</a>.
There's a very complicated set of rules used to set specific features on or off in
features.h and the macros created there control what the header files include and exclude. The
__USE_* variants are cleared and set in that header file based on <em>other</em> macros provided by yourself.
For example, to get
__USE_SVID set so you can use
srand48, you need to provide the compiler with a
But perhaps an easier way is to just use C99 with the GNU extensions. To do that, replace
close, these can be obtained from
I was a little confused at first as to why these compiled with
-std=c99 when they have absolutely <em>nothing</em> to do with C99 but then I realised that flag only controls what the <em>standard C headers</em> give you.
strings.h (note the plural name, this is <em>not</em>
unistd.h are part of ISO C.
Looks like the functions that you are using are not ISO C99, so when you request strict C99 compliance they will not be visible.
Information here: <a href="https://bugzilla.redhat.com/show_bug.cgi?id=130815" rel="nofollow">https://bugzilla.redhat.com/show_bug.cgi?id=130815</a>
-D_POSIX_C_SOURCE=200809L should work.
See also this question: <a href="https://stackoverflow.com/questions/575350/why-cant-gcc-find-the-random-interface-when-std-c99-is-set" rel="nofollow">Why can't gcc find the random() interface when -std=c99 is set?</a>Answer3:
The error you're getting makes it sound like the functions you're using aren't being declared. Are you sure you're including the correct headers for them?
-std=c99 may disable some extensions which are not part of the standard. None of the functions you mentioned are part of the C standard. If you can't find separate headers for them, you might try
-std=c99 causes the headers to omit anything that could clash with uses of names outside the C99 reserved namespace, including all standard POSIX functions. The portable way to request the headers give you the POSIX interfaces is to define
_POSIX_C_SOURCE to a value corresponding to the desired POSIX version. For the latest POSIX (2008), this means:
#define _POSIX_C_SOURCE 200809L
or on the command line:
<strong>Edit:</strong> It seems the functions you want are not in the POSIX base but in the XSI option, so you should define
_XOPEN_SOURCE to an appropriate value (
700 is the latest) to get them. This too can be done from the command line or your source files (but if from the source files, it must be done <strong>before</strong> including any system headers.
Implicit declarations (where an undeclared function is assumed to return int) are no longer allowed in C99.
That said, gcc will not abort the compilation.
Try to include
bzero, see also paxdiablo's answer.
You're asking for standards compliance, and <a href="http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf" rel="nofollow">C99</a> doesn't define
srand48() as a function provided by
For the GNU C library, you can request additional features by defining one or more of the options listed in the comment at the top of
/usr/include/features.h, either by
#define before you
#include, or with
-D flag to
drand48()) you probably want either
#define _XOPEN_SOURCE 500 etc. in the source files).
close() should work even with
-std=c99 if you
#include the documented header files for them, which are