1

When i compile the following code snippet I get this error message from my compiler:

/tmp/ccT1yBa1.o: In function `main':
test.c:(.text.startup+0x34): undefined reference to `log'
collect2: error: ld returned 1 exit status

_

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <math.h>

void main(){

srand(time(NULL));
double r1 = (rand() % 1000)/1000.0;
double r2 = log(r1);
printf("%lf\n",r2);

}

Compiled with

gcc -O2 test.c

What's wrong?

  • 3
    You need [-lm after the source file](http://stackoverflow.com/questions/19372317/c-failing-to-compile-cant-find-math-h-functions/19372412#19372412) – Shafik Yaghmour Jul 01 '14 at 14:31

1 Answers1

2

You have forgot the -lm to link with the math library.

gcc -O2 test.c -lm
jmlemetayer
  • 4,774
  • 1
  • 32
  • 46