Douglas B. Staple
Douglas B. Staple

Reputation: 10946

Can't find math.h constant with -std=c99?

I have the simple test code, t.c:

#include <stdio.h>
#include <math.h>

int main(){
  printf("%f\n", M_LN10);
}

On one of my systems (OS X 10.8.4, GCC 4.8.1), this compiles fine. Strangely, on another system (Ubuntu 12.04.2 LTS, GCC 4.6.3) this compiles fine with gcc t.c, but if I do a gcc -std=c99 t.c I get:

t.c: In function ‘main’:
t.c:5:18: error: ‘M_LN10’ undeclared (first use in this function)
t.c:5:18: note: each undeclared identifier is reported only once 
for each function it appears in

I see no reason why GCC finds and accepts M_LN10 in math.h no problem with the default C-standard, but not if C99 is enforced. Any idea what's going on here?

Upvotes: 3

Views: 912

Answers (1)

shanet
shanet

Reputation: 7324

Add -D_BSD_SOURCE or -D_XOPEN_SOURCE to your GCC command. Something like gcc -std=c99 -D_XOPEN_SOURCE t.c

After looking in the math.h file on my system, M_LN10 is defined as such:

#if defined __USE_BSD || defined __USE_XOPEN
# define M_LN10         2.30258509299404568402  /* log_e 10 */
#endif

Upvotes: 8

Related Questions