I'm porting some things to Android, and I've got several static libraries that should be linked into the .so file (using the Android NDK). I tried using -Wl,-whole-archive
to the linker (and terminated by the appropriate -Wl,-no-whole-archive
) but get errors such as:
c:/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe : error : C:/Users/Brent/Documents/git/gamelib/Core/Android/Android/Debug/Core-Android.a:
member at 25678 is not an ELF object [C:\Users\Brent\Documents\git\blackjack\blackjack-android.vcxproj]
And lots of undefined references to functions that aren't implemented yet, but are called by other functions that are implemented but not used (which would normally be stripped). The whole things not ported yet, but I can't test (or even build) existing code with -whole-archive because of this. Something more fine-grained is needed.
So, I decided to try using __attribute__((used))
when declaring the functions in my static library:
extern "C" {
void Java_com_brainium_blackjack_BlackjackJNILib_step(JNIEnv* _jni_env, jobject jthis) __attribute__ ((visibility ("default"))) __attribute__ ((used));
};
void Java_com_brainium_blackjack_BlackjackJNILib_step(JNIEnv* _jni_env, jobject jthis) {
}
But it still gets stripped. This code put into one of the .cpps compiled directly into the .so works fine, but if I put it into a static lib and link that lib into the so it doesn't work. I've used nm to make sure that it is infact being excluded, and that other symbols of the static library (that are being referenced) are present (so the link is working in general). Is __attribute__((used))
not working as it should, or am I just using it wrong? Thanks.
Side note: I'm not using ndk-build (or make files) to build this project. I'm using (and extending) vs-android.
Edit: After a bit more searching, it seems that __attribute__((used))
doesn't do anything because the .o files aren't even examined if the linker doesn't see a reference. This also suggests to me that if I simply forced it to read all the .o files by some other means (combining the .o files into one .o file, or using the -u option), that it would still have the same effect as --whole-archive. It seems that getting --whole-archive
working may be the only solution.