我一直在把头发拉出来。我正在 Ubuntu 12.04 64 位上开发。我创建了一个嵌入了 V8 的原生 Android 应用程序,但重新启动计算机后,它不再与 V8 链接!我尝试重新安装 ndk 并重新编译 V8,但无济于事。看起来链接器确实包含静态库,但没有在其中找到符号。我用nm检查过,符号似乎在那里。
当我运行 ndk_build 时,我得到以下输出:
$ ndk-build V=1
rm -f ./libs/armeabi/lib*.so ./libs/armeabi-v7a/lib*.so ./libs/armeabi-v7a-hard/lib*.so ./libs/mips/lib*.so ./libs/x86/lib*.so
rm -f ./libs/armeabi/gdbserver ./libs/armeabi-v7a/gdbserver ./libs/armeabi-v7a-hard/gdbserver ./libs/mips/gdbserver ./libs/x86/gdbserver
rm -f ./libs/armeabi/gdb.setup ./libs/armeabi-v7a/gdb.setup ./libs/armeabi-v7a-hard/gdb.setup ./libs/mips/gdb.setup ./libs/x86/gdb.setup
[armeabi] Compile++ thumb: native-activity <= main.cpp
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ -MMD -MP -MF ./obj/local/armeabi/objs/native-activity/main.o.d -fpic -ffunction-sections -funwind-tables -fstack-protector -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-exceptions -fno-rtti -mthumb -Os -g -DNDEBUG -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -Ijni/include -I/home/schooten/bin/packages/android-ndk-r9d/sources/android/native_app_glue -I/home/schooten/bin/packages/android-ndk-r9d/sources/cxx-stl/system/include -Ijni -DANDROID -Wa,--noexecstack -Wformat -Werror=format-security -I/home/schooten/bin/packages/android-ndk-r9d/platforms/android-9/arch-arm/usr/include -c jni/main.cpp -o ./obj/local/armeabi/objs/native-activity/main.o
[armeabi] Compile thumb : android_native_app_glue <= android_native_app_glue.c
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc -MMD -MP -MF ./obj/local/armeabi/objs/android_native_app_glue/android_native_app_glue.o.d -fpic -ffunction-sections -funwind-tables -fstack-protector -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -mthumb -Os -g -DNDEBUG -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -I/home/schooten/bin/packages/android-ndk-r9d/sources/android/native_app_glue -DANDROID -Wa,--noexecstack -Wformat -Werror=format-security -I/home/schooten/bin/packages/android-ndk-r9d/platforms/android-9/arch-arm/usr/include -c /home/schooten/bin/packages/android-ndk-r9d/sources/android/native_app_glue/android_native_app_glue.c -o ./obj/local/armeabi/objs/android_native_app_glue/android_native_app_glue.o
[armeabi] StaticLibrary : libandroid_native_app_glue.a
rm -f obj/local/armeabi/libandroid_native_app_glue.a
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar crsD ./obj/local/armeabi/libandroid_native_app_glue.a ./obj/local/armeabi/objs/android_native_app_glue/android_native_app_glue.o
[armeabi] StaticLibrary : libstdc++.a
rm -f obj/local/armeabi/libstdc++.a
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar crsD ./obj/local/armeabi/libstdc++.a
[armeabi] SharedLibrary : libnative-activity.so
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ -Wl,-soname,libnative-activity.so -shared --sysroot=/home/schooten/bin/packages/android-ndk-r9d/platforms/android-9/arch-arm ./obj/local/armeabi/objs/native-activity/main.o ./obj/local/armeabi/libandroid_native_app_glue.a jni/lib/libv8_libbase.arm.a jni/lib/libv8_nosnapshot.a jni/lib/libv8_base.a jni/lib/libstlport_static.a ./obj/local/armeabi/libstdc++.a -lgcc -no-canonical-prefixes -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -L/home/schooten/bin/packages/android-ndk-r9d/platforms/android-9/arch-arm/usr/lib -llog -landroid -lEGL -lGLESv1_CM -llog -lstdc++ -lc -lm -o ./obj/local/armeabi/libnative-activity.so
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/native-activity/main.o: in function __clearColor(v8::FunctionCallbackInfo<v8::Value> const&):jni/main.cpp:88: error: undefined reference to 'v8::Value::Int32Value() const'
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/native-activity/main.o: in function __clearColor(v8::FunctionCallbackInfo<v8::Value> const&):jni/main.cpp:89: error: undefined reference to 'v8::Value::Int32Value() const'
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/native-activity/main.o: in function __clearColor(v8::FunctionCallbackInfo<v8::Value> const&):jni/main.cpp:90: error: undefined reference to 'v8::Value::Int32Value() const'
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/native-activity/main.o: in function __clearColor(v8::FunctionCallbackInfo<v8::Value> const&):jni/main.cpp:91: error: undefined reference to 'v8::Value::Int32Value() const'
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/native-activity/main.o: in function run_javascript(char*):jni/main.cpp:101: error: undefined reference to 'v8::Isolate::New()'
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/native-activity/main.o: in function run_javascript(char*):jni/include/v8.h:4045: error: undefined reference to 'v8::Isolate::Enter()'
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/native-activity/main.o: in function run_javascript(char*):jni/main.cpp:104: error: undefined reference to 'v8::HandleScope::HandleScope(v8::Isolate*)'
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/native-activity/main.o: in function run_javascript(char*):jni/main.cpp:108: error: undefined reference to 'v8::ObjectTemplate::New(v8::Isolate*)'
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/native-activity/main.o: in function run_javascript(char*):jni/main.cpp:110: error: undefined reference to 'v8::ObjectTemplate::New(v8::Isolate*)'
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/native-activity/main.o: in function run_javascript(char*):jni/main.cpp:112: error: undefined reference to 'v8::String::NewFromUtf8(v8::Isolate*, char const*, v8::String::NewStringType, int)'
... etc ...
特别是,这一行告诉我 *.a 文件包括在内:
/home/schooten/bin/packages/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ -Wl,-soname,libnative-activity.so -shared --sysroot=/home/schooten/bin/packages/android-ndk-r9d/platforms/android-9/arch-arm ./obj/local/armeabi/objs/native-activity/main.o ./obj/local/armeabi/libandroid_native_app_glue.a jni/lib/libv8_libbase.arm.a jni/lib/libv8_nosnapshot.a jni/lib/libv8_base.a jni/lib/libstlport_static.a ./obj/local/armeabi/libstdc++.a -lgcc -no-canonical-prefixes -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -L/home/schooten/bin/packages/android-ndk-r9d/platforms/android-9/arch-arm/usr/lib -llog -landroid -lEGL -lGLESv1_CM -llog -lstdc++ -lc -lm -o ./obj/local/armeabi/libnative-activity.so
我的 Android.mk 看起来像这样:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := stlport
LOCAL_MODULE_FILENAME := stlport_static
LOCAL_SRC_FILES := lib/libstlport_static.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := v8_base
LOCAL_MODULE_FILENAME := v8_base_static
LOCAL_SRC_FILES := lib/libv8_base.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := v8_nosnapshot
LOCAL_MODULE_FILENAME := v8_nosnapshot_static
LOCAL_SRC_FILES := lib/libv8_nosnapshot.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := v8_libbase
LOCAL_MODULE_FILENAME := v8_libbase_static
LOCAL_SRC_FILES := lib/libv8_libbase.arm.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := native-activity
LOCAL_SRC_FILES := main.cpp
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM
LOCAL_STATIC_LIBRARIES := android_native_app_glue v8_libbase v8_nosnapshot v8_base stlport
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
LOCAL_STATIC_LIBRARIES 顺序是凭经验确定的。我又摆弄了一些,但无济于事。我什至尝试了 LOCAL_WHOLE_STATIC_LIBRARIES。我用 nm 检查了符号的存在:
arm-linux-gnueabi-nm jni/lib/libv8_base.a | grep Int32Value
U _ZNK2v85Value10Int32ValueEv
00000000 T _ZNK2v85Value10Int32ValueEv
U _ZNK2v85Value10Int32ValueEv
arm-linux-gnueabi-nm jni/lib/libv8_base.a | grep Utf8Value
00000000 T _ZN2v86String9Utf8ValueC1ENS_6HandleINS_5ValueEEE
00000000 T _ZN2v86String9Utf8ValueC2ENS_6HandleINS_5ValueEEE
00000000 T _ZN2v86String9Utf8ValueD1Ev
00000000 T _ZN2v86String9Utf8ValueD2Ev
U _ZN2v86String9Utf8ValueC1ENS_6HandleINS_5ValueEEE
U _ZN2v86String9Utf8ValueD1Ev
U _ZN2v86String9Utf8ValueC1ENS_6HandleINS_5ValueEEE
U _ZN2v86String9Utf8ValueD1Ev
U _ZN2v86String9Utf8ValueC1ENS_6HandleINS_5ValueEEE
U _ZN2v86String9Utf8ValueD1Ev
U _ZN2v86String9Utf8ValueC1ENS_6HandleINS_5ValueEEE
U _ZN2v86String9Utf8ValueD1Ev
U _ZN2v86String9Utf8ValueC1ENS_6HandleINS_5ValueEEE
U _ZN2v86String9Utf8ValueD1Ev
我不是命名专家,但似乎他们在那里。这些值都是零,但我相信这是正常的。
所以我想,也许链接器不理解静态库,它会默默地失败。
但是我如何检查为什么链接器无法拾取符号?