【发布时间】:2013-08-07 23:40:02
【问题描述】:
我有一个代码需要编译到共享库并从中删除所有未使用的代码,但我找不到合适的解决方案。这是一个简单的例子:
// 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。有没有办法在不破解编译器的情况下删除这些符号? (是的,我正在考虑这一点!)
谢谢!
【问题讨论】:
-
请解释为什么你想这样做。
-
呃,把所有的函数都声明为静态的?
-
@MatsPetersson:这不适用于 C++ 成员函数。
-
@bmargulies:有很多似是而非的解释。它可能是一个受约束的环境,或者它可以链接到它使用一小部分的第三方静态库等。剥离未使用的符号也可能导致 dylib 依赖项消失,因此您实际上可以获得很多里程有时会这样。
-
你能解释一下你想要达到的目标吗?这闻起来像 XY 问题:meta.stackexchange.com/questions/66377/what-is-the-xy-problem
标签: c++ c shared-libraries