66457

Can -std=c99 prevent my #includes from working properly?

Question:

I am trying to compile a C program on a Linux system. I have an #include statement for stdlib.h.

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).

Given that 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 #include statements.

I'm using gcc 4.1.2-50 (Red Hat).

Answer1:

<blockquote>

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 -D_SVID_SOURCE parameter.

But perhaps an easier way is to just use C99 with the GNU extensions. To do that, replace -std=c99 with -std=gnu99.

And, for bzero and close, these can be obtained from strings.h and unistd.h respectively.

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.

Neither strings.h (note the plural name, this is <em>not</em> string.h) nor unistd.h are part of ISO C.

Answer2:

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>

The flag -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?

Also, using -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=gnu99.

Answer4:

-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:

-D_POSIX_C_SOURCE=200809L

<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.

Answer5:

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 strings.h for bzero, see also paxdiablo's answer.

Answer6:

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 <stdlib.h>.

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 gcc.

For srand48() (and drand48()) you probably want either -D_XOPEN_SOURCE=500 or -D_SVID_SOURCE (or #define _XOPEN_SOURCE 500 etc. in the source files).

bzero() and close() should work even with -std=c99 if you #include the documented header files for them, which are <strings.h> and <unistd.h> respectively.

Recommend

  • Unable to build feature modules in a multi-flavor app
  • Swift_TransportException error in laravel
  • Eclipse project.properties backslash paths considered harmful
  • How to turn of electric-indent-mode for specific Major mode?
  • Can't create itunesconnect sandbox tester
  • Using true and false as the expressions in a conditional operation
  • How to display the median value in a faceted boxplot in ggplot?
  • How to find the midPoint of arc in SVG with javascript
  • Validation is not working in codeigniter
  • Can PHP be used to control a GUI application?
  • How to Backup and Restore a MySQL Database using NetBeans?
  • javascript use drag to move element
  • port not opening even after adding rule in ufw?
  • Split string on comma and ignore comma in double quotes [duplicate]
  • Extracting table data from a website using R [closed]
  • Swift: How to continuously send an action from a NSTextField
  • How to set md-select panel to be open by default
  • CSS/HTML: How to add a dropdown menu to existing navbar?
  • $http.jsonp call from angular app returns errors
  • SQL Server stored procedure runs fast in SSMS and slow in application [duplicate]
  • The initialization of non-local non-inline variables: does it take place strictly before the `main()
  • Control access to WebDav/Apache using Python
  • Genetic Programming Implementation
  • RestKit - Process one REST operation at a time
  • How to access a bundled ES6 class in inline
  • I am trying to create an app in android to insert data into sql server through a web service.
  • How to set and check a session after login?
  • Image insertion from SQL info
  • Why does packing not work across sibling unions or structs
  • pass sessionid through jquery ajax call to php
  • Windows biometric framework sample umdf driver: This device cannot start. (Code 10)
  • playing mp3 from nsbundle
  • Visual Studio 2017 Professional- Unable to find package at source
  • How to join two tables from different databases
  • How to create subsets of a single set of elements with XSLT?
  • Creating 2d platforms using JavaScript
  • How to write seo friendly url's using htaccess?
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • how to snap two objects in runtime in unity?
  • How to check if object is null in Java?