I have recently purchased a book to assist in my development of C++ for Android which contained some code samples. However, when I come to build some of the sample code, I receive the following error:
C:\ndk\android-ndk-r9\ndk-build.cmd all
"Compile++ : Chapter10 <= Chapter10.cpp
process_begin: CreateProcess(NULL, C:/ndk/android-ndk-r9/toolchains/arm-linux-androideabi-4.7/prebuilt/windows/bin/arm-linux-androideabi-g++ -MMD -MP -MF ./obj/local/armeabi/objs/Chapter10/Chapter10.o.d -fno-exceptions -fno-rtti -Ijni -IC:/ndk/android-ndk-r9/sources/android/native_app_glue -IC:/ndk/android-ndk-r9/sources/cxx-stl/stlport/stlport -IC:/ndk/android-ndk-r9/sources/cxx-stl//gabi++/include -Ijni -DANDROID -Wa,--noexecstack -Wformat -Werror=format-security -frtti -c jni/Chapter10.cpp -o ./obj/local/armeabi/objs/Chapter10/Chapter10.o, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [obj/local/armeabi/objs/Chapter10/Chapter10.o] Error 2
The make file is as shown below:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_MODULE := Chapter10
LOCAL_SRC_FILES := Chapter10.cpp \
(Other cpp Files . . . )
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2 -lOpenSLES
LOCAL_STATIC_LIBRARIES := android_native_app_glue
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
The Application.mk file is as below:
APP_PLATFORM := android-9
APP_STL := stlport_static
NDK_TOOLCHAIN_VERSION := 4.7
This was compiled using ADT v22.2.1 and Android NDK r9 on a Windows 7 Machine.
The NDK was installed to 'C:\ndk\android-ndk-r9\'.
Android NDK r9 contains the following toolchains:
arm-linux-androideabi-4.6
arm-linux-androideabi-4.8
arm-linux-androideabi-clang3.2
arm-linux-androideabi-clang3.3
llvm-3.2
llvm-3.3
mipsel-linux-android-4.6
mipsel-linux-android-4.8
mipsel-linux-android-clang3.2
mipsel-linux-android-clang3.3
x86-4.6
x86-4.8
x86-clang3.2
x86-clang3.3
There is no toolchain for gcc 4.7. However, your Application.mk contains the line:
NDK_TOOLCHAIN_VERSION := 4.7
Which tells the NDK to look for the 4.7 toolchain. And it fails.
So, the solution to your problem is changing the NDK_TOOLCHAIN_VERSION variable to 4.6, 4.8, clang3.2, clang3.3, or just clang (which will use the most recent version of Clang available in the NDK).
Check your project path if is contain spaces and non-english characters.
I moved my project into somewhere without spaces, re-build it and works.
Related
I'm trying to implement an OEX (open engine exchange protocol) chess program for Android.
This has been discussed at What is OEX (Open Exchange Protocol?) and can I call such APK from my app? earlier.
The java-part of the program runs fine, yet the native library always segfaults. Even with something simple as
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello, world!\n");
return 0;
}
I noticed one thing to be different: the libstockfish.so library seems to be linked to /system/bin/linker while my library is not linked at all?
file libs/x86_64/libstockfish.so:
ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically
linked, interpreter /system/bin/linker64,
BuildID[sha1]=b690a6bb7630099c6618950a9e1604850f1de836, stripped
file libs/x86_64/libDog.so
libs/x86_64/libDog.so: ELF 64-bit LSB shared object, x86-64, version 1
(SYSV), dynamically linked,
BuildID[sha1]=93a9d3635eef9fc0f2140a87956db9ac80459993, stripped
What is it that I could be doing wrong?
I'm using the ndk-build script:
jni/Application.mk:
APP_ABI := all
APP_PLATFORM := 21
#NDK_TOOLCHAIN_VERSION := 4.9
APP_STL := c++_shared
APP_CPPFLAGS += -frtti
APP_CPPFLAGS += -std=c++17
jni/Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Dog
LOCAL_SRC_FILES := test.cpp
LOCAL_STRIP_MODE := none
LOCAL_PRELINK_MODULE := false
LOCAL_CPPFLAGS += -std=c++17 -Wall -DVERSION=\"0.8\" -DNAME_EXTRA=\"\" -fexceptions -Wno-c++11-narrowing -fPIC -pie -fPIE
LOCAL_LDLIBS += -shared
LOCAL_LDFLAGS += -fPIC -pie -fPIE -Wl,--entry=main,-dynamic-linker=/system/bin/linker
LOCAL_CPP_FEATURES := exceptions
include $(BUILD_SHARED_LIBRARY)
Any ideas?
Found the solution: apparently the idea is to trick the packaging system by creating a regular binary and renaming it to e.g. libstockfish.so. Then the board-programs can happily import them and play chess with them.
I covert a ADT project to Android Studio. however, i kept getting the error when building this project:
Error:(687) Android NDK: Module MediaEncoder depends on undefined modules: cutils gnustl dl
This is what is in the Android.mk. I am new to NTK, anybody can advise the issue here?
# building application library
#
include $(CLEAR_VARS)
LOCAL_MODULE := libMediaEncoder
LOCAL_CPP_EXTENSION := .cc .cpp
LOCAL_CPPFLAGS := -O2 -Werror -Wall
LOCAL_C_INCLUDES := $(MY_LOCAL_PATH)
LOCAL_SRC_FILES := main_jni.cpp \
h264encoder.cpp \
g72x/g726_32.c \
g72x/g711.c \
g72x/g72x.c
LOCAL_LDLIBS += -llog -lz
LOCAL_SHARED_LIBRARIES := libcutils\
libgnustl\
libdl
I believe that you use a project that was configured to be built in the context of AOSP build, not by NDK. But it could still work with a very old version of NDK.
The current version, r14 which is integrated into Android Studio 2.3, requires some changes to Android.mk.
include $(CLEAR_VARS)
LOCAL_MODULE := libMediaEncoder
LOCAL_CPPFLAGS := -O2 -Werror -Wall
LOCAL_C_INCLUDES := $(MY_LOCAL_PATH)
LOCAL_SRC_FILES := main_jni.cpp \
h264encoder.cpp \
g72x/g726_32.c \
g72x/g711.c \
g72x/g72x.c
LOCAL_LDLIBS += -llog -lz -ldl
LOCAL_SHARED_LIBRARIES := libcutils_prebuilt
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libcutils_prebuilt
LOCAL_SRC_FILES := {full-path-to}/libcutils.so
include $(PREBUILT_SHARED_LIBRARY)
To resolve the dependency on gnustl, run ndk-build APP_STL=gnustl_static, or define APP_STL in your Application.mk file.
Note that your project uses non-public system library libcutils.so. This library was once included in NDK (see https://stackoverflow.com/a/22017733/192373), but for the last 3 years Google has been struggling to discourage linking to it.
You can build libcutils.so yourself as part of AOSP, or you can adb pull it from your device or even from a compatible emulator. You can also find this binary somewhere on the Web (e.g. GitHub).
The final blow comes with the announced changes to system linking for Android 7.0 Nougat. TL;NR: any app that depends on this library will not work on future Android versions.
You need to install an older version of ndk Android NDK, Revision 10e (May 2015) worked for me and then use that ndk-build
am compiling android ndk project but couldn't get it successfully done. It says no such file or directory found for Log header file at main.cpp file. I'm new to android ndk please help.
here is my android.mk file.
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := user
LOCAL_ARM_MODE := arm
# This is the target being built.
LOCAL_MODULE := libemu
# All of the source files that we will compile.
LOCAL_SRC_FILES := \
ticks.c \
main.cpp \
emulator.cpp
# All of the shared libraries we link against.
LOCAL_SHARED_LIBRARIES := \
libdl \
libnativehelper \
libutils
# Static libraries.
LOCAL_STATIC_LIBRARIES :=
# Also need the JNI headers.
LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE)
# Special compiler flags.
LOCAL_CFLAGS += -O3 -fvisibility=hidden
# Don't prelink this library. For more efficient code, you may want
# to add this library to the prelink map and set this to true. However,
# it's difficult to do this for applications that are not supplied as
# part of a system image.
LOCAL_PRELINK_MODULE := false
include $(BUILD_SHARED_LIBRARY)
here is the log
"Compile++ arm : emu <= main.cpp
D:/EclipseWorkspace/NineTendo//jni/main.cpp:2:23: fatal error: utils/Log.h: No such file or directory
compilation terminated.
make: *** [D:/EclipseWorkspace/NineTendo//obj/local/armeabi/objs/emu/main.o] Error 1
any help would be much appreciated.
Thanks
This error started to show up in NDK version R9x. You need to declare the API version number in your Application.mk file. Here's how it looks to define a minimum API level of 8:
APP_PLATFORM := android-8
The Application.mk file should live alongside your Android.mk file in the JNI directory. Here's a sample from one of my projects:
APP_ABI := armeabi armeabi-v7a x86
APP_CFLAGS += -O2
LOCAL_ARM_MODE := arm
APP_PLATFORM := android-8
I am attempting to compile an Android-9 project that calls Android NDK functions (such as "AAsset_close") by running "ndk-build" via the Cygwin terminal, but the "ndk-build" command returns an error message, stating that it is unable to find the function definitions.
The error messages from "ndk-build" are as follows:
[Android NDK path]/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: [project folder]/static library: in function [function name]:[file name]:39: error: undefined reference to 'AAsset_close'
etc.
My "Android.mk" file is as follows:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := <name of project>
LOCAL_C_INCLUDES := <folders where header files are located>
LOCAL_CFLAGS := $(LOCAL_C_INCLUDES:%=-I%) -O2 -Wall -D__ANDROID__ -DtyANDROID
LOCAL_CPPFLAGS := $(LOCAL_C_INCLUDES:%=-I%) -O2 -Wall -D__ANDROID__ -DtyANDROID
LOCAL_LDLIBS := -lm -lEGL -lGLESv2 -llog -lz -landroid
LOCAL_STATIC_LIBRARIES := android_native_app_glue
LOCAL_ARM_MODE := arm
LOCAL_SRC_FILES := <source files>
include $(BUILD_STATIC_LIBRARY)
...
My "Application.mk" makefile is as follows:
APP_ABI := armeabi armeabi-v7a x86
APP_PLATFORM := android-9
According to the "nm" Unix command, this function is defined within the Android NDK library "libandroid.so", which is present in the NDK path for all target platforms. As demonstrated above, I have included "-landroid" to the "LOCAL_LDLIBS" macro, which should allow me to link against that library. What else do I need to do to fix this compile error?
You show the Android.mk that builds a static library, at this step the linker is not called, threfore $(LOCAL_LDLIBS) is ignored. You probably have another Android.mk or a different section, where you have
include $(BUILD_SHARED_LIBRARY)
Please check, -landroid is probably missing in that section.
I am trying to build an android application that calls into a C++ backend. This backend uses ZeroMQ for messaging. Per the android build page on the ZeroMQ guide, I have built a native toolchain of ndk version 6 and used that to (successfully) build ZeroMQ.
However, when I build my own .so with the JNI implementations, I can't seem to make everything load properly. Specifically, if I call System.LoadLibrary("zmq"), this completes successfully, but if I then call *System.LoadLibrary("my_lib")* I always get an UnsatisfiedLinkError complaining that:
Cannot load library reloc_library[1244]: 29 cannot locate zmq_msg_init'...
The libmy_lib.so has been generated several different ways, each with no success. After generating it, I always copies libmy_lib.so (as well as libzmq.so) to my android project's folder: libs/armeabi/.
# Compile all object files - this part was done for all options
/opt/android-toolchain/bin/arm-linux-androideabi-g++ -fpic -c Client_Events.cpp \
Client Wrapper.cpp jni.cpp -I /opt/android-root/include/ -I /my/project/specific/stuff
# Option 1 - Don't link the lib in at all
/opt/android-toolchain/bin/arm-linux-androideabi-g++ -shared \
-Wl,-soname,libmy_lib.so -o libmy_lib.so jni.o Client_Events.o Client_Wrapper.o
# Option 2 - Link ZeroMQ in statically
/opt/android-toolchain/bin/arm-linux-androideabi-g++ -shared \
-Wl,-soname,libmy_lib.so -o libmy_lib.so jni.o Client_Events.o Client_Wrapper.o \
libzmq.a libstdc++.a -Wl,--whole-archive
# Option 3 - Explicitly link ZeroMQ in dynamically
/opt/android-toolchain/bin/arm-linux-androideabi-g++ -shared \
-Wl,-soname,libmy_lib.so -o libmy_lib.so jni.o Client_Events.o Client_Wrapper.o \
-L /opt/android-root/lib/ -lzmq
With each of these options I tried both explicitly calling System.LoadLibrary("zmq") before loading my own library and not. Nothing varied the results. Using nm confirms that, at least in the case of option #2, the missing symbol *zmq_msg_init* is indeed present in libmy_lib.so.
Any ideas as to why it cannot find the ZeroMQ symbols that are linked in?
I just learnt how to compile a 2nd library and link it to my main library in android ndk. Let me see if I am of any use to you.
The following is how I create my 2nd library (In my case, I build bullet physics library and the irrlicht rendering engine as 2 separate libraries for my game).
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := HEADER FILES
LOCAL_MODULE := bullet
LOCAL_SRC_FILES := SRC FILES
LOCAL_ARM_MODE := arm
LOCAL_CFLAGS := $(LOCAL_C_INCLUDES:%=-I%) -O3 -DANDROID_NDK -DDISABLE_IMPORTGL
LOCAL_LDLIBS := -ldl -llog
include $(BUILD_SHARED_LIBRARY)
Then copy your libxxxx.so (In my case, libbullet.so and libirrlicht.so) to your jni folder. And in your main library .mk file add the following.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := (includes for bullet)
LOCAL_MODULE := bullet
LOCAL_SRC_FILES := libbullet.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := (includes for irrlicht)
LOCAL_MODULE := irrlicht
LOCAL_SRC_FILES := libirrlicht.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := (includes for bullet + includes for irrlicht + includes for main code)
LOCAL_SRC_FILES := main code src files
LOCAL_MODULE := gamescript
LOCAL_ARM_MODE := arm
LOCAL_CFLAGS := $(LOCAL_C_INCLUDES:%=-I%) -O3 -DANDROID_NDK -DDISABLE_IMPORTGL
LOCAL_LDLIBS := -lOpenSLES -landroid -ldl -llog
LOCAL_SHARED_LIBRARIES := bullet irrlicht
include $(BUILD_SHARED_LIBRARY)
And now, add all the libraries to your java code in right order.
System.loadLibrary("bullet");
System.loadLibrary("irrlicht");
System.loadLibrary("gamescript");