C++ linker presentation
This commit is contained in:
24
cpplinker/01_compilation_pipeline/build.sh
Normal file
24
cpplinker/01_compilation_pipeline/build.sh
Normal 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
|
||||
3
cpplinker/01_compilation_pipeline/foo.cpp
Normal file
3
cpplinker/01_compilation_pipeline/foo.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
9
cpplinker/01_compilation_pipeline/main.cpp
Normal file
9
cpplinker/01_compilation_pipeline/main.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user