Frage

When compiling an application that depends on a library we have to specify the include the library and also the header file.

Since the library binary contains the actual function names couldn't the function names be provided from just the binary?

War es hilfreich?

Lösung

Libraries are specified during linking, rather than compiling, and thus don't provide functions names during compilation. Also, the library object files may not have all the information necessary for the compilation stage (such as the number and types of arguments), depending on what features are available in the object format and which features the compiler made use of when producing the library file.

Andere Tipps

It is entirely possible to use libraries without using the headers. Here is an example using just the C library:

int puts(const char *);

int main()
{
    puts("Hello, World!");
}

The advantage of header files is to provide the function signatures and structure bodies for you. In the case of the Standard C Library itself, the standard explicitly forbids declaring any function signature yourself if it involves any library-defined type (a real-world example is anything that uses 'off_t', which usually exists in a 32-bit version and a 64-bit version and uses some compiler-specific magic to rename the symbol)

Lizenziert unter: CC-BY-SA mit Zuschreibung
scroll top