1

For a predefined function where are the declaration and definition stored? And is the declaration stored in libraries? If so, then why it is named library function?

Ruslan
  • 18,162
  • 8
  • 67
  • 136
  • 1
    By "pre-defined", do you mean one of the C standard APIs? Or something you wrote? (The answer is roughly the same, but you need to clarify your point of confusion here) – ShadowRanger Aug 25 '19 at 12:15
  • Boilerplate is to have the declaration in a .h file, definition in a source file that's stored on the library programmer's machine and implementation in the library you link. – Hans Passant Aug 25 '19 at 14:19
  • @ShadowRanger i mean the c standard functions like printf() – user11974307 Aug 25 '19 at 18:29

3 Answers3

1

This is an imprecise question. The best answers we can give are:

  1. The declaration of standard library functions can best be thought of as being stored in their header files.

  2. Depending on how you want to think about it, the definition of standard library files is either in the source files for those libraries (which may be invisible to you, a trade secret of your compiler vendor), or in the library files themselves (.a, .so, .lib, or .dll).

  3. These days, knowledge of standard library functions is typically built in to the compiler, also. For example, if I write the old classic int main() { printf("Hello, world!\n"); }, but without any #include directives, my compiler says, "warning: implicitly declaring library function 'printf'" and "include the header <stdio.h>".

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • These days gcc knows so much about `printf()` that it replaces it with `puts()` where possible (like in your example: `callq 400420 `). – U. Windl Aug 25 '19 at 22:42
0

The header file contains the declaration of built-in functions and the library contains the definition of the functions.

The name library is because, in my opinion, as the actual library which contains books, these libraries contain the classes, functions, variables etc.

Mhmd Az
  • 82
  • 9
0

There are two sides of this story:

  • The code that calls a library/external function: The compiler generates a reference in your compiled module that encodes which function prototype you expect to exist elsewhere.
  • The pre-compiled library files against which your code must be linked: A library file contains both the coded prototypes of its functions as well as the actual compiled binary (i.e. the definition/implementation) for these function.

When your code uses an external function, the compiler generates a reference to this function that it assumes will be resolved later during the linking phase.

During the linking process lists of function references are build up. The linker expects to find the 'definition'/implementation of each of the used references.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142