Android r8b c++ shared library problems - android

This is a weird setup here. I compiled a custom NDK with GCC 4.7 (for Debian armhf chroot host). In testing, it compiles and runs regular c and c++ binaries on Android perfectly. When trying to compile a test shared library with C++ using:
arm-linux-androideabi-g++ -fPIC -shared test.cpp -lstdc++ -o test.so
I get numerous linker errors to _staticinitialization_destruction and _dso_handle being unreferenced.
I have both libstdc++.a and libstdc++.so available in the lib folder under arm-linux-androideabi directory. What is going on? There's hardly any info on Android C++ standalone compiling out there.
Edit
It seems __dso_handle is undefined for the iostream portion of the GNU STL. I found the answer from a 2005 Linux post. Still an issue in 2012 it seems :O lol.

Related

Android NDK - arm-linux-androideabi-g++: not found

I am trying to build the C++ POCO library for an Android target in a fresh Ubuntu that I just installed for that.
I have installed the Android NDK in /home/user/dev/Android/android-ndk-r9-x86 and added the path to the NDK in the environement variables using :
export ANDROID_NDK_ROOT=/home/user/dev/Android/android-ndk-r9-x86
To build the libraries I first move to the root directory of the POCO library, and configure it using :
./configure --omit=NetSSL_OpenSSL,Crypto,Data/ODBC,Data/MySQL --static --config=Android
So that it compiles static .a libraries, doesn't compile the modules I don't want and compiles for an Android target.
But than calling make causes the following error :
user#user-VirtualBox:~/dev/Lib/POCO/poco-1.6.1$ make
make -C /home/user/dev/Lib/POCO/poco-1.6.1/Foundation
make[1]: Entering directory `/home/user/dev/Lib/POCO/poco-1.6.1/Foundation'
** Compiling src/ArchiveStrategy.cpp (debug, static)
arm-linux-androideabi-g++ -Iinclude -I/home/user/dev/Lib/POCO/poco-1.6.1/CppUnit/include -I/home/user/dev/Lib/POCO/poco-1.6.1/CppUnit/WinTestRunner/include -I/home/user/dev/Lib/POCO/poco-1.6.1/Foundation/include -I/home/user/dev/Lib/POCO/poco-1.6.1/XML/include -I/home/user/dev/Lib/POCO/poco-1.6.1/JSON/include -I/home/user/dev/Lib/POCO/poco-1.6.1/Util/include -I/home/user/dev/Lib/POCO/poco-1.6.1/Net/include -mthumb -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions -DPOCO_BUILD_HOST=user-VirtualBox -DPOCO_ANDROID -DPOCO_NO_FPENVIRONMENT -DPOCO_NO_WSTRING -DPOCO_NO_SHAREDMEMORY -g -D_DEBUG -c src/ArchiveStrategy.cpp -o /home/user/dev/Lib/POCO/poco-1.6.1/Foundation/obj/Android/armeabi/debug_static/ArchiveStrategy.o
sh: 1: arm-linux-androideabi-g++: not found
make[1]: *** [/home/user/dev/Lib/POCO/poco-1.6.1/Foundation/obj/Android/armeabi/debug_static/ArchiveStrategy.o] Error 127
make[1]: Leaving directory `/home/user/dev/Lib/POCO/poco-1.6.1/Foundation'
make: *** [Foundation-libexec] Error 2
Make seems unable to find the compiler used for Android, and I have no idea why ? What am I missing ? Did i forget something when "installing" the NDK ?
Thank you.
The error you're getting is caused by a missing toolchain invocation - rather, the arm-linux-androideabi-g++ command/executable/binary was nowhere to be found.
Luckily, we can get around that by installing the Standalone toolchain - that one actually has the exact thing you're missing, a general purpose arm-linux-androideabi cross-compiler instead of some other, a bit more obscure, vendor/platform-specific crosscompiler/toolchain, such as armv7a-marvell-linux-android which is what marvell uses, or arm-linux-android which is what Clang uses. For more info on Clang, look here. I could be wrong though and that Clang actually produces a arm-linux-androideabi toolchain out of the box, but I'm unsure. I know you can use it easily, I'm just unsure if it can be used "straight out of the box" which is what you're looking for. The "rest of the work" is just a few path exports - but still. We're aiming for the laziest solution here.
The standalone toolchain should be quite sufficient for your task, so try using it as much as possible over any other cross-compilation solutions.
However, if you're feeling adventurous - you're free to make your own cross-compiler (or the whole toolchain!) using the crosstool-ng tool. However, try to stick with the Linaro libc branch; personal experience showed me that one somehow works the best and causes the least amount of problems/time wasted.
Also, make sure you download the right one for your architecture (arch) and OS, 32bit vs 64bit matters here as well. After a lengthy discussion, we realized it was a "32bit vs 64bit" problem.
Here's a link to read more about it.

