Saturday, October 24, 2009

Android NDK add stlport and compile C++ program

(1) Assume you have Android ndk 1.6 and your ndk directory is in ~/Android/android-ndk-1.6_r1 and you are using mac
download ndk-wrapper from

cd ~/Android
git clone git://umbel.mooo.com/ndk-wrappers.git


reference
http://umbel.mooo.com/git?p=ndk-wrappers.git;a=summary

(2) Edit ~/Android/ndk-wrappers/setup.sh and add these

export HOST=darwin-x86
export NDK_DIR=~/Android/android-ndk-1.6_r1
export NDK_WRAPPERS_BASE=~/Android/ndk-wrappers


(3) Edit ~/Android/ndk-wrappers/scripts/build-stlport.sh ad add these

export NDK_WRAPPERS_BASE=~/Android/ndk-wrappers
export PATH=~/Android/ndk-wrappers/bin:$PATH


(4) Edit ~/Android/ndk-wrappers/scripts/env-utils.sh and change / add these

SYSROOT="${NDK_DIR}/build/platforms/android-4/arch-arm"
HOST=darwin-x86


For linux
HOST=linux-x86

For CYGWIN
HOST=windows

(5) Change ~/Android/ndk-wrappers/stlport/build/lib/android.mak
from

--sysroot=$(NDK_DIR)/build/platforms/android-1.5/arch-arm \


to

--sysroot=$(NDK_DIR)/build/platforms/android-4/arch-arm \


(6) Add link

cd ~/Android/ndk-wrappers/stlport/build/Makefiles/gmake
ln -s linux arm-linux


(7) For Mac only, upgrade sed using Mac port

sudo port install gsed
sudo ln -s /opt/local/bin/gsed /opt/local/bin/sed



(8) build stlport

cd ~/Android/ndk-wrappers
export NDK_DIR=~/Android/android-ndk-1.6_r1
./setup.sh


(9) Create hello.cpp to test

#include <iostream>
using namespace std;
int main() {
  cout << "hello, world\n";
  return 0;
}


(10) Compile

~/Android/ndk-wrappers/bin/arm-linux-g++ -o hello hello.cpp -L~/Android/ndk-wrappers/stlport/build/lib/obj/arm-linux-gcc/so


(11) Sent to device for testing

adb push hello /data/local/bin/hello
adb shell chmod 755 /data/local/bin/hello
adb shell /data/local/bin/hello


You can use Makefile
Makefile Select all

APP := hello
SDKTOOL := ~/Android/android-sdk-mac_x86-1.6_r1/tools
WRAPPER := ~/Android/ndk-wrappers
STLLIB := -L~/Android/ndk-wrappers/stlport/build/lib/obj/arm-linux-gcc/so
BIN := $(WRAPPER)/bin
CPP := $(BIN)/arm-linux-g++
CC := $(BIN)/arm-linux-gcc

all: $(APP)

OBJS += $(APP).o

$(APP): $(OBJS)
$(CPP) $(LDFLAGS) -o $@ $(STLLIB) $^

%.o: %.c
$(CC) -c $(INCLUDE) $(CFLAGS) $< -o $@

%.o: %.cpp
$(CPP) -c $(INCLUDE) $(CFLAGS) $(CPPFLAGS) $< -o $@

install: $(APP)
$(SDKTOOL)/adb push $(APP) /data/local/bin/$(APP)
$(SDKTOOL)/adb shell chmod 755 /data/local/bin/$(APP)

shell:
$(SDKTOOL)/adb shell

run:
$(SDKTOOL)/adb shell /data/local/bin/$(APP)

clean:
@rm -f $(APP).o $(APP)






2 comments:

CaseyB said...

I get STLPort to build fine, (thanks!) but when I try to use it I get a bunch of errors that look like this:

~/Development/android-ndk-1.6_r1/ndk-wrappers/stlport/stlport/stl/_cstdlib.h:140: error: conflicting types for 'abs'
build/platforms/android-4/arch-arm/usr/include/stdlib.h:84: error: previous definition of 'abs' was here

My Android.mk looks like this:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

STLPORT_BASE := ~/Documents/Development/android-ndk-1.6_r1/ndk-wrappers/stlport

LOCAL_MODULE := stlportdemo

LOCAL_CFLAGS += -I$(STLPORT_BASE)/stlport \
-D__NEW__ \
-D__SGI_STL_INTERNAL_PAIR_H \
-DANDROID \
-DOS_ANDROID \
-DANDROID_NDK

LOCAL_SRC_FILES := \
GameObjects/BackgroundColor.cpp \
GameObjects/GameObject.cpp \
GameObjects/GLManager.cpp \
GameObjects/Particle.cpp \
GameObjects/ParticleContainer.cpp \
GameObjects/ParticleGravity.cpp \
GameObjects/Streamer.cpp \
GameObjects/TunnelParticleContainer.cpp \
GameObjects/Word.cpp \
GameObjects/WordBall.cpp \
GameObjects/WordBallContainer.cpp \

LOCAL_PREBUILT_LIBS := libs/libstlport.a

include $(BUILD_MULTI_PREBUILT)

LOCAL_STATIC_LIBRARIES := libs/libstlport

LOCAL_LDLIBS := -lGLESv1_CM -ldl -llog

include $(BUILD_SHARED_LIBRARY)

Do you have any ideas as to why this might be?

Great Android said...

Thanks you very much