Linking to library

I have doubt when there is need to link library.

Suppose,
I have a exe prog.exe which referring to one lib i.e. one.lib and exe running fine.

Now,
Case 1:
I have made some changes into a lib function in one.lib.
So now Signature of function and body of function has been changed.

In this case
a. Build one.lib.
b. Needs to update prog code(as function signature also changed)
c. ReBuild prog to get new prog.exe.
d. Link prog.o to the latest build of new.lib.
e. prog.exe should run.


Case 2:
I have made some changes into a lib function in one.lib.
Signature of function is same but made some changes in body of function.

In this case
a. Build one.lib.
b. Dont need to update prog code(as function signature not changed)
c. Link prog.o to the latest build of new.lib.
ld -o prog.exe prog.o new.lib
d. prog.exe should run.


For more details-
Static lib:
https://helloacm.com/how-to-link-static-library-in-cc-using-gcc-compiler/

Dynamic shared lib:
https://helloacm.com/how-to-use-the-dynamic-link-library-in-c-linux-gcc-compiler/


Last edited on
If the library is static then in both cases the program needs to be rebuilt, because the library's code is directly included in the executable. If the library is dynamic then you only need to rebuild the executable on case 1, since the .lib only includes references to function implementations, not the implementations themselves.
Last edited on
Now, this being UNIX/Linux Forum, ...

A convention is that executable binaries do not have extension in the filename.

Library-names have prefix "lib".

Dynamically linked libraries ("shared objects") have extension ".so" and static libraries have extension ".a" (as in "archive"). There is no ".lib".

Therefore, you could build both libone.so and libone.a for one, but you don't have to.

Building your prog with .so, the linker inserts calls to libone.so into prog.
Running your prog, dynamic linker loads libone.so.

Building your prog with .a, the linker copies code from libone.a into prog like from any other object file.
Running your prog, code is in it.


You have probably used a Linux distro that has package management and have installed updated packages. Occasionally, there are updates for libraries, for example the glibc, the core C library. An .so. That does not mean that every package that has binary executable would get an update too, even though those bianries link glibc dynamically. No, just the library-file changes; signature has not changed.

(One should reboot though, to ensure that the new version of glibc is loaded to memory.)
Last edited on
Now its clear to me.
Thanks to both of you helios and keskiverto.
Topic archived. No new replies allowed.