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

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

Related

How to resolve "Relocations in Generic ELF" during ndk-build?

I'm trying to build a static library using a standalone toolchain for a project that has its own build process, which I then wrap with a C++ library and expose to Android (compiled with ndk-build). However, on the ndk-build step I receive the following error:
➜ jni /Users/chrisfosterelli/Library/Android/sdk/ndk-bundle/ndk-build
[arm64-v8a] Compile++ : wrapper <= wrapper.cpp
In file included from /Users/chrisfosterelli/workspace/android/jni/wrapper.cpp:9:
In file included from ../prebuild/include/valhalla/meili/universal_cost.h:7:
In file included from ../prebuild/include/valhalla/sif/dynamiccost.h:4:
In file included from ../prebuild/include/valhalla/baldr/directededge.h:5:
../prebuild/include/valhalla/baldr/graphconstants.h:432:11: warning: 21 enumeration values not handled in switch: 'kRoad', 'kRamp', 'kTurnChannel'... [-Wswitch]
switch (use) {
^
1 warning generated.
[arm64-v8a] SharedLibrary : libwrapper.so
/Users/chrisfosterelli/Library/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: /Users/chrisfosterelli/workspace/android/jni/../prebuild/libvalhalla_meili.a(libvalhalla_meili_la-map_matcher_factory.o): Relocations in generic ELF (EM: 62)
[...above message repeated many times...]
/Users/chrisfosterelli/workspace/android/jni/../prebuild/libvalhalla_meili.a: error adding symbols: File in wrong format
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/Users/chrisfosterelli/workspace/android/obj/local/arm64-v8a/libwrapper.so] Error 1
➜ jni ls /Users/chrisfosterelli/Library/Android/sdk/ndk-bundle/
This error indicates, as far as I can tell, that I'm trying to mix and match binaries compiled for different architectures. However, from what I can tell the library is the correct architecture:
root#eacbdb1c0e46:/meili/meili2/newtest# ar x libvalhalla_meili.a
root#eacbdb1c0e46:/meili/meili2/newtest# file libvalhalla_meili_la-map_matcher_factory.o
libvalhalla_meili_la-map_matcher_factory.o: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), not stripped
root#eacbdb1c0e46:/meili/meili2/newtest#
FWIW, these are my current Application.mk,
APP_STL := c++_static
APP_CPPFLAGS := -frtti -std=gnu++11 -D_GLIBCXX_USE_C99
APP_CPPFLAGS += -fexceptions
NDK_TOOLCHAIN_VERSION := clang
APP_LDFLAGS := -latomic
APP_PLATFORM := android-21
APP_ABI := arm64-v8a
and Android.mk,
LOCAL_PATH := $(call my-dir)
# static library info
include $(CLEAR_VARS)
LOCAL_MODULE := libvalhalla_meili
LOCAL_SRC_FILES := ../prebuild/libvalhalla_meili.a
LOCAL_EXPORT_C_INCLUDES := ../prebuild/include
include $(PREBUILT_STATIC_LIBRARY)
# wrapper info
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += ../prebuild/include
LOCAL_MODULE := wrapper
LOCAL_SRC_FILES := wrapper.cpp
LOCAL_STATIC_LIBRARIES := libvalhalla_meili
include $(BUILD_SHARED_LIBRARY)
Any ideas how to resolve this? I've tried a number of things but all of them lead to more (more obscure) errors, so I'm hoping someone here can point me in the correct direction!
I discovered the problem, but I wish I had a more insightful answer. Apparently the build process had created two library files. The one that I inspected was, of course, ARM64. However the one that I actually copied to the Android device was x86.
So, the error message was correct and so was the library file, but there was more than one file involved. If you're running into the same problem and pretty sure your library is ARM64, double check that's actually the same file that is being compiled into the ndk build!

fatal error: fitz.h: No such file or directory in muPDF library

