programing

`__android_log_print '에 대한 정의되지 않은 참조

nasanasas 2020. 8. 24. 18:50
반응형

`__android_log_print '에 대한 정의되지 않은 참조


내 make 파일에 어떤 문제가 있습니까?

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 빌드

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

Android.mk파일 에서 다음을 시도 하십시오.

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog

추가해야합니다.

LOCAL_LDLIBS := -llog

Android.mk로


Android Studio 및 gradle을 사용하는 경우 Android.mk를 무시합니다. build.gradle 파일에 다음을 추가하십시오.

android {
    defaultConfig {
        ndk {
            moduleName "your_module_name"
            ldLibs "log"
        }
    }
}

Android Studio 2.2 및 tools.build:gradle:2.2.0의 경우 CMake를 사용하여 CMakeLists.txt에서 행을 추가하거나 편집합니다.

target_link_libraries(<your_library_name> 
                      android 
                      log)

로그 라이브러리를 연결하는 것입니다.


Android Studio 2.1로 업그레이드하면 위의 답변이 작동하지 않으므로 ldLibs.add ()를 사용하여 아래와 같이 lib를로드해야합니다.

android.ndk {
    moduleName = "[the_module_name]"
    ldLibs.addAll(['android', 'log'])
}

Android에서 공유 라이브러리를 3 가지 방법으로 연결할 수 있습니다. 3 개 이하의 경우 언급 된 줄을Android.mk

여기에 세 가지 방법이 있습니다.

1. LOCAL_LDLIBS way
LOCAL_LDLIBS := -llog

어떤 이유로 1이 작동하지 않으면 (나에게 적합하지 않음), 아래 두 가지 방법을 시도 할 수 있습니다.

2. LOCAL_LDFLAGS way
LOCAL_LDFLAGS := -llog

3. LOCAL_SHARED_LIBRARIES way
LOCAL_SHARED_LIBRARIES += liblog

물론 #include <android/log.h>C / H 파일 에도 포함시켜야 합니다.


대신

If using the new Gradle NDK integration in Android Studio 1.3, you need to add ldLibs = ["android", "log"] to your android.ndk options – Stephen Kaiser Sep 24 at 4:20

use ldLibs.addAll(["android", "log"]) for the experimental plugin


Add

LOCAL_SHARED_LIBRARIES:= \
        libbinder                       \
        liblog                          \

to Android.mk


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.


In case the project you are working on has the following characteristics that differ from other 'standard' answers:

  • Not using Android Studio
  • Not using gradle and the integrated CMake
  • No Android.mk or Application.mk used at all for build
  • Using CMake and the toolchain directly (maybe your project is Qt based and without using QtCreator neither)

The following target_link_libraries usage makes it:

    find_library(ANDROID_LOG_LIB log)
    target_link_libraries(${TARGET_NAME} ${ANDROID_LOG_LIB})

Being TARGET_NAMEthe name of the target to build (having set it up before with add_library or add_executable).

find_library is equally important as well as setting up the toolchain properly (use the toolchain provided by Android SDK at ANDROID_SDK_HOME/cmake/<version>/android.toolchain.cmake so it sets up CMAKE_SYSROOTwhich is used by find_ commands).


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)

In the android studio version 2.2 and higher, there is inbuilt support for CPP when you create a new project. Also, the liblog.so is included by default. Nothing to be done apart from including the header file (android/log.h).

Checkout app/CMakeLists.txt that is created by the studio when we create new android studio project. We can see that the find_library() block and target_link_libraries() block for loglib are already present.

Also, pay attention towards the function syntax. It should be:

__android_log_print (int priority, const char *tag, const char *fmt,...);

In my case, I had left out tag parameter and ended up spending good 3 days in figuring it out.

More about CMake: Add C and C++ Code to Your Project

참고URL : https://stackoverflow.com/questions/4455941/undefined-reference-to-android-log-print

반응형