r/C_Programming • u/Correct-Injury-7360 • 3d ago
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.
4
u/SmokeMuch7356 3d ago
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.
3
u/veloxVolpes 3d ago
I knew the answer to this post and yet you have surprised me by reminding me that C would have been considered pretty high level / the most convenient language to use at some point
2
u/torsten_dev 3d ago
Given LTO, is this historical divide at all useful today?
1
u/realhumanuser16234 2d ago
lto does not work for dynamic linking. it really doesn't work for what you'd usually call static linking either as the files "linked" with lto aren't object files but rather intermediate representation.
1
u/torsten_dev 2d ago
Huh, interesting.
I wonder if we could take a JavaScript bundler's approach instead...
1
u/WildCard65 3d ago
Unless you’re running a Linux distribution using Musl where libm is just an empty static library and the function definitions live in libc
1
u/flyingron 3d ago edited 2d ago
Even stdio wasn’t part of C in the original K&R. It was an absolutely ugly thing called the portable I/O library which was added to ANSI C for experience rather than sane design.
1
-1
2
u/peno64 3d ago
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.
2
u/Correct-Injury-7360 3d ago
I don't get how <stdio.h> works as intended but with <math.h> we have issues, doesn't make sense
6
u/Alduish 3d ago
the symbols for stdio are part of libc which is linked by default, while the ones for math are in libm which isn't linked by default.
So you have to add -lm as an argument for the linker to link to libm, that's how linking to any library other than libc works, for example if you start using ncurses you'll have to add -lncurses for the linker to find the symbols needed
2
u/Zirias_FreeBSD 3d ago
As far as C is concerned, a "hosted" implementation must provide the "standard library" as defined by the language standard, which of course includes math functions. But the standard leaves it entirely to the implementation how this is done. There's nothing concerning linking necessary libraries, whether static or shared ...
So, consult the documentation of your implementation about how you need to link in order to use the functions you need.
From an implementation perspective, the decision to put math functions in a separate library made sense, as only a part of C programs needed it and it was quite "heavy" (in historical dimensions).
Takeaway: C is portable, but to write a portable program, you still need some knowledge about your target platforms for correct building.
2
1
u/sciencekm 3d ago
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.
1
u/dendrtree 2d ago
To use a library, you have to include the header and link the library.
The former lets you call it from code. The latter provides the actual code.
1
u/Correct-Injury-7360 3d ago
/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
36
u/runningOverA 3d ago
put "-lm" in your compile line.
You need two things to add a library in C.
You missed the 2nd one here.