Background
OSX is OS
R8 NDK
I am trying to compile the following class using the Android GCC compiler...
#include <stdint.h>
int main (void){
return 0;
}
I do the with the following command...
un#un:~/Development/Code/OpenGL$ ~/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/arm-linux-androideabi-gcc hello.c -o hello
I get...
In file included from hello.c:1:0:
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/include/stdint.h:3:26: fatal error: stdint.h: No such file or directory
compilation terminated.
So due to a lack of gcc knowledge (but some Google ability) I find this and try it...
un#un:~/Development/Code/OpenGL$ ~/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/arm-linux-androideabi-gcc hello.c -o hello -ffreestanding
and I get...
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtbegin_dynamic.o: No such file or directory
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtend_android.o: No such file or directory
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lc
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -ldl
collect2: ld returned 1 exit status
Can someone help me with what I am doing wrong? Am I missing a link or something? Android.mk is not an option.
UPDATE this isn't working either...
arm-linux-androideabi-gcc hello.c --sysroot=~/Development/Android/android-ndk-r8c/platforms/android-9/arch-arm
/Users/jackiegleason/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtbegin_dynamic.o: No such file or directory
/Users/jackiegleason/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtend_android.o: No such file or directory
/Users/jackiegleason/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lc
/Users/jackiegleason/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -ldl
collect2: ld returned 1 exit status
You must tell GCC where to find the Android system files and headers. Either use:
ndk-build and an Android.mk with BUILD_EXECUTABLE
or, the --sysroot GCC option
[1]
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := foo.c
include $(BUILD_EXECUTABLE)
[2]
# Change `android-9` with the level you target
/path/to/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt\
/darwin-x86/bin/arm-linux-androideabi-gcc\
--sysroot /path/to/android-ndk-r8c/platforms/android-9/arch-arm/\
foo.c -o foo
# Or generate a ready-to-use standalone toolchain (better)
/path/to/android-ndk-r8c/build/tools/make-standalone-toolchain.sh \
--platform=android-9 \
--install-dir=/tmp/my-android-toolchain
export SYSROOT=/tmp/my-android-toolchain/sysroot
/path/to/arm-linux-androideabi-gcc --sysroot $SYSROOT foo.c -o foo
So, since I don't want to use Android.mk file, I went ahead and created a standalone toolchain. this is done using the following...
/Users/un/Downloads/android-ndk-r8d/build/tools/make-standalone-toolchain.sh --platform=android-9 --install-dir=/tmp/my-toolchain
/tmp/my-toolchain/bin/arm-linux-androideabi-gcc hello.c
I would like to know what the "alternative" is in terms of the gcc linking I could do.
This answer adds a bit more details to #deltheil's answer. I had similar issues as I was trying to compile I2C-tools for debugging I2C bus on Android. Somehow after struggling for more than a day with make files and trying different options including --sysroot & --dynamic-linker options etc., I finally tried to compile it within the Android AOSP tree. I used the Google Nexus-S AOSP to build a binary that I intended to run on Samsung S3 phone. I created a folder called i2c-tools for the sources inside the AOSP/external folder and copied Android.mk, Cleanspec.mk & MODULE_LICENCE from another executable folder (ping) and modified it for i2c-tools as follows:
ifneq ($(TARGET_SIMULATOR), true)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := i2cdetect.c i2cbusses.c
LOCAL_C_INCLUDES := $(KERNEL_HEADERS)
LOCAL_MODULE := i2cdetect
LOCAL_MODULE_TAGS := tests
LOCAL_SHARED_LIBRARIES := libc
include $(BUILD_EXECUTABLE)
endif
Then I just ran:
source build/envsetup.sh
make i2cdetect
from the AOSP base folder and voila, I had a working executable in out/target/product/generic/system/bin/ folder. Note that I had to copy all needed source and header files from the original (i2c-tools)/tools & include folders and had to modify some of the #include to remove the extra path for header files that were now in the same place as the c-source.
In my case, I needed an .o file and did not need to define main().
I had to specify the -c switch:
~/ax/arm-linux-androideabi-g++ --sysroot=~/an/platforms/android-8/arch-arm/ -c dummy.c
where ~/ax and ~/an are links:
ax -> ~/android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin/
an -> ~/android-ndk-r9d/
did dummy.o.
i solved the problem ,
the default compress file manager in ubuntu wasn't extracting symbolic links ,
so i tried : tar jxf filename.tar.bz2
to untar the ndk.tar.bz2 and now it works fine
Related
This is a most vexing issue. When I run my makefile I see two sysroot in the output:
ubuntu#ubuntu:~/Desktop/LatestAndroidstuff/CmE_source_DAL_1.7_3564/CmCompact$ make
platform/wince/WibuCmHID/rules.mak:31: WibuCmHID only for Windows CE
/home/ubuntu/my-android-toolchain/bin//arm-linux-androideabi-gcc -I./ -I./external/hidapi/include -Os -std=c99 -Wall -Wextra -fno-short-enums -fno-strict-aliasing --sysroot=/home/ubuntu/my-android-toolchain/sysroot --sysroot=/home/ubuntu/android-ndk-r13b/platforms/android-14/arch-arm -o obj/release/android/armeabi-v7a/static/w_refcount.o -c base/w_refcount.c
In file included from base/w_refcount.c:29:0:
./base/w_define.h:31:19: fatal error: stdio.h: No such file or directory
#include <stdio.h>
^
compilation terminated.
rules.mak:54: recipe for target 'obj/release/android/armeabi-v7a/static/w_refcount.o' failed
make: *** [obj/release/android/armeabi-v7a/static/w_refcount.o] Error 1
If I edit the failing command and remove the extra sysroot (the second one), it works.
/home/ubuntu/my-android-toolchain/bin//arm-linux-androideabi-gcc -I./ -I./external/hidapi/include -Os -std=c99 -Wall -Wextra -fno-short-enums -fno-strict-aliasing --sysroot=/home/ubuntu/my-android-toolchain/sysroot --sysroot=/home/ubuntu/android-ndk-r13b/platforms/android-14/arch-arm -o obj/release/android/armeabi-v7a/static/w_refcount.o -c base/w_refcount.c
Problem is, I have looked all over the makefile. The makefile is simple enough:
WBSMAK_ANDROID_NDK_HOME=/home/ubuntu/android-ndk-r13b
SYSROOT=/home/ubuntu/my-android-toolchain/sysroot
ANDROID_TOOLCHAIN_DIR=/home/ubuntu/my-android-toolchain/bin/
WBSMAK_ANDROID_CC=${ANDROID_TOOLCHAIN_DIR}/arm-linux-androideabi-gcc
WBSMAK_ANDROID_LD=${ANDROID_TOOLCHAIN_DIR}/arm-linux-androideabi-ld
WBSMAK_ANDROID_AR=${ANDROID_TOOLCHAIN_DIR}/arm-linux-androideabi-ar
WBSMAK_ANDROID_RANLIB=${ANDROID_TOOLCHAIN_DIR}/arm-linux-androideabi-ranlib
SHELL := /bin/bash
AR := WBSMAK_ANDROID_AR
CC ?= WBSMAK_ANDROID_CC
LD ?= WBSMAK_ANDROID_LD
RANLIB := WBSMAK_ANDROID_RANLIB
In order to make the file pick up sysroot:
CFLAGS_ARCH := --sysroot=${SYSROOT}
But there is no mention at all in the makefile about that other second sysroot nor that path that shows up. Also, I looked and made sure it's not picking up an environment variable.
ubuntu#ubuntu:~/my-android-toolchain$ printenv | grep SYSROOT
ANDROID_SYSROOT=
LINARO_SYSROOT=/home/ubuntu/linaro-toolchain-4.6/arm-unknown-linux-gnueabi/sysroot
SYSROOT=/home/ubuntu/my-android-toolchain/
The environment sysroot it pointed to where I want it.
If I move the Android NDK folder and all its stuff out of the path, that is nowhere in the makefile, then I get the error. If I have the folder where the extra sysroot is, then the library compiles and links, but it crashes on the Android device, which is Android 21, not 14. I don't know why it's always Android 14. Could I be missing some setting somewhere? Perhaps when I created the standalone tools using make-standalone-toolchain.sh something hardcoded went in so the calls to /home/ubuntu/my-android-toolchain/bin/arm-linux-androideabi-gcc always searches for the android-14 path?
Very vexing problem. I have been all over the makefile. I don't know why the gcc keeps picking up that android 14 sysroot. Could I be only providing for the compilation but the linking is pulling up a default sysroot?
Alright the issue appears to have been a carefully sidelines .mak file in a subfolder that was looking for a string in a string of an existing field and on finding that, was using ANDROID_ARCH for the --sysroot in the actual gcc call. But to the credit of who wrote it, it did have the other -march setting needed for proper compile and linking of the android native module.
Moral of the story: always surf around the folders and take a glance at everything.
(of course my app still crashes but that's another matter to find that out... )
building .so file, I receive this link error.
"C:/android-ndk-r10d/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -landroid"
"Android.mk" file includes this option.
LOCAL_LDLIBS += -llog -ldl -lz -landroid
And "Application.mk" file includes this options.
APP_ABI := armeabis
APP_ABI += x86
when I set the "APP_PLATFORM=android-xx" option in "Application.mk" file, I can build .so file successfully for both x86 and armeabi.
OR
when I remove "APP_ABI := armeabi" option in Application.mk file without "APP_PLATFORM=android-xx" option, I can also build only .so file for x86.
In my desktop, there is libandroid.so file in ndk\platform\android-xx\arm\usr\lib directory...
Why does this error occur?
If not set, APP_PLATFORM defaults to the lowest supported platform version for that NDK. For r10d, this was android-3 (cupcake).
libandroid wasn't available until android-9 (Gingerbread): https://developer.android.com/ndk/guides/stable_apis.html
I've got an issue building my C++ code using NDK r9d, since I try to compile C files using C++ compiler (G++) I've got this warnings :
C:/Android/ndk/build/core/build-binary.mk:393: warning: overriding commands for target
C:/Android/ndk/build/core/build-binary.mk:391: warning: ignoring old commands for target
Before I didn't need to compile with C++ 11 and my C files was compiled with GCC, I had no problems, but since I had LOCAL_CPP_EXTENSION := .cpp .c, this warnings appears (only for C files).
I found that someone else had the same problem (Overriding commands for target Android Makefile) but didn't get any answer.
Here is my files :
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
MY_INC_PATH := ../../..
LOCAL_MODULE := test
LOCAL_CFLAGS := -Werror
LOCAL_CPPFLAGS := -std=c++11
LOCAL_LDLIBS := -ldl -llog -lGLESv1_CM
LOCAL_C_INCLUDES := \
$(MY_INC_PATH)
MY_SRC_PATH := ../../../..
LOCAL_CPP_EXTENSION := .cpp .c
LOCAL_SRC_FILES := \
$(MY_SRC_PATH)/XXX.c \
$(MY_SRC_PATH)/YYY.cpp \
$(MY_SRC_PATH)/ZZZ.cpp
include $(BUILD_SHARED_LIBRARY)
I use NDK r9d and compile with G++ 4.8 and C++ 11 activated. Thanks for your help.
As of r9d, NDK does not provide methods to unassociate .c files from C compiler. You can redefine $$(TARGET_CC), and also set LOCAL_CFLAGS += -std=c++11, and not set LOCAL_CPP_EXTENSION to include .c, but that would be a hack, anyways. So if you cannot rename the files, and do not want to hack your NDK, the cleanest solution would be to simply ignore the warning.
Now I have a so file which developed by DNK. I want to call the so file use C in Linux. But it always prompts:
[root#PCGiter Code]# gcc SoTest2.c -o SoTest2.exe -ldl
[root#PCGiter Code]# ./SoTest2.exe
Open Error:libcom_wuba_aes_ExecV3_1_0.so: cannot open shared object file: No such file or directory.
This answer is for creating the executables for Android and executing them in shell like in Linux, but not how to execute Android executables in Linux.
Use Android-ndk for building the source files, then you can copy directly to emulator and execute in the adb shell.
Example of the make file for creating an executable for android
# For building the Test executable
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Linker flags
LOCAL_LDLIBS += -llog
LOCAL_LDLIBS += -lOpenSLES
LOCAL_LDLIBS += -landroid
# Include paths
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)
# Local C Flags if any
LOCAL_CFLAGS :=
# Source Files to compile
LOCAL_SRC_FILES := \
# Shared libraries to be used while linking
LOCAL_SHARED_LIBRARIES :=
# Local module name
LOCAL_MODULE :=
include $(BUILD_EXECUTABLE)
You can get in to the emulator shell by launching the emulator and then executing the command in command prompt "adb shell" .
Usually what I observed is when copied to /data/ folder only, I was able to execute. Other folders such as /mnt/sdcard I was not able to execute the executable.
I am doing NDK profiling for my project using android-ndk-profiler-3.1. I have made changes in Android.mk as follows...
LOCAL_PATH := $(call my-dir)
-include android-ndk-profiler.mk
include $(CLEAR_VARS)
# Module name -------------------------------------------------------
LOCAL_CFLAGS += -O3
TARGET_ARCH_ABI :=armeabi
LOCAL_CFLAGS := -pg
LOCAL_STATIC_LIBRARIES := andprof
LOCAL_LDLIBS += -llog
LOCAL_MODULE := libitv
include $(BUILD_SHARED_LIBRARY)
Application.mk is as follows...
APP_ABI := armeabi
APP_PLATFORM := android-10
I have called monstartup("itv.so"); function in the beginning of the native code and moncleanup(); function in the stop method. And gmon.out file is created successfully.And then I have pasted gmon.out in
D:\android\android-ndk-r6-windows\android-ndk-r6\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\bin directory.
But when I am trying to read gmon.out using the following command...
D:\android\android-ndk-r6-windows\android-ndk-r6\toolchains\arm-linux-androideab
i-4.4.3\prebuilt\windows\bin>arm-linux-androideabi-gprof D:\InternetTV_FD_Canvas
\libs\armeabi\libitv.so > out.txt
This Error is showing...
arm-linux-androideabi-gprof: file `D:\InternetTV_FD_Canvas\libs\armeabi\libitv.so'
has no symbols
I am not able to make out why this error is coming even I have done everything fine.
Can anybody please help me.
Any help will be appreciated.
Thanks in Advance.
The NDK build process creates 2 libraries, one with symbols and one stripped without. You install the stripped, symbol-less library in your APK, but you need to use the unstripped version with gprof. If you run:
arm-linux-androideabi-gprof D:\InternetTV_FD_Canvas\obj\local\armeabi\libitv.so
... then that should be the correct library.