r/C_Programming • u/Correct-Injury-7360 • Aug 30 '26
Question The linker doesn't link the pow function's precompiled library, even though header is included AND used. Why?
Pls help me idk what's going on...
https://imgur.com/a/5KLsigY
It complains that it can't find the pow function.
9
Upvotes
38
u/runningOverA Aug 30 '26
put "-lm" in your compile line.
You need two things to add a library in C.
- Include the header in source code.
- Include the library in compiler's command line.
You missed the 2nd one here.
2
u/Correct-Injury-7360 Aug 30 '26
I don't get how <stdio.h> works as intended but with <math.h> we have issues, doesn't make sense
5
u/SmokeMuch7356 Aug 30 '26
Hysterical raisins.
K&R-era C wasn't used for number-crunching and relatively few programs needed anything from
math.h, so to save space and time the math library would not be linked by default; you'd have to explicitly specify its inclusion in the link command.At least in *nix land, that philosophy has carried through to the present day.
1
u/Correct-Injury-7360 Aug 30 '26
/usr/bin/ld.bfd: /tmp/ccAqymX6.o: in function `main':
18.c:(.text+0x44): undefined reference to `pow'
collect2: error: ld returned 1 exit status
2
u/peno64 Aug 30 '26
Not sure if you understand the difference between a header file and a link library. It's not because you include a header file that the linker knows which library to include in the binary. An include file makes the definition known to your source file or better to the compiler. The linker links your program with libraries and you have to specify these also. So in fact you need to specify them twice.
1
u/sciencekm Aug 30 '26
External functions reside in some library file somewhere. It just so happened that standard c library are linked by default.
If you add the -nostdlib option to gcc/clang, the standard c library will not be linked by default, and you will see the same issues.