C++ linker presentation

This commit is contained in:
2026-02-24 10:21:15 +01:00
parent 0fda0d75fb
commit 6f3d98f388
32 changed files with 788 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
#!/bin/bash
set -e
echo "=== Step 1: Compile each .cpp to object files ==="
g++ -std=c++17 -c foo.cpp -o foo.o
g++ -std=c++17 -c main.cpp -o main.o
echo "Created foo.o and main.o"
echo ""
echo "=== Step 2: Inspect object files with nm ==="
echo "--- foo.o symbols (defines add) ---"
nm -C foo.o
echo ""
echo "--- main.o symbols (references add) ---"
nm -C main.o
echo ""
echo "=== Step 3: Link object files into executable ==="
g++ foo.o main.o -o app
echo "Created app"
echo ""
echo "=== Step 4: Run ==="
./app

View File

@@ -0,0 +1,3 @@
int add(int a, int b) {
return a + b;
}

View File

@@ -0,0 +1,9 @@
#include <iostream>
extern int add(int, int);
int main() {
int r = add(3, 4);
std::cout << "add(3, 4) = " << r << "\n";
return 0;
}