I rooted my android phone and installed termux and clang.
When I tried to compile this code:
#include <android/sensor.h>
int main(){
ASensorManager* sensorManager = ASensorManager_getInstance();
}
to access sensors in my phone it gives a bunch of errors like:
undefined reference to 'llvm::Type::getIntegerBitWidth
undefined reference to 'llvm::APFloat::IEEEHalfe
I compiled it on termux using clang:
clang++ main.cpp -o main -landroid
but I cant find the library Im supposed to link with or if this even works at all.
Related
I am trying to link my c-program to a shared object library (libfoo.so) using the ARM cross-compiler arm-linux-gnueabi-gcc. I am compiling on a Ubuntu system, and I want to run the program on an Android device. The compiling works, but when I try to run the program on my android device I get an error.
I've created a simple test program containing the following files:
foo.c:
#include <stdio.h>
void foo(void){ puts("Hello, I am a shared library"); }
foo.h:
#ifndef foo_h__
#define foo_h__
extern void foo(void);
#endif
main.c:
#include <stdio.h>
#include "foo.h"
int main(void)
{
puts("This is a shared library test...");
foo();
return 0;
}
I have then created the shared object library using:
arm-linux-gnueabi-gcc -c -fPIC foo.c
arm-linux-gnueabi-gcc -shared -o libfoo.so foo.o
I then compile my program using:
arm-linux-gnueabi-gcc -L/home/foo -o test main.c -lfoo
After uploading the test-program to the Android device using adb, I am not able to run it. Instead I get the error: /system/bin/sh: ./test: No such file or directory
I am in the right directory and the test-file is present, so I assume that it is the shared library that cannot be found. I've tried uploading libfoo.so to the android device as well (to the same path as specified when compiling), but it still doesn't work to run the program.
I've gotten it to work with a static library (foo.o) using arm-linux-gnueabi-gcc -static -o test main.c foo.o, but not with a shared library.
How do I properly link a shared library when cross-compiling, to make sure that the program can then run on an Android device?
The linker isn't able to dynamically link to the library.
Define LD_LIBRARY_PATH environment variable to include the library:
export LD_LIBRARY_PATH=/home/foo
I try to build a cpp file as below to an executable on the Android platform. Therefore, by calling dumping_callstack(), I can get call stack of my executable in run time. But there are some errors。
cpp file:mycallstack.cpp
#include <utils/CallStack.h>
extern "C" void dumping_callstack()
{
CallStack stack("haha");
}
mycallstack.h
void dumping_callstack();
test.c
#include <mycallstack.h>
main()
{
dumping_callstack();
}
android.mk
LOCAL_SRC_FILES += mycallstack.cpp
LOCAL_SHARED_LIBRARIES := libc libcutils liblog libutils
then compile.
error: undefined reference to 'android::CallStack::CallStack(char const*,int)'
error: undefined reference to 'android::CallStack::~CallStack()'
In android 9.0, you should use libutilscallstack.
Look "android/system/core/libutils/Android.bp" for more details.
The implementation of CallStack::CallStack and CallStack::~CallStack isn't provided to the compiler/linker.
You might forgot to link it to the corresponding object file/library, I suggest you to read the documentation, there might be some information about linking. Sometimes it can help to just compile it with the -static switch, this makes the executable almost standalone, some libraries even require to be linked staticly.
It might also be the case, that the implementation is not available for release builds, I think the CallStack-class could only be available for debugging.
Trying to build my Qt application for android linking to FFmpeg.
I get the following linker errors on android.
libavformat/hls.c:783: error: undefined reference to 'atof'
libavcodec/ffv1enc.c:476: error: undefined reference to 'log2'
libavcodec/imc.c:472: error: undefined reference to 'log2f'
Following is the list of libraries I trying to link for FFmpeg.
-lavformat -lavcodec -lswscale -lavutil -lavfilter -lswresample -lavdevice -lpostproc -lm -lgnustl_static
What am I missing in the linker options?
I have also set the following to :
CONFIG += c++14
QMAKE_CXXFLAGS_RELEASE += -std=c++1y
QMAKE_CXXFLAGS_DEBUG += -std=c++1y
This is strictly not related to Qt and happens only because those functions are not defined in Android, as discussed in Does Android support log2 and Android ndk can't find atof function, among other places.
You either have to define those functions before including FFmpeg headers or find a library that does this for you.
I've two files:
lib.c
#include<stdio.h>
void hi() {
printf("Hi i'm a library function in lib.so\n");
}
and main.c
#include<stdio.h>
#include<dlfcn.h>
/* based on Jeff Scudder's code */
int main() {
void *SharedObjectFile;
void (*hi)();
// Load the shared libary;
SharedObjectFile = dlopen("./lib.so", RTLD_LAZY);
// Obtain the address of a function in the shared library.
ciao = dlsym(SharedObjectFile, "hi");
// Use the dynamically loaded function.
(*hi)();
dlclose(SharedObjectFile);
}
And I've tried to build an executables using the following commands:
export LD_LIBRARY_PATH=pwd
gcc -c -fpic lib.c
gcc -shared -lc -o lib.so lib.o
gcc main.c -ldl
And it works pretty well.
Then I've tried to export my program on Android (Nexus One, with ARM-v7-0a arch) using the following commands:
export LD_LIBRARY_PATH=pwd
arm-none-linux-gnueabi-gcc -c -fpic lib.c
arm-none-linux-gnueabi-gcc -shared -lc -o lib.so lib.o
arm-none-linux-gnueabi-gcc main.c -ldl -o main
adb push main /system/app
The result of executing ./main on the correct folder on my smartphone is just:
./main: not found
even if my file is right there!
Am I missing anything during the cross-compile process? Any help?
I'm using the cross-compiler from CodeSourcery and it works well for static programs without .so libraries.
Thanks
EDIT: as Igor states below, that was a linker issue. This command fixes it:
arm-none-linux-gnueabi-gcc -o test main.c -Wl,--dynamic-linker=/system/bin/linker -ldl
in my very case I need other libraries because in /system/lib/ there are no many .so files.
The "not found" message refers not to the shared object but to the dynamic linker. Linux uses /lib/ld-linux.so.2 (or /lib64/ld-linux-x86-64.so.2 for x64) while Android uses /bin/linker. You can check which dynamic loader your program uses with readelf -l, e.g.:
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000034 0x08048034 0x08048034 0x00100 0x00100 R E 0x4
INTERP 0x000134 0x08048134 0x08048134 0x00013 0x00013 R 0x1
[Requesting program interpreter: /lib/ld-linux.so.2]
You can specify a linker to use with ld's --dynamic-linker switch, but there are likely to be other differences. For example, Android uses a stripped-down libc implementation called bionic, and it may be missing functionality that your program relies on, or have different behavior.
You should use NDK or another Android-targeted toolchain when compiling programs for Android. Even though it's based on Linux kernel, the differences are large enough that Linux-targeted toolchains are not sufficient.
I am trying to build my code for h.264 video decoding using hardware decoder(OMX codec) in native code of android 4.0.4 by keeping it in the android source tree. The android source is already built. I am using mm command to build my decoder module.
But when I try to build it I am getting the following error
prebuilt/linux-x86/toolchain/arm-linux-androideabi-4.4.x/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld:
out/target/product/generic/obj/SHARED_LIBRARIES/custom_decoder_intermediates/custom_decoder.o:
in function custom_decoder::decode_video():frameworks/base/include/utils/RefBase.h:171:
error: undefined reference to 'android_atomic_dec'
when I explored about it, came to know that android_atomic_dec is defined in cutils/atomic.h
which is in system/core/libcutils and the header at system/core/include/cutils of AOSP 4.0.4.
So I have added this also in my android.mk via LOCAL_C_INCLUDES but still to get the same error.
Can someone help me to solve this ??....
You must add cutils to the linkage stage:
LOCAL_LDLIBS += -L $(path-to-system-libs) -lcutils
If you are building AOSP tree, use instead the following:
LOCAL_SHARED_LIBRARIES += libcutils