0

This answer says we need header files for declarations of other functions placed somewhere else, for example in .lib or .dll or .cpp files.

My question is, why do we really need to use declarations in header files at all? Why can't we just use definitions straight away? For example, place a definition instead of a declaration and address to it directly. To a beginner, all this declaration stuff seems to be sort of a redundant work.

14
  • If file A doesn't know what a function defined in file B looks like, File A has a visible declaration, how can the compiler be sure File A is using the function correctly? Commented Dec 9, 2021 at 19:43
  • 2
    Because you may need to reuse those header files elsewhere. Commented Dec 9, 2021 at 19:43
  • stackoverflow.com/questions/51488008/… Commented Dec 9, 2021 at 19:43
  • 1
    It's possible to create a program only in a cpp file, but it's better to separate it logically into several files, modules. Commented Dec 9, 2021 at 19:43
  • 3
    Cannot we just place definitions in a header file instead of declarations? If you do that then you run into ODR violations if more than 1 source file includes the header. Related: https://en.cppreference.com/w/cpp/language/definition Commented Dec 9, 2021 at 19:48

2 Answers 2

1

A few reasons come to mind:

  1. C++ being an old language, it is sensitive to the ordering of code. Things must be declared before they can be used, and header files are a convenient and consistent way to do this. For example, this example would work in most modern languages, but not in C++:
int main() {
    foo();  // error: 'foo' has not been declared
    return 0;
}

void foo() { /* ... */ }
  1. It separates the interface from the implementation. This means you can push out updates to a library's implementation, and as long as none of the interfaces have changed, clients do not need to re-compile their code. This also means that you can have multiple implementations corresponding to a single interface - for example, maybe you want to write a filesystem library, but the implementation of that library will have to look significantly different on Linux than it does on Windows. Both of these implementations can share the same header, making the implementation details "opaque" to users.
Sign up to request clarification or add additional context in comments.

Comments

0

Bear in mind that all definitions are declarations in C++ (but not all declarations are definitions).

Non-inline functions are usually declared but not defined in a header to avoid breaking the one-definition rule (and linking problems) when that header is included in multiple sources.

Declarations (not definitions) of some class/struct types are needed in headers to break circular dependencies (which causes diagnosable errors and prevents compilation).

These types of concerns rarely arise in "toy" problems used in programming courses and ivory towers, but are critically important in real-world development. For example, in toy problems, a header file might only be included in a single source file - so the concerns of breaking the one-definition rule don't arise. But real-world projects typically include headers in multiple source files - so are affected by problems that arise when definitions are inappropriately placed into header files.

In short, the approaches used in small learning exercises (which cause students to wonder why there is any need to put declarations into headers rather than definitions) don't scale to real-world development.

Some types of definitions (templates, inline functions, and class/struct definitions) are routinely placed into headers. That doesn't mean that all definitions should be placed in headers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.