Compiling against C++ standard libraries on the android toolchain

I have a really simple helloworld.cpp program
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
And I'm trying to compile it for android x86 with the cross-compiler from the toolchain:
/Users/me/android-ndk-r8/toolchains/x86-4.4.3/prebuilt/darwin-x86/bin/i686-android-linux-g++ helloworld.cpp -L "/Users/me/android-ndk-r8/sources/cxx-stl/stlport/libs/x86/" -lstlport_static
However, I get errors:
helloworld.cpp:2:20: error: iostream: No such file or directory
Any idea why?
Check the documentation.html file included with the NDK, under "Standalone Toolchain". It says that if you invoke the compiler in this way you won't be able to "use any C++ STL". However it is possible, as the documentation explains, if you first create a "customized" toolchain installation, using something like the following command:
$NDK/build/tools/make-standalone-toolchain.sh --platform=android-8 --install-dir=/tmp/my-android-toolchain --arch=x86
where $NDK is the path to your NDK directory. Note the --arch=x86 which means that the toolchain is prepared specifically for the x86 Android. This prepares what you need in one directory, including the STL headers and folders. You should then be able to use -lstdc++ to link against the STL (static version), i.e. something like:
/tmp/my-android-toolchain/bin/i686-android-linux-g++ helloworld.cpp -lstdc++
For a more complete explanation, please see the NDK documentation.
The NDK documentation is not entirely accurate, at least not currently. In fact, it states when using the prebuilt toolchain "You won't be able to use any C++ STL (either STLport or the GNU libstdc++) with it.", but this is out of date. I created a small hello world program using the include with the same error. It can be solved without creating your own toolchain though, which is nice if you don't want to have to add one more step to your configuration process and allows you to always use the latest SDK platform without creating a new toolchain every time.
The NDK ships with the source code for several versions of standard C++ libraries: GAbi++, STLport, and GNU STL. Each flavor comes with prebuilt shared and static libs as well. My example below will use stlport.
To use the stand-alone toolchain at its installed location, you can do something like this:
export CXX='$NDK_ROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++ --sysroot="$NDK_ROOT/platforms/android-19/arch-arm"'
This, for example, would set your CXX compiler to compile ARM on the OS X system using SDK platform level 19. This much you probably already knew. Also, you would want to export your CC, CPP, LD, AR, and RANLIB if you use it. I also personally create an envar for READELF.
To add support for C++ libs, you could do something like follows:
$CXX helloworld.cpp -I$NDK_ROOT/sources/cxx-stl/stlport/stlport -L$NDK_ROOT/sources/cxx-stl/stlport/libs/armeabi -lstlport_shared
Note that this will link the libstlport_shared.so which will now be required at runtime, so you may need to add a relative path to the command above to support that, depending on your APK structure. If you want to just test this simple case, you can just run this on the emulator as follows:
adb push a.out /data
adb push $NDK_ROOT/sources/cxx-stl/stlport/libs/armeabi/libstlport_shared.so /data
adb shell
# su
# cd /data
# chmod 777 a.out
# ./a.out
To get rid of the headache of dealing with shared library paths, you can also statically link the C++ library in by changing "-lstlport_shared" to "-lstlport_static". There are some consequences of doing this, as explained in the NDK Dev Guide. The biggest issue is due to having the static library linked in multiple places, causing:
- memory allocated in one library, and freed in the other would leak or even corrupt the heap.
- exceptions raised in libfoo.so cannot be caught in libbar.so (and may simply crash the program).
- the buffering of std::cout not working properly
A useful tool is also included to see what dependencies your program has, the readelf tool.
To see what other shared libraries your program requires, you can run the following:
$NDK_ROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-readelf -a a.out | grep NEEDED
Other cool standard tools:
addr2line - convert a stack trace address to a line of code
nm - Displays the symbol table
objdump - displays info on an object
i call one of the functions from gnustl after it runs fine from prebuilt aosp arm-linux-androideabi-gcc --std=c++11 and after crashing error i cant get a backtrace from logs or reason, my hope is turning to crossbuilt qemu-linux-user to run host compiled i686 binary on the arm, difficulty is interacting with crosshost libs aapt from adt always crashes on any other than platform specific libs, unless kernel module packaged update is possible...

How can I get JHC to cooperate with the android ndk?

