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条回答
孤傲高冷的网名
2楼-- · 2019-01-16 08:20

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).

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-16 08:21

If you upgrade to Android Studio 2.1, above answers do not work, need use ldLibs.add() to load the lib as below:

android.ndk {
    moduleName = "[the_module_name]"
    ldLibs.addAll(['android', 'log'])
}
查看更多
相关推荐>>
4楼-- · 2019-01-16 08:25

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

查看更多
【Aperson】
5楼-- · 2019-01-16 08:27

We can link a shared library in Android in 3 ways. In below 3 cases, the lines mentioned should be added in Android.mk

So here are the three ways.

1. LOCAL_LDLIBS way
LOCAL_LDLIBS := -llog

For some reason if 1 doesnt work(it did not work for me), You can try below 2 ways

2. LOCAL_LDFLAGS way
LOCAL_LDFLAGS := -llog

3. LOCAL_SHARED_LIBRARIES way
LOCAL_SHARED_LIBRARIES += liblog

Of course you also need to include #include <android/log.h> in your C/H file.

查看更多
Bombasti
6楼-- · 2019-01-16 08:33

For Android Studio 2.2 and tools.build:gradle:2.2.0 using CMake add or edit row in CMakeLists.txt:

target_link_libraries(<your_library_name> 
                      android 
                      log)

Thats connecting log library to yours.

查看更多
The star\"
7楼-- · 2019-01-16 08:36

In lieu with

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

查看更多
登录 后发表回答