【问题汇总】在C/C++中使用Android Log导致编译失败的问题

在JNI中使用Android自带的Log日志功能,在编译时提示如下异常:
kaizen@tuan800-inc:~/workspace_eclipse_1/im_pulltorefresh/MyJNITest$ ndk-build
[armeabi] SharedLibrary  : libuninstalled_observer.so
/home/kaizen/developer/android-ndk-r10b/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/uninstalled_observer/main_activity_UninstalledObserverActivity.o: in function Java_main_activity_UninstalledObserverActivity_init:jni/main_activity_UninstalledObserverActivity.c:30: error: undefined reference to '__android_log_write'
/home/kaizen/developer/android-ndk-r10b/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/uninstalled_observer/main_activity_UninstalledObserverActivity.o: in function Java_main_activity_UninstalledObserverActivity_init:jni/main_activity_UninstalledObserverActivity.c:51: error: undefined reference to '__android_log_write'
/home/kaizen/developer/android-ndk-r10b/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/uninstalled_observer/main_activity_UninstalledObserverActivity.o: in function Java_main_activity_UninstalledObserverActivity_init:jni/main_activity_UninstalledObserverActivity.c:75: error: undefined reference to '__android_log_write'
/home/kaizen/developer/android-ndk-r10b/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/uninstalled_observer/main_activity_UninstalledObserverActivity.o: in function Java_main_activity_UninstalledObserverActivity_init:jni/main_activity_UninstalledObserverActivity.c:80: error: undefined reference to '__android_log_write'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libuninstalled_observer.so] Error 1

观察错误日志,可以看出,是由于找不到__android_log_write这个函数。

在该工程的Android.mk文件中,添加如下代码:

LOCAL_LDLIBS += -llog

再次执行ndk-build,成功输出so文件。

kaizen@tuan800-inc:~/workspace_eclipse_1/im_pulltorefresh/MyJNITest$ ndk-build
[armeabi] Compile thumb  : uninstalled_observer <= main_activity_UninstalledObserverActivity.c
jni/main_activity_UninstalledObserverActivity.c: In function 'Java_main_activity_UninstalledObserverActivity_init':
jni/main_activity_UninstalledObserverActivity.c:141:72: warning: embedded '\0' in format [-Wformat-contains-nul]
[armeabi] SharedLibrary  : libuninstalled_observer.so
[armeabi] Install        : libuninstalled_observer.so => libs/armeabi/libuninstalled_observer.so


你可能感兴趣的:(【问题汇总】在C/C++中使用Android Log导致编译失败的问题)