如何修复 GCC undefined reference to `sqrt'

问题:

尝试使用 gcc 编译你的应用程序时,你看到类似这样的错误消息

how-to-fix-gcc-undefined-reference-to-sqrt.txt
/usr/bin/ld: /tmp/ccxPIowU.o: in function `run_mymath':
mathstuff.c:(.text+0x15d): undefined reference to `sqrt'

解决方案

你需要使用 -lm 标志链接数学库(-lxxx 表示:“链接 xxx 库”,即 -lm 表示"链接 m 库")

例如,而不是

gcc -o myprogram *.c

gcc_link_math.sh
gcc -o myprogram *.c

使用

gcc -o myprogram *.c -lm

gcc_link_math_with_lm.sh
gcc -o myprogram *.c -lm

Check out similar posts by category: C/C++, GCC Errors