# man 3 sin.
一般来说,可以根据 link 使用的 flag 来推断用到的函数在哪个文件中。
比如 -lm , 相应的文件名应该为 libm.so,一般位于 /usr/lib/ 下面,即 /usr/lib/libm.so
比如我的机器上:
/tmp $ readelf -s /usr/lib/libm.so | grep sin
72: 0000000000030b80 466 FUNC WEAK DEFAULT 12 casinhf@@GLIBC_2.2.5
79: 0000000000038540 407 FUNC WEAK DEFAULT 12 casinhl@@GLIBC_2.2.5
113: 00000000000238c0 122 FUNC WEAK DEFAULT 12 asin@@GLIBC_2.2.5
116: 000000000002d960 223 FUNC WEAK DEFAULT 12 sinf@@GLIBC_2.2.5
119: 00000000000247f8 103 FUNC WEAK DEFAULT 12 sinh@@GLIBC_2.2.5
124: 00000000000356d0 67 FUNC WEAK DEFAULT 12 sinl@@GLIBC_2.2.5
132: 00000000000252d0 52 FUNC WEAK DEFAULT 12 sincos@@GLIBC_2.2.5
135: 000000000003018c 282 FUNC WEAK DEFAULT 12 casinf@@GLIBC_2.2.5
138: 0000000000026690 398 FUNC WEAK DEFAULT 12 casinh@@GLIBC_2.2.5
139: 0000000000037b14 262 FUNC WEAK DEFAULT 12 casinl@@GLIBC_2.2.5
194: 000000000002dcb4 119 FUNC WEAK DEFAULT 12 asinf@@GLIBC_2.2.5
195: 00000000000151dc 376 FUNC WEAK DEFAULT 12 asinh@@GLIBC_2.2.5
197: 0000000000035934 118 FUNC WEAK DEFAULT 12 asinl@@GLIBC_2.2.5
201: 000000000002f610 394 FUNC WEAK DEFAULT 12 sincosf@@GLIBC_2.2.5
205: 0000000000037190 46 FUNC WEAK DEFAULT 12 sincosl@@GLIBC_2.2.5
238: 000000000001b545 8034 FUNC WEAK DEFAULT 12 sin@@GLIBC_2.2.5
239: 0000000000025d30 275 FUNC WEAK DEFAULT 12 casin@@GLIBC_2.2.5
从中可以看到 sin 。
又比如 -lpthread, 则相应文件名为 libpthread.so, 以此类推。
可以通过“nm”命令查找我们想要找的函数,例如:sin函数,方法如下:
[root@lyb root]#nm –o /lib/*.so|grep sin
这时,查找(部分)结果如下:
……………………………………………………
/lib/libm-2.3.4.so:00008610 W sin
/lib/libm-2.3.4.so:00008610 t _sin
/lib/libm-2.3.4.so:000183e0 W sinl
/lib/libm-2.3.4.so:000183e0 t _sinl
……………………………………………………
在 /lib/libm-2.3.2.so:00008610 W sin 中,/lib是系统存放函数的默认位置,libm-2.3.2.so是包含sin函数的函数库名,其中,所有函数库的名都以“lib”开头,跟着的字母“m”是包含sin函数的函数库的真正的名子,“-2.3.2”是版本号,“.so”说明它的动态库。
在使用“ - l”参数时,通常的习惯是出去“lib”函数库头和后面的版本号,使用真名和参数“-l”连接,形成“- lm”。于是,我们需要在gcc找不到库时,可是使用“-l”直接给定库名。
查找math.h路径:
whereis math.h
math: /usr/include/math.h