Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

math

gcc

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

like image 715
mahmood Avatar asked Sep 03 '25 06:09

mahmood


2 Answers

You should add -lm to your compiler option.

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

like image 153
rralf Avatar answered Sep 05 '25 01:09

rralf


In the end, it has to be like this: gcc -pedantic -Wall -o theaded_program pth.c -pthread -lm

like image 30
DevBush Avatar answered Sep 05 '25 01:09

DevBush