Andree
Andree

Reputation: 3103

Linking Math library to a C90 code using GCC

I would like to compile a simple C90 code utilizing math library:

main.c:

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

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

I use GCC compiler and use option -ansi -pedantic to enforce C90 standard.

gcc -ansi -pedantic -lm main.c

But it doesn't compile. The following is the error message:

main.c: In function ‘main’:
main.c:7:25: error: ‘M_PI’ undeclared (first use in this function)
main.c:7:25: note: each undeclared identifier is reported only once for each function it appears in

My question is, why? Does C90 standard prohibits the use of math library?

Upvotes: 1

Views: 5259

Answers (2)

Darcy Rayner
Darcy Rayner

Reputation: 3445

M_PI isn't defined when strict iso standard is required. Look on this page under trigonometric functions. It is suggested that when using -ansi, just define it yourself:

#define M_PI 3.14159265358979323846264338327

Upvotes: 5

Foo Bah
Foo Bah

Reputation: 26251

M_PI is generally declared as a macro and there is an explicit guard #if !defined(_ANSI_SOURCE) (at least in OSX), which suggests that the ANSI implementation doesnt support it

for gcc you can also use -std=c90 to force C90

Upvotes: 2

Related Questions