I m trying to compile the muPDF lib in eclipse for android app since 3 days without any success
I downloaded and configured the android-ndk, cygwin
Im getting this still
15:25:36 **** Build of configuration Debug for project ChoosePDFActivity ****
"E:\\android-ndk\\android-ndk-r9d\\ndk-build.cmd" all
Android NDK: WARNING:E:/Workspace_SIL/ChoosePDFActivity//jni/Android.mk:mupdfcore: LOCAL_LDLIBS is always ignored for static libraries
Android NDK: WARNING:E:/Workspace_SIL/ChoosePDFActivity//jni/Android.mk:mupdfcore: LOCAL_LDLIBS is always ignored for static libraries
[armeabi] Compile thumb : mupdf <= mupdf.c
E:/Workspace_SIL/ChoosePDFActivity//jni/mupdf.c:10:18: fatal error: fitz.h: No such file or directory
compilation terminated.
make.exe: *** [E:/Workspace_SIL/ChoosePDFActivity//obj/local/armeabi/objs-debug/mupdf/mupdf.o] Error 1
15:25:36 Build Finished (took 489ms)
jni/mupdf.c:10:18: fatal error: fitz.h: No such file or directory
where I can get this God Damn fitz.h and other header files
see this happens in my eclipse
included the following paths for my header files
setup android Ndk path like this
Update1
Here is my Android.mk file
LOCAL_PATH := $(call my-dir)
TOP_LOCAL_PATH := $(LOCAL_PATH)
MUPDF_ROOT := ..
include $(TOP_LOCAL_PATH)/Core.mk
include $(TOP_LOCAL_PATH)/ThirdParty.mk
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := \
$(MUPDF_ROOT)/draw \
$(MUPDF_ROOT)/fitz \
$(MUPDF_ROOT)/pdf
LOCAL_CFLAGS :=
LOCAL_MODULE := mupdf
LOCAL_SRC_FILES := mupdf.c
LOCAL_STATIC_LIBRARIES := mupdfcore mupdfthirdparty
LOCAL_LDLIBS := -lm -llog -ljnigraphics
include $(BUILD_SHARED_LIBRARY)
Check the jni folder and find the Anroid.mk. Open it and check whether you added the LOCAL_SRC_FILES and LOCAL_C_INCLUDES. The LOCAL_C_INCLUDES should be your .h files' path. The LOCAL_SRC_FILES should be every .c or .cpp file's path.
cygwin no t install properly, follow following links and sure your installations correctly than run some commands according to below links.
For window 7
For window XP
You're also using an old version of the MuPDF source code.
1) Use the latest version (1.5 currently, or better, work from git).
2) Don't build it using eclipse, use the supported ndk/sdk based build as described in platform/android/ReadMe.txt. If you want to use eclipse, do so only after you have managed to build it in the supported way.
3) Do not skip step 10 of the instructions. Seriously, why does everyone try to skip step 10?
4) If you get stuck, come talk to us in the #ghostscript irc channel on freenode. A weblink can be found to this on mupdf.com

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)

Eclipse Android Native debugging fails

I have a problem getting the native debugging support to work for my Eclipse project under Windows for Android.
Google unfortunately didn't help after more than 1 day of research.
I am pretty much a beginner with eclipse, so the solution might be easy because I do also some non-standard things I guess.
My android project is already functioning and running fine on my devices (Acer Iconia A500 Android 3.2.1, HTC Incredible S Android 4.0.4 etc.).
I created the project with the ADT Plugin Wizard and then added native support by using the project context menu entry added by ADT.
I then added the java files I already had (previously I used to compile with CMake and the Android stand-alone toolchain, though I never tried to remote debug this way). I created a new package for that "com.x.y" where I dropped them on to (I don't want to reveal the real package name as the project will be part of a commercial product).
Then I added my Sources by dragging them into the jni folder. The sources are not located inside the jni folder but are linked to a location "../../" below the project (Its part of a cross platform application so I can reuse the source files accross different toolchains).
I also link against 4 static libraries that will be shipped with the product by adjusting the Android.mk the following way:
LOCAL_PATH := $(call my-dir)
MY_PATH := $(LOCAL_PATH)
LOCAL_PATH := $(MY_SDK)/lib/Android/armeabi-v7aD
include $(CLEAR_VARS)
LOCAL_MODULE := A
LOCAL_SRC_FILES := libA.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := B
LOCAL_SRC_FILES := libB.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := C
LOCAL_SRC_FILES := libC.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := D
LOCAL_SRC_FILES := libD.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += $(my_SDK)/include/
LOCAL_PATH := $(MY_PATH)/../../..
LOCAL_CFLAGS += -Wno-format -DANDROID -fno-rtti -fno-exceptions #And many other flags If they are important I will post them later, too
LOCAL_CPPFLAGS += -fno-rtti -fno-exceptions
LOCAL_MODULE := MY_PROJECT
LOCAL_SRC_FILES := Android/Main.cpp # Some other sources..
LOCAL_LDLIBS := -lstdc++ -lEGL -lGLESv2 -llog -Wl,--allow-multiple-definition -Wl,--no-undefined
LOCAL_STATIC_LIBRARIES := A B C D
include $(BUILD_SHARED_LIBRARY)
I also have a Application.mk:
APP_ABI := armeabi-v7a
APP_PLATFORM := android-9
APP_STL := stlport_static
APP_CFLAGS += -fno-rtti -fno-exceptions
Then I added under Debug Configurations a new "Android Native Application" launching the default Activity and using
Debugger "${NdkGdb}"
Command File "${NdkProject}\libs\${NdkCompatAbi}\gdb.setup"
Shared Libraries (Added automatically) ${NdkProject}/obj/local/$NdkCompatAbi}/
The rest is also added automatically and I think should work this way.
One thing that I think is odd is that I get the console Output
[2013-03-08 10:50:36 - Unable to launch cygpath. Is Cygwin on the path?] java.io.IOException: Cannot run program "cygpath": CreateProcess error=2, The system cannot find the file specified
But I can build the project fine this way and later I don't get any warnings trying to attach gdb (I am using Msys, I also have cygwin installed so I don't know why Eclipse is complaining here).
When I launch the application, it starts up on the device and then I get the following Console output([Android Native Application] gdb):
(no debugging symbols found)
Error while mapping shared library sections:
/system/bin/linker: No such file or directory.
Error while mapping shared library sections:
libstdc++.so: No such file or directory.
... And alot more of that type
(no debugging symbols found)
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
warning: shared library handler failed to enable breakpoint
No line 219 in file "jni/../../../MySourceFile.cpp".
I definitely build with NDK_DEBUG=1, my static .a Libraries are build with -g
AndroidManifest.xml has an entry android:debuggable="true".
When I run "sh /ndk-gdb --verbose" in the eclipse project directory (using Msys) I get the following output:
Android NDK installation path: /c/SDK/android-ndk-r7
Using default adb command: /c/SDK/android-sdks/platform-tools/adb.exe
ADB version found: Android Debug Bridge version 1.0.31
Using final ADB command: '/c/SDK/android-sdks/platform-tools/adb.exe'
Using auto-detected project path: .
Found package name: com.x.y
ABIs targetted by application: armeabi-v7a
Device API Level: 13
Device CPU ABIs: armeabi-v7a armeabi
Compatible device ABI: armeabi-v7a
Found debuggable flag: true
ERROR: Non-debuggable application installed on the target device.
Please re-install the debuggable version!
I really want to have native debugging support for this project. So I really appreciate any help.
Is it true that I have to use cygwin? I don't get any reasonable error message for that while trying to attach the debugger.

