网上参考资料:
[root@linux ~]# vi sin.c
#include <stdio.h>
int main(void)
{
float value;
value = sin ( 3.14 / 2 );
printf("%f/n",value);
}
# 这个文件的内容可以在下面取得。
# http://linux.vbird.org/linux_basic/0520source/sin.c
我们先直接进行编译:
[root@linux ~]# gcc sin.c
sin.c: In function 'main':
sin.c:5: warning: incompatible implicit declaration of built-in function 'sin'
/tmp/cciDlilg.o(.text+0x 2c ): In function `main':
sin.c: undefined reference to `sin'
collect2: ld returned 1 exit status
# 注意上面最后一行,有个错误消息,表示没有成功。
特别注意上面的错误消息,说明没有编译成功,意指“undefined reference to sin”,表示“没有sin的相关定义参考值”,这是因为C语言里的sin函数是写在libm.so函数库中,而我们并没有在源码里加入相关的说明,所以需要在编译与连接的时候,将这个函数库连接进执行文件。我们可以这样做: