开发者

header file and standard library

I am new to programming. learning C as of now. I understand that header file only contains the declarations and function prototypes, not the functions themselves. Am I correct?

I understand that Library is a single file that contains different object codes. Are these object codes necessarily written only in C language or 开发者_如何学Cother languages can also be used to generate such object codes?

On linking, does the entire library file get attached to the executable or just the object codes declared in the header file?


I understand that header file only contains the declarations and function prototypes, not the functions themselves.

Usually, yes (although you can theoretically put any code you want in a header file). Remember that a header file is usually just #include-d into a source file, and #include is basically equivalent to a copy-and-paste.

I understand that Library is a single file

Not necessarily. "Library" is a bit of a loose term, but generally speaking, it's used to describe a collection of functions (and potentially data) that together perform some useful set of tasks. These functions may be defined in one or several source files. Typically, a library is pre-compiled into a standalone object file. But again, not necessarily.

Are these object codes necessarily written only in C language

No. They can be written in any language (because it will always be compiled down to raw machine code). But if you want to use the library from C, then certain compatibility requirements must be fulfilled, to ensure that the C compiler knows how to call the library functions correctly.

On linking, does the entire library file get attached to the executable

Sometimes. This is what's known as static linking. The other main type is dynamic linking, where the library object code is linked at run-time.


I understand that header file only contains the declarations and function prototypes, not the functions themselves. Am I correct?

Yes, in most cases. Code can appear in headers which the compiler will try to "inline" (look that up).

Are these object codes necessarily written only in C language or other languages can also be used to generate such object codes?

They can be written in any complied language but the most common are C and C++.

does the entire library file get attached to the executable or just the object codes declared in the header file?

Linking just tells the executable how to call the underlying the library code, it does not "attached" the library unless you are doing "static" linking.


The header file usually contains just declarations and prototypes, but in modern C it can contain "static inline" functions along with their definitions. (And in C++, it is common for template classes to have their implementations in the header file, but do not worry about that for now...)

Objects in a library can be written in any language; as long as they obey "C linkage" (essentially, calling and naming conventions at the assembly level) they can call and be called from C functions. It is not uncommon for a few performance-critical library functions to be written in assembly language, for instance.

The answer to your last question depends on the linker. I believe GNU ld includes all of the functions from all .o files any of whose functions is referenced, but to be honest, I am not even sure. Most libraries you link against are "shared libraries" anyway, which means the answer to your question does not even matter. (Actually, it does not matter much in general, as the OS will only load pages from disk when they are actually accessed. So the size of your executable on disk and the amount of RAM it takes while running can be very, very different.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