从preventing功能从静态库被剥离,当连接到一个共享库? [英] Preventing functions from being stripped from a static library when linked into a shared library?

查看:139
本文介绍了从preventing功能从静态库被剥离,当连接到一个共享库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态库,富,所使用的共享库,酒吧。酒吧是我的Andr​​oid应用程序加载本地共享库。美孚包含JNI函数只调用的Java code和不被任何C ++ code在酒吧。正因为如此,这些JNI函数得到剥离出来的静态库(美孚)时,共享库(栏)是建立。我目前使用的情况发生稍微哈克方法prevent。

I have a static library, Foo, that is used by a shared library, Bar. Bar is the native shared library loaded by my Android app. Foo contains JNI functions that are only called by Java code and not by any C++ code in Bar. Because of this, those JNI functions get stripped out of the static library (Foo) when the shared library (Bar) is built. I'm currently using a slightly hacky method to prevent that from happening.

所以,在这种情况下,有没有办法告诉编译器不要剥去JNI(或)功能进行连接的时候?

So, in this case, is there a way to tell the compiler to not strip the JNI (or any) functions out when linking?

推荐答案

不予理会剥离,他们得到忽略。当共享库被链接,链接器仅拉​​在目标文件中与实际使用的功能。 (这就是静态库的定义工作。)

They're not getting stripped, they're getting ignored. When the shared library is linked, the linker is only pulling in the object files with functions that are actually used. (This is how static libs are defined to work.)

我相信,通过了--whole存档标志的连接器将导致它在所有目标文件从静态库拉。你可以提供它与-Wl,-whole归档海湾合作委员会链接线。你需要指定你的库后,以-Wl, - 无 - 全档案跟着它,或劳工处会继续的行为遇到任何其他静态库,这很可能不是你想要的行为。另见在Linux系统上的LD(1)手册页。

I believe passing the "--whole-archive" flag to the linker will cause it to pull in all object files from a static library. You can provide it on the gcc link line with "-Wl,-whole-archive". You need to follow it with "-Wl,-no-whole-archive" after specifying your library, or ld will continue the behavior for any other static libraries it encounters, which is likely not the behavior you want. See also the ld(1) man page on a Linux system.

另一种方式来完成同样的事情是要输出一个庞大的.o文件而不是某文件。

Another way to accomplish the same thing is to output a single massive .o file instead of a .a file.

编辑: 简单的命令行例子中,使用libz进行了桌面上:

Simple command-line example, using libz on the desktop:

% echo "int main() { return 0; }" > foo.c
% gcc -o foo /usr/lib/libz.a foo.c
% ls -s foo
12 foo*
% gcc -o foo -Wl,-whole-archive /usr/lib/libz.a -Wl,-no-whole-archive foo.c
% ls -s foo
104 foo*

(你必须使用/usr/lib/libz.a,而不是-lz在这里,因为后者找到共享库/usr/lib/libz.so。)

(You have to use "/usr/lib/libz.a" instead of "-lz" here because the latter finds the shared library /usr/lib/libz.so.)

我还没有使用NDK的不多,但它看起来像加了标志LOCAL_LDFLAGS可能达到目的。

I haven't used the NDK much, but it looks like adding the flags to LOCAL_LDFLAGS might do the trick.

这篇关于从preventing功能从静态库被剥离,当连接到一个共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