Theoretical questions about error handling in C99

Viewed 57

I have two questions regarding error types in C99.

  1. is "Two function definitions with identical prototypes detected" considered a compilation error or is it a linking error, I would be happy for an explanation regarding the answer.
  2. Can we regard "Size of static variable exceeded maximally allowed bytes" as an error that can occur in all phases (compilation, linking, runtime)?,I managed to get why we can consider it a compilation and runtime error but I still can't manage to comprehend how it can be treated as a linking error. Thanks in advance.
1 Answers
  1. It is a linking error. Check the following example:

foo.h

int bar(void);

foo.c

int bar(void)
{
    return 0;
}

main.c

#include "foo.h"

int bar(void)
{
    return 1;
}

int main(void)
{
    return bar();
}

You can compile it with '-c' option in gcc, wich means "Compile and assemble, but do not link.". It runs just fine:

$ gcc -c main.c foo.c

However, if you try to link it:

$ gcc main.c foo.c
/usr/bin/ld: /tmp/ccFILZyR.o: in function `bar':
foo.c:(.text+0x0): multiple definition of `bar'; /tmp/ccJ1YUOR.o:main.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status

When compiling these source files, you get their respective object files with the function definitions inside. The error will only occur when trying to place those definitions in main.c, i.e. linking foo.o against main.o.

  1. I honestly can't imagine an overflow occurring in the linking phase. Could you show an example in which such thing happens?
Related