Код: Выделить всё
$ cat static.cpp
int use_static()
{
return {};
}
void unused_in_static() {}
Код: Выделить всё
$ cat shared.cpp
void use_static();
void use_shared()
{
use_static();
}
void unused_in_shared() {}
Код: Выделить всё
$ cat app.cpp
void use_shared();
void use_static();
int main()
{
use_shared();
use_static();
}
Код: Выделить всё
#!/bin/bash -xe
COMPILE_OPTIONS="-ffunction-sections -fPIC"
g++ $COMPILE_OPTIONS -c -o static.o static.cpp
ar qc libstatic.a static.o
g++ $COMPILE_OPTIONS -c -o shared.o shared.cpp
g++ -Wl,-gc-sections -shared -o libshared.so shared.o libstatic.a
g++ $COMPILE_OPTIONS -c -o app.o app.cpp
g++ -Wl,-gc-sections -Wl,-rpath=. -o app app.o libstatic.a libshared.so
Код: Выделить всё
readelf -s app | grep unused
13: 000000000000079a 7 FUNC GLOBAL DEFAULT 14 _Z16unused_in_staticv
57: 000000000000079a 7 FUNC GLOBAL DEFAULT 14 _Z16unused_in_staticv
То же самое произойдет, если я добавлю libstatic в компоновщик после libshared:
Код: Выделить всё
g++ -Wl,-gc-sections -Wl,-rpath=. -o app app.o libstatic.a libshared.so libstatic.a
Код: Выделить всё
g++ -Wl,-gc-sections -Wl,-rpath=. -o app app.o libshared.so libstatic.a
readelf -s app | grep unused
Подробнее здесь: https://stackoverflow.com/questions/687 ... brary-befo
Мобильная версия