Search code examples
ccompiler-constructionlinkerc-preprocessor

What is responsible for ensuring all symbols are known/defined?


Is it the C preprocessor, compiler, or linkage editor?


Solution

  • The answer you are looking for is... the compiler it depends. Sometimes it's the compiler, sometimes it's the linker, and sometimes it doesn't happen until the program is loaded.


    The preprocessor:

    handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if).
    ...
    The language of preprocessor directives is agnostic to the grammar of C, so the C preprocessor can also be used independently to process other kinds of text files.

    The linker:

    takes one or more objects generated by a compiler and combines them into a single executable program.
    ...

    Computer programs typically comprise several parts or modules; all these parts/modules need not be contained within a single object file, and in such case refer to each other by means of symbols. Typically, an object file can contain three kinds of symbols:

    • defined symbols, which allow it to be called by other modules,
    • undefined symbols, which call the other modules where these symbols are defined, and
    • local symbols, used internally within the object file to facilitate relocation.

    When a program comprises multiple object files, the linker combines these files into a unified executable program, resolving the symbols as it goes along.

    In environments which allow dynamic linking, it is possible that

    executable code still contains undefined symbols, plus a list of objects or libraries that will provide definitions for these.