tags:

views:

76

answers:

1

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.

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

+1  A: 

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.)

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.

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

EDIT: 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*

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

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

fadden
fadden - for the dense among us - can you post a quick example of how you would need to set up the Android.mk file (at least the two lines pertinent to linking)?
EboMike