"undefined reference to" (function) & "in archive is not an object" Android ndk-build

I'll expose my problem quickly. I am trying to port curl on Android and to use it within my app. I built the curl library with the ARM toolchain, configured and made (a couple times to make sure I didn't do nothing wrong the first time).
I then proceeded to put the newly created libcurl.a and my curljni.c into my jni folder, as long as the following Android.mk :
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= libcurl
LOCAL_SRC_FILES := libcurl.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include/curl
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := curljni
LOCAL_SRC_FILES := curljni.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include/curl
LOCAL_STATIC_LIBRARIES := libcurl
include $(BUILD_SHARED_LIBRARY)
I've been trying a lot of things and I'm pretty sure it looks good now, but whenever I try to build with the ndk-build tool I obtain the following :
Note : curljni.c makes calls to functions within the libcurl library and its easy.h and curl.h files. They are then included in top of the file.
I also tried to ask for the whole library to get loaded into my Android app, using LOCAL_WHOLE_STATIC_LIBRARIES instead of LOCAL_STATIC_LIBRARIES, but without much more success :
Previously :
Downloaded curl.7.28.0
Made a standalone toolchain for ARM 4.6
Fixed several files within curl whose linebreaks were DOS like and needed Unix like (bug in configure) - one of which was depcomp, linked to libcurl_la-file.lo
./configure --host=arm-linux-androidaebi --with-zlib --enable-ipv6
make/make install
Added the resulting libcurl.a from curl\lib.libs
Ok,
Your Problem is that your library libcurl.a is not builded with Android ndk gcc ..
You have done :
$ ./configure --host=arm-linux-androidaebi --with-zlib --enable-ipv6
$ make
$ make install
this will generate a library using your PC gcc ..NOt good .
What i do is to configure open source library l for android using line command (or like you have done):
./configure --build=x86_64-unknown-linux-gnu --host=arm-linux-androideabi --target=arm-linux-androideabi
But then you schould not call make and make install ! .
You have to create an android.mk whinch will compile all source file in your libcurl + your jni file ' curljni.c' and put all in one lib : here an example of Android.mk compiling SQLITE3
###################################################
# SQLITE3
###################################################
include $(CLEAR_VARS)
LOCAL_MODULE := Mysqlite3
MY_LOCAL_SQLITE_SRC := $(LOCAL_PATH)/sqlite/
LOCAL_CPPFLAGS := -g
LOCAL_CPPFLAGS += -I $(MY_LOCAL_SQLITE_SRC)
LOCAL_EXPORT_C_INCLUDES:=$(MY_LOCAL_SQLITE_SRC)
FILE_LIST :=$(wildcard $(MY_LOCAL_SQLITE_SRC)*.c*)
LOCAL_SRC_FILES += $(FILE_LIST:$(LOCAL_PATH)/%=%)
# My SQLITE3 JNI FILE
LOCAL_SRC_FILES +=mysqlite_jni.cpp
# include native NDK library liblog and libz
LOCAL_LDLIBS := -llog -lz
include $(BUILD_SHARED_LIBRARY)

Categories

Resources