From 69861846a29f049d0c36eebf2b15597516023e2e Mon Sep 17 00:00:00 2001 From: xavi Date: Fri, 19 Dec 2025 13:06:31 -0800 Subject: [PATCH] Added the linker error playground Here is a project where I was just trying to figure out the nuances of different Translation Units and the scope of variables and function. TLDR: run gcc d.c b.c c.c and see that the linker errors There is storage duration and linkage that can be specified for vars and funtions. There are four different types of Storage Duration Automatic - which is get initialized when the block starts and dies when the block ends Static - which is the entire duration of the program Thread - which i didn't get into really Allocated - which is the typical heap allocation thingys There are three types of linkage. Basically where can you reach a varibale from No Linkage - can only be refered to in the block Internal Linkage - can only be refered to in the same TLU External Linkage - can be reachable by any TLU in the program This shows how defining a variable in 1 h file (a.h), which will have external linkage because it is a file scope variables not declared static will default to, and then #include-ing the h file in multiple other files will lead to redefiniation exceptions thrown by the linker. If confused see more here: https://en.cppreference.com/w/c/language/storage_class_specifiers.html --- c_projects/linker_error_pg/a.h | 6 ++++++ c_projects/linker_error_pg/b.c | 7 +++++++ c_projects/linker_error_pg/b.h | 4 ++++ c_projects/linker_error_pg/c.c | 8 ++++++++ c_projects/linker_error_pg/c.h | 3 +++ c_projects/linker_error_pg/d.c | 8 ++++++++ 6 files changed, 36 insertions(+) create mode 100644 c_projects/linker_error_pg/a.h create mode 100644 c_projects/linker_error_pg/b.c create mode 100644 c_projects/linker_error_pg/b.h create mode 100644 c_projects/linker_error_pg/c.c create mode 100644 c_projects/linker_error_pg/c.h create mode 100644 c_projects/linker_error_pg/d.c diff --git a/c_projects/linker_error_pg/a.h b/c_projects/linker_error_pg/a.h new file mode 100644 index 0000000..df69349 --- /dev/null +++ b/c_projects/linker_error_pg/a.h @@ -0,0 +1,6 @@ +#ifndef A_H +#define A_H + +int x = 42; + +#endif diff --git a/c_projects/linker_error_pg/b.c b/c_projects/linker_error_pg/b.c new file mode 100644 index 0000000..1545479 --- /dev/null +++ b/c_projects/linker_error_pg/b.c @@ -0,0 +1,7 @@ +#include +#include "b.h" + + +int b_print_x(){ + printf("b.c: x = %d\n", x); +} diff --git a/c_projects/linker_error_pg/b.h b/c_projects/linker_error_pg/b.h new file mode 100644 index 0000000..edc8cd5 --- /dev/null +++ b/c_projects/linker_error_pg/b.h @@ -0,0 +1,4 @@ +#include +#include "a.h" + +int b_print_x(); diff --git a/c_projects/linker_error_pg/c.c b/c_projects/linker_error_pg/c.c new file mode 100644 index 0000000..c0a0111 --- /dev/null +++ b/c_projects/linker_error_pg/c.c @@ -0,0 +1,8 @@ +#include +#include "c.h" + + + +int c_print_x(){ + printf("c.c: x = %d\n", x); +} diff --git a/c_projects/linker_error_pg/c.h b/c_projects/linker_error_pg/c.h new file mode 100644 index 0000000..4ae5846 --- /dev/null +++ b/c_projects/linker_error_pg/c.h @@ -0,0 +1,3 @@ +#include "a.h" + +int c_print_x(); diff --git a/c_projects/linker_error_pg/d.c b/c_projects/linker_error_pg/d.c new file mode 100644 index 0000000..e2abb6d --- /dev/null +++ b/c_projects/linker_error_pg/d.c @@ -0,0 +1,8 @@ +#include "b.h" +#include "c.h" + +int main(){ + b_print_x(); + c_print_x(); + return 0; +}