Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

header file and standard library

Tags:

c

header

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 other 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?

like image 484
KawaiKx Avatar asked Dec 09 '22 08:12

KawaiKx


2 Answers

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.

like image 184
Oliver Charlesworth Avatar answered Dec 24 '22 02:12

Oliver Charlesworth


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.

like image 31
Andrew White Avatar answered Dec 24 '22 02:12

Andrew White