Friday, 21 August 2009

Android NDK - getting STLPort up and running

If you want to get STLPort running with the Android NDK, you'll have a bit of work to do; especially if using Cygwin under Windows.

First, visit http://umbel.mooo.com/ and get the STLPort version from John Ripley (great job, John!). If you're a cygwin user, you'll first need to remember to install the git package.

Next thing is to copy the files to a place that works for you. On my system, I copied the files into my c:\android-ndk-1.5_r1 folder, and then moved the two hello-stl folders to somewhere that seemed consistent with the other NDK files:


C:\android-ndk-1.5_r1\apps\hello-stl
C:\android-ndk-1.5_r1\sources\samples\hello-stl


I then had to change a few files before I could build the STLPort...

setup.sh

Change the top few lines to look like this:

#!/bin/bash
if uname | grep -qi linux; then
export NDK_HOST=linux-x86
elif uname | grep -qi CYGWIN; then
export NDK_HOST=windows
else

export NDK_HOST=darwin-x86
fi

Change the third line up from the bottom to look like this:


ln -sf "$NDK_DIR/build/prebuilt/${NDK_HOST}/arm-eabi-4.2.1/bin/arm-eabi-$tool" arm-linux-$tool


stlport/build/lib/android.mak

Change the TOOLCHAIN_PREFIX line to look like this:


TOOLCHAIN_PREFIX := $(NDK_DIR)/build/prebuilt/$(NDK_HOST)/arm-eabi-4.2.1/bin/arm-eabi-


sources/samples/hello-stl/Android.mk

Change the STLPORT_BASE line to be this:


STLPORT_BASE := /cygdrive/c/android-ndk-1.5_r1/stlport


Change this:


LOCAL_LDLIBS += -L$(STLPORT_BASE)/build/lib/obj/gcc/so \
-lstlport


to this:


LOCAL_STATIC_LIBRARIES := libstlport.5.1





Building STLPort

Now that the files are merged-in, you're ready to build STLPort!

Run the following in the cygwin shell... it will build the STLPort static library for you! You'll only need to do this once.


export NDK_DIR=/cygdrive/c/android-ndk-1.5-r1
cd $NDK_DIR
./env.sh
./setup.sh


Now, do this to copy the file to your project:


cp -p \
/cygdrive/c/android-ndk-1.5_r1\stlport\build\lib\obj\gcc\ar\libstlport.5.1.a \
/cygdrive/c/android-ndk-1.5_r1\out\apps\hello-stl\android-1.5-arm





Building your project's shared library

You can now go back to the cygwin shell, and do this:


cd /cygdrive/c/android-ndk-1.5_r1
make APP=hello-stl


... which will build your shared library file:

C:\android-ndk-1.5_r1\apps\hello-stl\project\libs\armeabi\libhello-stl.so


Note that this will have the static STLPort library linked-in.

Building the hello-stl project

From Eclipse, use the New Project Wizard to create a new Android project for each sample, using the "Import from Existing Source" option and import the source from c:\android-ndk-1.5_r1\apps\hello-stl\project

You can now build and run your project. The string displayed comes from the shared library, using std::string ...!

8 comments:

Unknown said...

hi,
I link here from google group.
I'm trying to build STLPort using Cygwin under Windows, but got some problems.

I fellow the step you list but after I type

export NDK_DIR=/cygdrive/c/android-ndk-1.5_r1
cd $NDK_DIR
./env.sh
./setup.sh

i got the error


Calling STLport rebuild
scripts/build-stlport.sh: line 4: /scripts/env-utils.sh: No

such file or directory
make: *** /stlport/build/lib: No such file or directory.

Stop.
Could not clean STLport tree
Could not rebuild STLport tree

**
then I type " export

NDK_WRAPPERS_BASE=/cygdrive/c/android-ndk-1.5_r1" and try

"./setup.sh" again.
and it show

Calling STLport rebuild
/bin/sh: arm-linux-c++: command not found
make: arm-linux-c++: Command not found
make: Entering directory `/cygdrive/c/android-ndk-

1.5_r1/stlport/build/lib'
make: arm-linux-c++: Command not found
.....

finally it "Making symlinks to NDK from /bin"

but I can not find the filelibstlport.5.1.a
in the directory .
did I miss anything?

thank you very much.

Pete Cole said...

Sounds to me from this "/bin/sh: arm-linux-c++: command not found" as though you might not have installed the Gnu C++ components in Cygwin...? In which case, you'd simply need to double-check you have all the appropriate components from Cygwin installed, and then try again...

HTH! Pete

Unknown said...

I'm sure I installed all the Gnu C++ components, but still have the
msg"make: arm-linux-c++: Command not found". After, I install
cygwin-arm-cross-2.95.3.tar.tar,the msg is disapear but still have no
libstlport.5.1.a. I find libstlport.a in
C:\android-ndk-1.5_r1\stlport\build\lib\obj\arm-linux-gcc\so, and I change
the file name to libstlport.5.1.a then it work.
Thanks for your help.

Pete Cole said...

I now wish that I'd remembered to document all the "extras" I'd had to install in Cygwin to get this working. :)

Anyways, great to hear that you now have STLPort working. It was quite a lot of work to build, but is incredibly useful!

I'd guess that the .a file renaming might be due to John having updated his files slightly. If you find any more changes, it'd be great if you log them in this blog thread!

Pete

andrew said...

I don't have the arm-linux-c++ cross compiler. Did you build this yourself from the gcc source? If not, where can I get this file I see mentioned here: cygwin-arm-cross-2.95.3.tar?
Thanks!

andrew said...

Okay, I see now that I obviously have the arm-linux-c++ (it's part of the Android NDK - Yes I am a moron). I did find that I needed to source the ./env.sh in order for the environment variables to be exported correctly. So, instead of:
./env.sh
I did:
. ./env.sh

Now I'm having a problem where when I try to build STLport, I get numerous errors complaining of missing include files:

In file included
from ../../stlport/ctime:27,
from ../../src/stlport_prefix.h:32,
from ../../src/dll_main.cpp:29:
../../stlport/stl/_ctime.h:24:43: error: ../include/time.h: No such file or directory

And several more files are apparently missing: math.h, stdlib.h, and more.

Has anyone else gotten this problem?

andrew said...

Okay, as it turned out, I am using the Android NDK v1.6, whereas the script is setup for the Android NDK v1.5. The v1.5 has a folder $NDK_DIR/build/platforms/android-1.5, but in v1.6 that has been changed to $NDK_DIR/build/platforms/android-4. Changing occurrences of android-1.5 to android-4 in the scripts seemed to fix the missing include errors. There were only two files that needed to be changed:

$NDK_DIR/stlport/build/lib/android.mak
--sysroot=$(NDK_DIR)/build/platforms/android-1.5/arch-arm \
changed to:
--sysroot=$(NDK_DIR)/build/platforms/android-4/arch-arm \

$NDK_DIR/env-utils.sh
SYSROOT="${NDK_DIR}/build/platforms/android-1.5/arch-arm"
changed to:
SYSROOT="${NDK_DIR}/build/platforms/android-4/arch-arm"

Pete Cole said...

Aha - thanks for the feedback, Andrew!

Pete