Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

linux

gcc

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

like image 561
Raven Dreamer Avatar asked Feb 04 '11 00:02

Raven Dreamer


People also ask

What are some symptoms of the COVID-19 Omicron subvariant?

Compared to other SARS-CoV-2 variants, the Omicron variant is associated with generally less severe symptoms that may include fatigue, cough, headache, sore throat or a runny nose.

How effective is Paxlovid?

The data showed that participants (all of whom were unvaccinated) who were given Paxlovid were 89% less likely to develop severe illness and death compared to trial participants who received a placebo.

How long after a positive COVID-19 test do you remain contagious?

Those who do get infected with mild-to-moderate COVID-19 will likely remain infectious no longer than 10 days after symptoms begin. Individuals with severe-to-critical illness stemming from a COVID infection likely aren't infectious 20 days after symptoms first began.

Can you get COVID-19 from sex?

All close contact (within 6 feet or 2 meters) with an infected person can expose you to the virus that causes coronavirus disease 2019 (COVID-19) — whether you're engaged in sexual activity or not.


2 Answers

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

No, but they may show up limitations in your knowledge of how they work :-)


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 feature test macros.

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 other 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 nothing to do with C99 but then I realised that flag only controls what the standard C headers give you.

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

like image 106
paxdiablo Avatar answered Sep 18 '22 19:09

paxdiablo


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: https://bugzilla.redhat.com/show_bug.cgi?id=130815

The flag -D_POSIX_C_SOURCE=200809L should work.

See also this question: Why can't gcc find the random() interface when -std=c99 is set?

like image 28
Kevin Avatar answered Sep 19 '22 19:09

Kevin