我有一段代码需要编译为共享库,并删除其中所有未使用的代码,但是我找不到一个合适的解决方案。以下是一个简单的示例:
// 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"。有没有一种方法可以在不篡改编译器的情况下删除这些符号?(是的,我现在正在考虑这个问题!)
谢谢!