Sunday, March 13, 2016

where is fmin and fmax defined in gcc?

Not in float.h or math.h.

Clearly defined somewhere from the linker complaints.  I tried writing a macro to do it (for all the risks that entails) but finally broke down and wrote a local fmin function to do what I needed.  Boy do I feel old and stupid.

5 comments:

  1. #include

    Put -lm on the gcc -o command line.

    ReplyDelete
  2. Add -lm to your gcc command line. This links in the math library.

    ReplyDelete
  3. Somebody's probably already gotten to you, but this is what I found:

    fmin() is defined in nr.com (according to ftp://ftp.cpc.ncep.noaa.gov/wd51we/random_phase/nr.h). I was unable to find the text for math.h but I suspect fmin() is found there.
    For some reason, fmax() and its variants are defined in math.h (at least, that's what I inferred from code snippets I saw. Someone else might have had the same problem you did and posted to stackoverflow. In fact, there seems to be an issue with math.h because you get lots of hits if you google 'gcc math.h.' This link might help: http://stackoverflow.com/questions/11336477/gcc-will-not-properly-include-math-h

    I hope that's helpful, but without a linux system and actual header files to look at, I'm having to guess. I've become suspicious of the text of the nr.h file I found.

    You seem to have pull with google because when I did a google search of 'fmin gcc', I got this request on your blog!

    ReplyDelete
  4. [hald@localhost ~]$ cat testfmin.c
    #include
    #include

    int main(int argc, char *argv[]);

    int main(int argc, char *argv[])
    {
    volatile double x, y, z;
    z = fmin(x, y);
    exit(0);
    return 0;
    }
    [hald@localhost ~]$ gcc -g -O0 -o testfmin testfmin.c
    /tmp/cc5gdKAK.o: In function `main':
    /home/hald/testfmin.c:9: undefined reference to `fmin'
    collect2: error: ld returned 1 exit status
    [hald@localhost ~]$ gcc -g -O0 -o testfmin testfmin.c -lm
    [hald@localhost ~]$

    ReplyDelete
  5. that may depend on which release of the C standard is default in your copy of GCC.

    One of the online references I've found claims that "fmin" and "fmax" were added to the C99 standard.

    http://en.cppreference.com/w/c/numeric/math/fmin

    I don't know that GCC supports the C99 standard fully, depends on which version of GCC you have installed.

    ReplyDelete