undefined reference to `__android_log_print' - android

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'

You need to add
LOCAL_LDLIBS := -llog
to Android.mk

Try the following in your Android.mk file:
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog

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"
}
}
}

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.

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'])
}

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

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.

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

Add
LOCAL_SHARED_LIBRARIES:= \
libbinder \
liblog \
to Android.mk

-DCMAKE_CXX_FLAGS="-llog" helps me

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)

for the case who use CMakeLists.txt to make
externalNativeBuild {
cmake {
path 'src/main/cpp/CMakeLists.txt'
}
}
just add -llog into target_link_libraries:
target_link_libraries(<your_library_name> -llog)
the env I used are :
gradle-6.5-all.zip
classpath 'com.android.tools.build:gradle:4.1.1'
Android Studio Arctic Fox | 2020.3.1 Patch 1
Build #AI-203.7717.56.2031.7621141, built on August 8, 2021

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

add
LOCAL_SHARED_LIBRARIES:= liblog
to Android.mk can solve my isuue.
This is because the __android_log_print is defined in libLog

TO build with Android.bp, follow the below solution:
In this -android_log_print is defined in NDK, so for this, there is already a library is available. Use "liblog" library using shared_libs tag, take reference of the below code:
target: {
android: {
cppflags: [
"-g",
"-DUSE_LIBLOG",
],
shared_libs: ["liblog"], // can use other dependency if required.
},
darwin: {
enabled: false,
},
},

Related

Android can't include linux in C program with Android.mk

I'm trying to compile a C program for Android 6. This is my Android.mk:
APP_PLATFORM := android-23
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Enable PIE manually. Will get reset on $(CLEAR_VARS). This
# is what enabling PIE translates to behind the scenes.
LOCAL_CFLAGS += -fPIE -DHAVE_FANOTIFY=1 -DHAVE_SYS_FANOTIFY=0
LOCAL_LDFLAGS += -fPIE -pie
# give module name
LOCAL_MODULE := fsmon
# list your C files to compile
LOCAL_SRC_FILES := inotify.c fanotify.c util.c main.c
# this option will build executables instead of building library for android application.
include $(BUILD_EXECUTABLE)
In the fanotify.c following include is written:
#include <linux/fanotify.h>
When I try to use ndk-build, following error appears:
fsmon/jni/fanotify.c:51:10: fatal error: 'linux/fanotify.h' file not found
#include <linux/fanotify.h>
^
The header fanotify.h is present in the ndk path /Android/Sdk/ndk-bundle/sysroot/usr/include/linux
Any suggestions?
EDIT: Same error if I try to include sys/fanotify.h
You can specify additional include paths for your module using LOCAL_C_INCLUDES.
LOCAL_C_INCLUDES := /Android/Sdk/ndk-bundle/sysroot/usr/include/
https://developer.android.com/ndk/guides/android_mk.html#mdv
The NDK historically didn't backport headers to old releases, but we've reworked things in r14 so this is possible: https://android.googlesource.com/platform/ndk/+/ndk-r14-release/docs/UnifiedHeaders.md
By default in r14 you still get the old form of the headers. The new "unified headers" have the headers you're looking for. If you want to try unified headers, set APP_UNIFIED_HEADERS := true in your Application.mk (settings for other build systems can be found in the link above).
In r15 (first beta due out soon), the default has changed to the new headers, and the option for disabling them has changed (see the same doc in r15 for changes in options: https://android.googlesource.com/platform/ndk/+/ndk-r15-release/docs/UnifiedHeaders.md).

Android: How to link my own static libraries correctly?

I have an Android project written in C++ and have a problem in linking phase. The code is put in some static libraries which should be linked together.
I have found a lot of questions and answers on the net about this topic and most of them suggest to put my libraries to LOCAL_STATIC_LIBRARIES in the Android.mk file. But, if I do this, I found the content of LOCAL_STATIC_LIBRARIES is simply ignored: my libraries are not linked, and adding any dummy text here does not generate any error or warning message.
I tried it this way:
LOCAL_STATIC_LIBRARIES := MyLib.a
or with full path:
LOCAL_STATIC_LIBRARIES := $(LOCAL_PATH)/MyLib.a
none of them worked.
If I put my static libraries to LOCAL_LDLIBS then it is linked, but I got a warning message about non-system libraries are used, and probably the build will be wrong.
The content of my Android.mk file is:
LOCAL_LDLIBS := $(LOCAL_PATH)/MyLib.a ...
and I got this message:
Android NDK: WARNING:jni/Android.mk:myapp: non-system libraries in linker flags: jni/MyLib.a
Android NDK: This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES
Android NDK: or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the
Android NDK: current module
I could not find how to use LOCAL_STATIC_LIBRARIES right way, please help me!
I have android-ndk-r9 and android-sdk_r22.2.1 on a OpenSuSE x86 and using target=android-18
See JBL's answer here.
The LOCAL_STATIC_LIBRARIES variable does not work that way. First you need a section that defines the library you want to include:
include $(CLEAR_VARS)
LOCAL_PATH = .
LOCAL_MODULE := curl
LOCAL_EXPORT_C_INCLUDES := ../curl/include
LOCAL_SRC_FILES := ../curl/lib/libcurl.a
include $(PREBUILT_STATIC_LIBRARY)
THEN, you can include it using
include $(CLEAR_VARS)
LOCAL_MODULE = mylib
CFLAGS = ...
...
LOCAL_STATIC_LIBRARIES = curl
include $(BUILD_STATIC_LIBRARY)
Most probably the problem lies in that you are giving the extension of the library:
LOCAL_STATIC_LIBRARIES := MyLib.a
I think, it should be written as:
LOCAL_STATIC_LIBRARIES := MyLib

How to render a bitmap in NDK using OpenGL ES 2.0

OS: Windows 7
SDK: adt-bundle-windows-x86-20130717
NDK: android-ndk-r8e
Eclipse ADT: Build: v22.0.4-741630
I'm trying to render a bitmap using OpenGL ES in JNI. I have these headers declared at the top of the file:
#include <android/bitmap.h>
#include <GLES2/gl2.h>
so I don't understand why tokens such as GL_TEXTURE_2D are reported as "could not be resolved" and the project doesn't build. The frustrating part is that functions and variables defined in bitmap.h such as AndroidBitmap_getInfo() and ANDROID_BITMAP_FORMAT_RGB_565 don't cause a problem; they resolve properly.
The C/C++>General>Paths and Symbols properties for the project has this path:
C:\Android\android-ndk-r8e-windows-x86\android-ndk-r8e\platforms\android-9\arch-arm\usr\include
I see the tree structure in the header node of the Eclipse project and I've confirmed that android & GLES2 folders exist at that path location and each contain the appropriate .h file (bitmap.h and gl2.h respectively). How can it be that bitmap.h resolves but gl2.h doesn't? I'm declaring the includes exactly the same way!
Application.mk:
APP_PLATFORM := android-10
APP_ABI := armeabi-v7a
Any troubleshooting tips?
Thanks
Have you ever tried the sample code bitmap-plasma & hello-gl2 in NDK package? you can check relevant Android.mk. make sure you have linked correct libraries
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libgl2jni
LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := gl_code.cpp
LOCAL_LDLIBS := -llog -lGLESv2 -lm -ljnigraphics
include $(BUILD_SHARED_LIBRARY)

AOSP building : Make file shared libraries issue

Following is my MAKE file for the source that i'm compiling with the build AOSP
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= abc.c
LOCAL_MODULE:= abc
LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_STATIC_LIBRARIES := libc
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_MODULE_TAGS := debug
LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/../../../external/sqlite/dist \
$(LOCAL_PATH)/../../../external/sqlite/android
LOCAL_SHARED_LIBRARIES := \
libsqlite \
libsqlite3_android
include $(BUILD_EXECUTABLE)
here, in the source abc.c i'm trying to use the functions declared in sqlite3.h. When i'm trying to build the android source it is returning error
no rule libsqlite3_android.so to make target abc.so
i want to link the sqlite library to my source file.
Plz help me to find where i'm going wrong and how can i solve the problem.
First, make sure you have built SQLite3 before. (You should build the whole project before add any new stuff or customize any code)
Second, make sure you are build the same target product when you build the SQLite3. (Make sure you have select the correct menu when you do 'lunch').
Last, make sure the SQLite3 objects is in the target folders. YOURANDROIDROOT/out/target/PRODUCTNAME/system/symbols...
In fact, you shouldn't have to link with libsqlite3_android library.
According to the AOSP libsqlite makefile (external/sqlite/android/Android.mk), libsqlite3_android is a STATIC library which is included in the libsqlite dynamic library (external/sqlite/dist/Android.mk).
So linking with libsqlite should be enough.

android external/stlport include in Android.mk build not successfull

I m trying to build an app with android-froyo source in which I am using skia and stl templates,
I have included
MY_INCLUDES=external/zlib external/jpeg external/freetype/include \
frameworks/base/core/jni/android/graphics external/skia/include/core \
external/libpng external/expat/lib <b>external/stlport/stlport</b>
libstlport_cflags := -D_GNU_SOURCE
libstlport_cppflags := -fuse-cxa-atexit
LOCAL_CPPFLAGS := $(libstlport_cppflags)
include $(BUILD_STATIC_LIBRARY)
I get the following error when i try to build the android source with this app, which i kept at packages/apps:
external/stlport/stlport/stl/_new.h:47:50: error: libstdc++/include/new: No such file or directory
Please guide me to rectify this issue.
Thanks
Mohit
As I understand the file which cannot be found by preprocessor is located in bionic folder.
I had the same issue and I solved it by adding the following line:
LOCAL_C_INCLUDES += bionic
I haven't tried this with Android 2.2 but I'm using Android Kitkat (4.4).
To get the stlport library working with our project we included it in our project's Android.mk as so:
include external/stlport/libstlport.mk
This is assuming that on Froyo, there is a libstlport.mk file to include in your build process. In 4.4, there is also a Android.mk file but that builds other code as well and builds stlport as a static library (which is not what we wanted).
You may need to also add the include directory as well, something like: external/stlport/stlport.
cpp
#include <stdio.h>
// The code
// The set of definitions and includes for STLPort
// They used defined() instead of #ifdef.
#define _STLP_HAS_INCLUDE_NEXT 1
#define _STLP_USE_MALLOC 1
#define _STLP_USE_NO_IOSTREAMS 1
#include <stl/config/_android.h>
#include <map>
#include <string>
int main(void)
{
std::string a = "abc";
printf("%s",a.c_str());
return 0;
}
Android.mk
# A simple test for the minimal standard C++ library
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := test-libstl.cpp
LOCAL_C_INCLUDES += sources/cxx-stl/stlport/stlport
LOCAL_SHARED_LIBRARIES += libstlport
LOCAL_MODULE := test-libstl
include $(BUILD_EXECUTABLE)

Categories

Resources