Compare commits

..

1 Commits

Author SHA1 Message Date
69861846a2 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
2025-12-19 13:06:31 -08:00
7 changed files with 36 additions and 67 deletions

View File

@ -0,0 +1,6 @@
#ifndef A_H
#define A_H
int x = 42;
#endif

View File

@ -0,0 +1,7 @@
#include <stdio.h>
#include "b.h"
int b_print_x(){
printf("b.c: x = %d\n", x);
}

View File

@ -0,0 +1,4 @@
#include <stdio.h>
#include "a.h"
int b_print_x();

View File

@ -0,0 +1,8 @@
#include <stdio.h>
#include "c.h"
int c_print_x(){
printf("c.c: x = %d\n", x);
}

View File

@ -0,0 +1,3 @@
#include "a.h"
int c_print_x();

View File

@ -0,0 +1,8 @@
#include "b.h"
#include "c.h"
int main(){
b_print_x();
c_print_x();
return 0;
}

View File

@ -1,67 +0,0 @@
# MakeFile for placeholder
# Created mm/dd/yyyy
# Authored by: xavi
# ----------------------
# | THIS IS A TEMPLATE |
# ----------------------
# TODO:
# %s/placeholder/project_name/g
# Change date file was created to today
# run make dirs to create project hierarchy
# Place headers in include and source files in src
# Delete this text
.POSIX:
# DIRECTORIES
SRC_DIR := src
INCLUDE_DIR := include
OBJ_DIR := obj
TESTS_DIR := tests
# C compiler settings
CC := gcc
CFLAGS := -ggdb -I$(INCLUDE_DIR) -Wall -Wextra -MMD -MP
DFLAGS :=
# Files
SRC := $(wildcard $(SRC_DIR)/*.c)
OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
TESTS := $(wildcard $(TESTS_DIR)/*.c)
# Get Dependencies from gcc using -MMD and -MP
DEPENDENCIES := $(OBJ:%.o=%.d)
# Final Binary Name
PROJECT_NAME := placeholder
all: $(PROJECT_NAME)
$(TESTS:$(TESTS_DIR)/%.c=%): $(OBJ)
$(CC) $(CFLAGS) $(DFLAGS) -o $(@) $(^)
$(PROJECT_NAME): $(OBJ)
$(CC) $(CFLAGS) $(DFLAGS) -o $(@) $(^)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) $(DFLAGS) -c -o $(@) $(<)
clean:
rm -f $(PROJECT_NAME) $(OBJ_DIR)/* $(TESTS:$(TESTS_DIR)/%.c=%)
-include $(DEPENDENCIES)
.PHONY: clean all
# Create Project Hierarchy
dirs: $(SRC_DIR) $(OBJ_DIR) $(TESTS_DIR) $(INCLUDE_DIR)
$(SRC_DIR) $(OBJ_DIR) $(TESTS_DIR) $(INCLUDE_DIR):
mkdir -p $(@)