1

I've been making websites and odd projects with Python and Javascript for about 4 years, and now I'm making the leap down to C/C++ for a new endeavor. Forgive me if my terminology is off, still finding the right language for everything.

my understanding is that the function and type declarations in header files help create a symbol table for compilation, which allows you to reference those imported functions in a main() routine (or something similar) before you've actually linked to the definitions. When you 'declare' something, you're putting it in the symbol table, so the compiler can verify references from files that are using those imported functions.

So my questions is, why would you construct the symbol table for imported funcs/types from a separate header file, rather than derive it directly from the source code you're going to import? why not just scan the .c file you're going to import, gather the function and typedef names and use those to populate the symbol table? would save code (no declarations), and I think would make importing code a bit more intuitive.

That said, my guess is i'm missing something - I'm sure there's a reason for the way it's organized the way it is. Flexibility? independence of the interface? is it less work to scan a header file? I don't know.

any insight you might have would be greatly appreciated!

das_schnuben
  • 101
  • 1
  • 3
  • 1
    Using a separate header file is not required, it is common convention. – Thomas Matthews May 01 '20 at 18:26
  • 1
    The fundamental premise is to have a declaration before the definition. This allows you to define your functions in any order after a declaration. It also tells the compiler that if the function is not defined in the present source, that it is a {required} external symbol. The declaration provides the compiler with a pattern for syntax checking also. – Thomas Matthews May 01 '20 at 18:28
  • 2
    The separation of declarations and source code into separate files or modules allows for separate compilation. This evolves into building only those modules that have changed. This speeds up the build process and also allows for sharing modules with other projects without recompiling. Compilation (translation) is the slowest part of the build process. Linking and emitting processor instructions is quick. – Thomas Matthews May 01 '20 at 18:31
  • 1
    The modern convention of a header file is to define the interface of a translation unit. The header file exposes the public part of the interface (contract). So in order to use a translation unit, one only has to include the header file (and the translation code or object module should be added to the build as well). Other additional purposes of a header file is to provide "inline" code definitions (preferably not as macros). – Thomas Matthews May 01 '20 at 18:34
  • 1
    For further details, I recommend taking a course in Compiler Theory or purchase a good book on compiler theory. You may need to fulfill some prerequisites before the Compiler Theory makes sense. Languages are a lot easier to compile when the symbols are defined before they are used. – Thomas Matthews May 01 '20 at 18:36
  • 1
    If you were to make C or C++ today, you'd have the compiler generate the "external surface API" data for you. But C was made a long time ago, and C++ inherited the same behavior. So instead of the compiler generating the data for the programmer, the programmer has to declare it manually. C++20 will have **modules**, which promises to be a big step forward for modernizing that aspect of C++ — assuming **modules** become widely adopted. – Eljay May 01 '20 at 18:38
  • 1
    Finally, function declarations (prototypes) are not required, as long as the functions are defined before they are called. Sometimes you may require function declarations to resolve circular issues. – Thomas Matthews May 01 '20 at 18:38

0 Answers0