How to fix GCC undefined reference to `sqrt'
Problem:
When trying to compile your application using gcc, you see an error message like
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'Solution
You need to link the math library using the -lm flag (-lxxx means: “link the xxx library”, i.e. -lm means “link the m” library)
For example, instead of
gcc -o myprogram *.c
gcc_link_math.sh
gcc -o myprogram *.cuse
gcc -o myprogram *.c -lm
gcc_link_math_with_lm.sh
gcc -o myprogram *.c -lmCheck out similar posts by category:
C/C++, GCC Errors
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow