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.

Kaiyakha
  • 1,463
  • 1
  • 6
  • 19
  • 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? – user4581301 Dec 09 '21 at 19:43
  • 2
    Because you may need to reuse those header files elsewhere. – smac89 Dec 09 '21 at 19:43
  • https://stackoverflow.com/questions/51488008/how-to-call-functions-from-one-cpp-file-in-another-cpp-file/51488203#51488203 – JMAA Dec 09 '21 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. – zerocukor287 Dec 09 '21 at 19:43
  • The compiler has to know that a function exists and how it is to be called, largely to ensure type correctness. One way to do this is to declare the function, along with its parameter types, in a header file that any number of source file can use. – jkb Dec 09 '21 at 19:45
  • Cannot we just place definitions in a header file instead of declarations? – Kaiyakha Dec 09 '21 at 19:46
  • You can place definitions in a header file. But then no one can reuse that header file to create new definitions. – smac89 Dec 09 '21 at 19:47
  • 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](https://en.cppreference.com/w/cpp/language/definition) – drescherjm Dec 09 '21 at 19:48
  • @drescherjm this is a good one – Kaiyakha Dec 09 '21 at 19:49
  • You can put definitions in header files for templates and for inline functions. – jkb Dec 09 '21 at 19:49
  • I believe this is a good question for language design. Some languages store definition to library already. So they don't need to include header file to use them. C and C++ instead, separated header for declaration with library for implementation. You can design another language that build header file into library file, and declare to that library in source file will be enough. – Herbert Yu Dec 09 '21 at 19:51
  • In simple, because C/C++ separates compile and linkage into two different stages. – Herbert Yu Dec 09 '21 at 19:54
  • I seem to start realising why today Java is more preferable, am I right? – Kaiyakha Dec 09 '21 at 19:56
  • Depends. Java uses a multi-pass compiler instead of the single pass compilers typically used with C++. Both have strengths and weaknesses. – user4581301 Dec 09 '21 at 22:35

2 Answers2

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.
0x5453
  • 12,753
  • 1
  • 32
  • 61
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.

Peter
  • 35,646
  • 4
  • 32
  • 74