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,29 @@
#!/bin/bash
set -e
echo "=== Compile with -fPIC for position-independent code ==="
g++ -std=c++17 -fPIC -c vec.cpp -o vec.o
g++ -std=c++17 -fPIC -c mat.cpp -o mat.o
echo ""
echo "=== Create shared library ==="
g++ -shared vec.o mat.o -o libmymath.so
echo "Created libmymath.so"
echo ""
echo "=== Symbols exported by the shared library ==="
nm -C -D libmymath.so | grep -E "vec_|mat_"
echo ""
echo "=== Compile and link main dynamically ==="
g++ -std=c++17 -c main.cpp -o main.o
g++ main.o -L. -lmymath -Wl,-rpath,'$ORIGIN' -o app
echo "Created app"
echo ""
echo "=== Runtime dependencies ==="
ldd app
echo ""
echo "=== Run ==="
./app

View File

@@ -0,0 +1,23 @@
#include <iostream>
void vec_add(double[3], double[3], double[3]);
void vec_print(double[3]);
void mat_identity(double[9]);
void mat_print(double[9]);
int main() {
double a[3] = {1, 2, 3};
double b[3] = {4, 5, 6};
double c[3];
vec_add(a, b, c);
std::cout << "vec_add: ";
vec_print(c);
double m[9];
mat_identity(m);
std::cout << "identity matrix:\n";
mat_print(m);
return 0;
}

View File

@@ -0,0 +1,12 @@
#include <iostream>
void mat_identity(double m[9]) {
for (int i = 0; i < 9; i++) m[i] = 0.0;
m[0] = m[4] = m[8] = 1.0;
}
void mat_print(double m[9]) {
for (int r = 0; r < 3; r++) {
std::cout << "| " << m[r*3] << " " << m[r*3+1] << " " << m[r*3+2] << " |\n";
}
}

View File

@@ -0,0 +1,9 @@
#include <iostream>
void vec_add(double a[3], double b[3], double out[3]) {
for (int i = 0; i < 3; i++) out[i] = a[i] + b[i];
}
void vec_print(double v[3]) {
std::cout << "(" << v[0] << ", " << v[1] << ", " << v[2] << ")\n";
}