undefined reference to `__android_log_print'

2019-01-16 08:06发布

What is wrong with my make file?

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := foo
LOCAL_SRC_FILES := foo.c
LOCAL_EXPORT_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)

foo.c

#include <string.h>
#include <jni.h>
#include <android/log.h>

#define  LOG_TAG    "foo"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)

void test() {
    LOGI("test");
}

ndk-build

foo.c:9: undefined reference to `__android_log_print'

12条回答
一纸荒年 Trace。
2楼-- · 2019-01-16 08:13

Try the following in your Android.mk file:

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
查看更多
何必那么认真
3楼-- · 2019-01-16 08:13

Add

LOCAL_SHARED_LIBRARIES:= \
        libbinder                       \
        liblog                          \

to Android.mk

查看更多
Anthone
4楼-- · 2019-01-16 08:14

You need to add

LOCAL_LDLIBS := -llog

to Android.mk

查看更多
放荡不羁爱自由
5楼-- · 2019-01-16 08:16

This helped for me:

Android.mk

    LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE    := nativeDemo
LOCAL_SRC_FILES := main.cpp
LOCAL_LDLIBS += -llog

include $(BUILD_SHARED_LIBRARY)
查看更多
狗以群分
6楼-- · 2019-01-16 08:18

If you use Android Studio and gradle, it ignores Android.mk. Add this to your build.gradle file:

android {
    defaultConfig {
        ndk {
            moduleName "your_module_name"
            ldLibs "log"
        }
    }
}
查看更多
该账号已被封号
7楼-- · 2019-01-16 08:19

Yes, you do need to add: LOCAL_LDLIBS := -llog as the other answers/comments have specified, however the original question did not specify if he use the jni library as: LOCAL_JNI_SHARED_LIBRARIES or as LOCAL_REQUIRED_MODULES.

I can pretty much say for sure that he has it used it as: LOCAL_REQUIRED_MODULES because of the LOCAL_EXPORT_LDLIBS := -llog in the question... unless that was added after an edit.

If you use LOCAL_REQUIRED_MODULES the shared library is installed in /system/lib instead of into the apk, because it is a required module. Therefore you will need to add LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog instead of just LOCAL_LDLIBS := -llog so that when the build system is building & linking the jni shared library, it will have the -llog definitions in the correct place, available to be built under $OUT/root/system/lib. Otherwise you will continue to get the same answer, even if you only add LOCAL_LDLIBS := -llog.

So, those who commented that the -L is not needed, and the other answer was correct, they were actually incorrect in this situation.

查看更多
登录 后发表回答