cc89ansi-c

Implicit declaration of function fmax


I have the following code:

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

int main(void) {
    printf("%f\n", fmax(1.2, 3.4));
    return 0;
}

If I compile with:

gcc a.c -o a && ./a

then I get the expected output:

3.400000

If I try to enable warnings though and target C89, I can't get it to compile:

$ gcc -Wall -Wextra -std=c89 -pedantic -Wstrict-prototypes a.c -o a
a.c: In function ‘main’:
a.c:5:5: warning: implicit declaration of function ‘fmax’ [-Wimplicit-function-declaration]
a.c:5:5: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat]
/tmp/cc8d2iQl.o: In function `main':
a.c:(.text+0x1d): undefined reference to `fmax'
collect2: ld returned 1 exit status
$ gcc -Wall -Wextra -std=c89 -pedantic -Wstrict-prototypes a.c -lm -o a
a.c: In function ‘main’:
a.c:5:5: warning: implicit declaration of function ‘fmax’ [-Wimplicit-function-declaration]
a.c:5:5: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat]

I found out that fmax() is only defined by the C99 standard, not C89. So the question is: why do these exact same commands work without issuing any warning on a Mac, but not on a Linux machine?


Solution

  • I think you need to build with -std=c99 (see the manual page for fmax).. see this

    From fmaxf manual page

    fmax(), fmaxf(), fmaxl():
    _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L;
    or cc -std=c99
    

    It seems fmax also requires C99