0

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?

  • What language are you using? – Winston Ewert Sep 28 '14 at 0:28
  • @WinstonEwert C++ – TheMeaningfulEngineer Sep 28 '14 at 7:02
  • you need the header for the compile stage; and the library during the link stage. – gbjbaanb Sep 28 '14 at 17:27
2

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.

1

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.