C++ linker presentation
This commit is contained in:
27
cpplinker/03_name_mangling/build.sh
Normal file
27
cpplinker/03_name_mangling/build.sh
Normal file
@@ -0,0 +1,27 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
echo "=== Compile to object file ==="
|
||||
g++ -std=c++17 -c mangling.cpp -o mangling.o
|
||||
|
||||
echo ""
|
||||
echo "=== Raw mangled symbols ==="
|
||||
nm -g mangling.o | grep -E "process|sqrt|dot|legacy"
|
||||
|
||||
echo ""
|
||||
echo "=== Demangled symbols ==="
|
||||
nm -C -g mangling.o | grep -E "process|sqrt|dot|legacy"
|
||||
|
||||
echo ""
|
||||
echo "=== Demangle individual symbols ==="
|
||||
echo "_Z7processi" | c++filt
|
||||
echo "_Z7processd" | c++filt
|
||||
echo "_Z7processid" | c++filt
|
||||
echo "_ZN4Math4sqrtEd" | c++filt
|
||||
echo "_ZN6Vector3dotERKS_" | c++filt
|
||||
echo "legacy_init" | c++filt
|
||||
|
||||
echo ""
|
||||
echo "=== Build and run ==="
|
||||
g++ -std=c++17 mangling.o -o app
|
||||
./app
|
||||
33
cpplinker/03_name_mangling/mangling.cpp
Normal file
33
cpplinker/03_name_mangling/mangling.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <iostream>
|
||||
|
||||
int process(int x) { return x * 2; }
|
||||
int process(double x) { return (int)(x * 2); }
|
||||
int process(int x, double y) { return x + (int)y; }
|
||||
|
||||
namespace Math {
|
||||
double sqrt(double x) { return x * 0.5; }
|
||||
}
|
||||
|
||||
class Vector {
|
||||
public:
|
||||
double x, y;
|
||||
double dot(const Vector& v) { return x * v.x + y * v.y; }
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
int legacy_init(void) { return 0; }
|
||||
void legacy_free(void* p) { (void)p; }
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "process(3) = " << process(3) << "\n";
|
||||
std::cout << "process(3.0) = " << process(3.0) << "\n";
|
||||
std::cout << "process(3, 4.0) = " << process(3, 4.0) << "\n";
|
||||
std::cout << "Math::sqrt(9) = " << Math::sqrt(9.0) << "\n";
|
||||
|
||||
Vector a{1, 2}, b{3, 4};
|
||||
std::cout << "a.dot(b) = " << a.dot(b) << "\n";
|
||||
|
||||
std::cout << "legacy_init() = " << legacy_init() << "\n";
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user