Zhanhailiang Date:
When using GCC to compile the following code, "undefined reference to 'sin'" is reported '":
#include <stdio.h>#include <math.h>#include <stdlib.h> main () { double a = sin(1); exit (0);}
[root@~/wade/codeReview/learningc/9]# gcc -o timetest timetest.c /tmp/ccIzIHF7.o: In function `func‘:timetest.c:(.text+0x3f): undefined reference to `sin‘collect2: ld returned 1 exit status
This is generally due to the lack of a mathematical library. You only need to manually add the GCC libm. So library during compilation, as shown below:
[root@~/wade/codeReview/learningc/9]# gcc timetest.c -lm -o timetest[root@~/wade/codeReview/learningc/9]# ./timetest
Specify the path of the corresponding library through-L. The default path is the path of the system library, such as/lib and/usr/lib64 (64-bit )(This default system library path value will be modified after confirmation by the authorIn Linux, the Unified Library naming rules are lib ***. So. Therefore,-LM indicates that GCC will go to the system library path by default to find the libm. So library, as shown below:
[root@/usr/lib64]# find /usr/lib64/ |grep "libm.so"/usr/lib64/libm.so[root@/usr/lib64]# find /lib |grep "libm.so"
Undefined reference to 'sin' Solution