JHC is a Haskell compiler which portable generates C code and then invokes a compiler backend to make an executable.
I need to dump the header files and libraries for JHC's runtime system, so the android ndk can use it to compile the generated C code.
Alternatively, I need to figure out how to link the android API into JHC.
Basically, how can I plug JHC into the android ndk backend?
Edit: I've made a standalone compiler using one of the ndk's build tools that JHC can use, "build/tools/make-standalone-toolchain.sh" . Now I need to figure out a way for JHC to compile my program into a shared object library, and how to get JHC to not demand a "main" function.
Edit2: I have the following targets.ini and compile script. Strangely the shared library fails to load.
[android]
cc=arm-linux-androideabi-gcc
cflags+= -shared -fPIC -rdynamic -Wno-all
gc=static
executable_extension=.so
merge=mle32
and
#!/bin/sh
jhc --cross -mandroid \
--main=Main.android_main \
hs/Main.hs -o libs/armeabi/libnative-activity &&
ant debug &&
ant debug install
I figured out the problem.
I didn't put the function call "app_dummy()" at the start of my "android_main" function.

how to compile static linked application using android cross compilation toolchain?

I am trying to compile fbgrab fbgrab website for arm devices.
I managed to build it using this command
arm-linux-androideabi-gcc --sysroot=$NDK_ROOT/platforms/android-8/arch-arm/ -lz -lm -I../zlib -I../lpng -W -Wall -o fbgrab fbgrab.c ../zlib/libz.a ../lpng/libpng.a
but it is dynamicly linked and i dont now how to install shared libraries on device
localhost:fbgrab-1.0 anatoly$ file fbgrab
fbgrab: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
So i need to build it with static linked libraries
when i try to use -static option compilation fails with this message ld: cannot find -lz although shared libraries are located in pointed directories
what is the problem here? how to build static linked binary? or how am i able to install shared libraries on to device?
You can easily install shared libraries, by either including their sources in your project and building them using the NDK tools, or by including a prebuilt shared lib. One can also use the NDK to do static linking as well. Have a look at these docs in your NDK directory:
/android-ndk-r7/docs/PREBUILTS.html
/android-ndk-r7/docs/ANDROID-MK.html
Hope that helps!

migrating a linux driver to Android

I'm porting a 3G modem written for Linux for laptops to an embedded android (Gingerbread) device with an ARM processor. I already got the device driver compiled (C code) as a module (.ko file) and installed. I see it when I boot the kernel and it runs well. It hooks up to the USB port as intended. It's happy ready to be talked to.
The next required piece is the "connection manager" written in C++. This is where I have a problem. This doesn't run in Kernel space but it is not a regular Android application with a user interface. This is a "task" running in background that should be started from the "init.rc" file at boot time. The makefile provided with the source code is good to set the dependencies but it is useless as far as the platform I want to target. I'm using the toolchain provided with the Android source code "arm-eabi-*" (runs off of a Ubuntu machine) that I used to compile Android and the kernel. I got lots of compile errors primary because it uses the standard "libc" libraries which doesn't exist in Android. I replaced it by the "bionic libc" which is a light weight subset version of linux libc for android. On top of it, it looks for "crt0.o" which is the start-up code a statically linked program in a linux environment(and several other OS). In Android it is dynamically linked at run time, therefor uses something else than crt0.o.
There is tons of information on Android app programming on the web but, very little on that kind of low level stuff. If anybody has a working makefile for building that kind of C++ code to run as a background task under Android ARM, I would very appreciate to have a look at it or if there is any information that could help me find a way to do that. Or if anyone has done something like that could give me some clues on how to achieve this.
Almost a year later, but here is a makefile that will compile a simple native app:
NDK_USR_PATH := $(NDK_USR)
C_FILES := $(wildcard *.c) $(wildcard *.cpp)
O_FILES := $(patsubst %.cpp,%.o,$(C_FILES))
O_FILES := $(patsubst %.c,%.o,$(C_FILES))
out: $(O_FILES)
#arm-eabi-gcc -o $# $< -Bdynamic -Wl,--gc-section -Wl,-z,nocopyreloc -Wl,--no-undefined -Wl,--dynamic-linker=/system/bin/linker -Wl,-rpath-link=$(NDK_USR_PATH)/lib -nostdlib $(NDK_USR_PATH)/lib/crtend_android.o $(NDK_USR_PATH)/lib/crtbegin_dynamic.o -L$(NDK_USR_PATH)/lib -lc
%.o: %.c
#arm-eabi-gcc -o $# $< -c -I$(NDK_USR_PATH)/include -fno-short-enums
clean:
#rm -f *.o
#rm -f out
It compiles any .c files in the same directory into an app named "out". It requires the environment variable NDK_USR to point to the ndk directory "ndk/android-ndk-r7/platforms/android-14/arch-arm/usr/".
This should dynamically link to the bionic libc, and should enable android driver development.
Be careful when copying and pasting the above makefile. Make is very specific about tab characters.

Categories

Resources