How does standard library inserted into executable file.

Hi,

For this is my first post in this forums, let me first introduce myself; my name is Yaşar. I am doing programming as an hobby. I know Python, PHP and a little bit shell scripting. I am starting to learn C and C++ but I am confused with standard libraries, linking and binary formats.

I have inspected C standard libraries ("the files in /usr/include") and I didn't see any function definition. There were only macros, type definitions and declarations. So I am a little confused about where standard library functions come from. My first guess would be ".so" files in /lib folder (in the linking step I guess). If so, for example, when I use some functions from standard C or C++ library, does it get copied into my executable file, or does it get loaded from so file everytime I start my program. And for example, if two programs use same function or object from standard library ".so" file, would only one function loaded into memory and shared between two files?

Sorry about not being very spesific about the question. Since I am very confused myself, I had no other option but to reflect it into my question.
My first guess would be ".so" files in /lib folder

Yes. In particular libc.so (C functions), libm.so (maths), libgcc_s.so (gcc-related stuff) and libstdc++.so (C++ runtime).

or does it get loaded from so file everytime I start my program.

Yes, that's the case when you link dynamically (which is the default).

And for example, if two programs use same function or object from standard library ".so" file, would only one function loaded into memory and shared between two files?

Yes, the library is shared in memory (not on a function basis, though). Each process that needs it has it mapped into its address space, but it only exists once in physical memory.
So, does this mean my program's behaviour might change if shared library in operating system changes? Is it recommended to link dynamically? After reading first couple of paragraphs of this http://www.iecc.com/linker/linker10.html, I have started to feel like dynamic linking is a dangerous thing. Does experienced C programmers prefer dynamic linking to static linking?
So, does this mean my program's behaviour might change if shared library in operating system changes?

In theory it could, but that should be avoided. Minor updates to libraries should contain just security fixes and other bugfixes.
Major new versions that might change behavior should not overwrite the old shared library.

Is it recommended to link dynamically?

Normally, yes. However, it can be problematic if you're trying to run your program on a machine where the correct versions of the libraries you're using are not installed and not available via packet manager either.
Topic archived. No new replies allowed.