Problem compiling Math. h

Asked

Viewed 15 times

2

Good evening, when I try to use some function of Math. h and put as argument a variable and try to compile I get an error. Can you explain to me why this is happening?

Follow an example code:

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

int main(){

    int cacheSize = 4;
    int memSize = 16;

    // log de cacheSize na base 2
    double cacheBits = 0;
   cacheBits = log(cacheSize)/log(2);

   // log de memSize na base 2
   double memBits = 0;
   memBits = log(memSize)/log(2);

   return 0;
}

The error message I get:

gcc teste.c -o teste

/usr/bin/ld: /tmp/ccHF5avR.o: na função "main":
teste.c:(.text+0x25): referência não definida para "log"
/usr/bin/ld: teste.c:(.text+0x51): referência não definida para "log"
collect2: error: ld returned 1 exit status

1 answer

3


If you get this error it is because the implementation of the Math library is not being linked in the process. To resolve add the -lm in the compilation instruction:

gcc teste.c -o teste -lm

When it includes the math.h includes the definition of functions but not their implementations, which is why you see this error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.