如何从共享库中删除*ALL*个未使用的符号
How to remove *ALL* unused symbols from a shared library?
我有一段代码需要编译到共享库中,并从中删除所有未使用的代码,但我找不到合适的解决方案。这里有一个简单的例子:
// test.cpp, compiled with GCC -fPIC -shared -fvisibility=hidden
#include <stdio.h>
class Foo {
void bar();
};
void Foo::bar() { printf("hello"); } // unused and should be removed
// I'm using printf("hello") so I can detect the symbols with `strings`
__attribute__((visibility("default"))) void test() {} // this function is "used"
-fvisibility=hidden
使得默认情况下所有函数都是隐藏的,我手动用__attribute__((visibility("default")))
标记公共函数。然而,隐藏函数不会被删除,除非标记为static
(显然,我不能对C++方法这样做)。
无论我做什么,GCC都会一直保留void Foo::bar()
和hello
。有没有一种方法可以在不侵入编译器的情况下删除这些符号?(是的,我现在正在考虑!)
谢谢!
使用标志-ffunction-sections
编译。然后与-Wl,--gc-sections
链接。我认为LTO也可以实现这一点,我不确定细节。
请注意,dylib中的所有公共符号都被视为活动符号。只有隐藏的符号才会以这种方式被剥离。
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 将无符号char*转换为std::istream*C++
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- vscode g++链路故障:体系结构x86_64的未定义符号
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 使用gcc从静态链接的文件中查找可选符号
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- VC++本机单元测试,找不到调试符号
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 检查TCHAR数组输入是否为带符号整数C++
- 用符号版本替换对函数的所有调用
- 未解析的外部符号_MsiLocateComponentW@12.
- 如何打印boost多精度128位无符号整数
- 为什么Eigen不能解析内置符号?(all、last、seq等)
- 如何从共享库中删除*ALL*个未使用的符号