gnu c lib log2

My program use log2().
pgCC didn’t find log2() in math.h.
How to solve this problem?

Hi YJS -

I think you’ll find the log2 function in the libm library. If you add -lm to your link line, the compiler driver should find it for you.


-lm didn’t solve the problem.
log2() comes from c99 according to some book.
Even pgCC use -c9x,nothing change.

Can you post the compiler/link line and error messages? If you have a pointer to the source, that would be helpful too.

“” ,line 45 : error:identifier “log2” is undefined

Hi YJS -

Can you try compiling this with pgCC -D_GNU_SOURCE and see if that helps and gives you the right answer?


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

main ()
int fingerCount;
int vsEstimateCount;



It works.
How did you know that?

Hi yjs -

I think what happens is that log2 is part of the ISOC99 standard and I believe it is defined in mathcalls.h. To include it, features.h, when given that _GNU_SOURCE flag I think set the _USE_ISOC99 flag for the rest of the includes. And then some where the compile includes something to start the chain of events. I think GNU is waiting for wider adoption of ISOC99 before including it by default, at least that is my understanding.

It looks like the funtion is in the libm.a library - just a matter of getting the definition introduced to your routine. So you were on the right path initially looking for it in the header file. Once I saw it was a compile error and not a link error I remembered reading something about this.

Glad it’s working for you.