undefined reference to `tan' but math.h has been included

gcc, math

Solution

You should add -lm to your compiler option.

Besides of that, you could also change -lpthread to -pthread.

Problem

I want to compile the sample pthread code from http://pages.cs.wisc.edu/~travitch/pthreads_primer.html (Mutex section). As I run this command: ``` gcc -pedantic -Wall -o theaded_program pth.c -lpthread ``` which is stated in the link, I get this error ``` pth.c:45:5: warning: ISO C90 forbids mixed declarations and code [-pedantic] /tmp/ccajksBv.o: In function `opponent': pth.c:(.text+0x4a): undefined reference to `tan' /tmp/ccajksBv.o: In function `main': pth.c:(.text+0x131): undefined reference to `tan' collect2: ld returned 1 exit status ``` However `#include <math.h>` is there in the code!! The gcc version is 4.6

Original source