Question

So I have the following code:

#include <math.h>
int main (void) {
float max = fmax (1.0,2.0);
return 0;
}

Which compiles and runs fine, but if instead of passing 1.0 and 2.0 to the function I pass a, b with those values:

#include <math.h>
int main (void) {
float a = 1.0; float b = 2.0;
float max = fmax (a,b);
return 0;
}

I get the following error:

undefined reference to `fmax'

What is the diffrence? What I'm doing wrong?

I'm using this command to compile:

c99 fmax_test.c
Was it helpful?

Solution

In the first case fmax probably gets optimised away at compile time. In the second case it does not and you then get a link error. Without knowing what compiler you are using it's hard to give a specific remedy, but if it's gcc then you may need to add -lm, e.g.

c99 -Wall fmax_test.c -lm

Note also that fmax is for doubles - you should be using fmaxf for floats.

OTHER TIPS

compile with -lm i'm using gcc. maybe not OK with your compiler.

try this:

c99 fmax_test.c -lm
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top