0

I thought the .so file is like a .o file. And since the .o file is built directly from a single .c file, I'm confused why .so file involves linking of multiple .c files.

4

2 回答 2

2

A .so file is a library- its the end result of compiling and linking a bunch of code that goes together. It can be made from 1 .c file or multiple .c files. It all depends on how you want to organize your code. A .o file is an object file. Its the result of compiling, but not linking, a single file. Its used by the linker to combine with other .o files into an executable (.exe or equivalent) or library (.so, .dll, etc).

于 2013-04-17T04:55:07.617 回答
2

I thought the .so file is like a .o file.

No, it isn't. .o files are object code (that's what compiling a .c source file results in), whereas .so files are dynamically linked libraries. They are similar to executable files in that they can potentially be built from multiple object files, which are linked together.

The reason for this is that since dynamic libraries serve a different purpose from that of object files' (namely, providing access to functions and other symbols in a dynamically resolvable way), they need to be contain proper executable code with no dependencies. Just like a normal, stand-alone executable. Object files contain code that has unresolved dependencies, it's not directly executable, etc. That's why linkage is performed on several object files, to resolve dependencies and to make a final file (referred to "the executable" even if it's not a stand-alone executable but a dynamic library, a kernel module, etc.)

于 2013-04-17T04:55:38.757 回答