Post Reply

[Q] Android cross compiling - please help

OP original_red

22nd May 2012, 11:41 AM   |  #1  
OP Junior Member
Thanks Meter: 1
 
2 posts
Join Date:Joined: Aug 2010
I need some help with compiling a lib (ODE) for android - problem is it uses automake and I can't seem to find any decent documentation/howto to cross compile a static/dynamic lib for android.

If anyone has any experience in doing such builds, please can you help.

BTW, I'm using ubuntu 12.04 as a dev environment (latest up-to-datest SDK and NDKs are installed) and I have no problem with making the wrapper library - just need to get the static (or dynamic) lib built.
The Following User Says Thank You to original_red For This Useful Post: [ View ]
22nd May 2012, 01:47 PM   |  #2  
OP Junior Member
Thanks Meter: 1
 
2 posts
Join Date:Joined: Aug 2010
Never mind...
Never mind, I seem to have cracked it...

Here's the script I wrote...

ndk-autoconf
Code:
#!/bin/bash

if [ "$1" = "" ]; then
	echo "Usage ndk-autoconf <api>\ne.g. ndk-autoconf android-9"
	exit 1
else
	platform=$1
	shift
	
	export NDK=${HOME}/android-ndk

	cp ${NDK}/config.guess .
	cp ${NDK}/config.sub .
	
	export NDK_TOOLCHAIN=${NDK}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin
	export CROSS_COMPILE=arm-linux-androideabi
	export CC=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-gcc
	export CXX=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-g++
	export LD=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-ld
	export AR=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-ar
	export RANLIB=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-ranlib
	export STRIP=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-strip

	export ac_cv_func_malloc_0_nonnull=yes
	export ac_cv_func_realloc_0_nonnull=yes
	
	export SYSROOT=${NDK}/platforms/${platform}/arch-arm
	export CXXFLAGS=--sysroot=${SYSROOT}
	export CFLAGS=--sysroot=${SYSROOT}

	argStr=""
	
	while [ "$1" != "" ]; do
		argStr="${argStr} $1"
		shift
	done
	
	./configure --host=arm-linux-androideabi --target=arm-linux-androideabi --prefix=$SYSROOT/usr LIBS="-lc -lgcc" $argStr
	make
	make install
fi
you'll need to get config.sub and config.guess from stackoverflow.com/questions/4594736/configure-does-not-recognize-androideabi (bottom answer) and copy them into your ndk root
Last edited by original_red; 22nd May 2012 at 02:21 PM. Reason: updated script
11th October 2013, 12:17 AM   |  #3  
Senior Member
Thanks Meter: 294
 
198 posts
Join Date:Joined: Mar 2012
Donate to Me
improved
i improved your script.
after i wrote it down i realize that with NDK is more easier create a specific Android.mk rather then running configure.

hope this helps.

Code:
#!/bin/bash

# static data
CROSS_COMPILE=arm-linux-androideabi
NDK_TOOLCHAIN=""

function die()
{
  echo $@
  exit 1
}

function arg_parse()
{
  if [ $# -lt 2 ] ; then
    die -e "Usage ndk-autoconf <ndk_path> <api>\ne.g. ndk-autoconf /opt/android-ndk 9"
  fi

  if [ ! -d "$1" ] ; then
    die "error: the ndk-path is not valid"
  fi

  if [ ! -d "$1/platforms/android-$2/" ] ; then
    die "cannot find android sysroot in your ndk_path"
  fi
}

function check_external_scripts()
{
  if [ ! -f "${NDK}/config.guess"  -o ! -f "${NDK}/config.sub" ] ; then
    echo "warning: missing config.guess or config.sub in your ndk dir"
    echo "info: should i download them for you? [y/n]"
    read choice
    if [ "x$choice" != "xy" -a "x$choice" != "xY" ] ; then
      die "error: missing config.guess or config.sub"
    fi
    wget "http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD" -qO "${NDK}/config.sub" || die "cannot download config.sub"
    wget "http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD" -qO "${NDK}/config.guess" || die "cannot download config.guess"
  fi
  cp ${NDK}/config.guess . || die "cannot import files"
  cp ${NDK}/config.sub . || die "cannot import files"
}

function find_toolchain()
{
  local tmp;
  local host_arch;
  
  case $(uname -m) in
    x86_64)
      host_arch="x86_64"
      ;;
    i[3456]86)
      host_arch="x32"
      ;;
    *)
      die "cannot detect your system arch"
      ;;
  esac
  
  for d in $( ls -1 "${NDK}/toolchains/" ); do
    name=$(basename "$d")
    case $name in
      *clang*) continue
	  ;;
      $CROSS_COMPILE-*) 
	  NDK_TOOLCHAIN="${NDK}/toolchains/$name/prebuilt/linux-$host_arch/bin"
	  tmp="${NDK_TOOLCHAIN}/${CROSS_COMPILE}-"
	  for bin in gcc g++ ld ar ranlib strip; do
	    if [ ! -f "$tmp$bin" ] ; then
	      unset NDK_TOOLCHAIN
	      break
	    fi
	  done
	  ;;
      *) 	continue
	  ;;
    esac
  done
  test "x$NDK_TOOLCHAIN" != "x" || die "cannot find an usable toolchain"
}

## MAIN ##

arg_parse $@
NDK=$1
PLATFORM="android-$2"
shift
shift
check_external_scripts $@

find_toolchain

export CC=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-gcc
export CXX=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-g++
export LD=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-ld
export AR=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-ar
export RANLIB=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-ranlib
export STRIP=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-strip

export ac_cv_func_malloc_0_nonnull=yes
export ac_cv_func_realloc_0_nonnull=yes

export SYSROOT=${NDK}/platforms/${PLATFORM}/arch-arm
export CXXFLAGS=--sysroot=${SYSROOT}
export CFLAGS=--sysroot=${SYSROOT}

argStr=""
	
while [ "$1" != "" ] ; do
	argStr="${argStr} $1"
	shift
done

./configure "--host=$CROSS_COMPILE" "--target=$CROSS_COMPILE" "--prefix=$SYSROOT/usr" LIBS="-lc -lgcc" $argStr && \
make -j$(( $(cat /proc/cpuinfo | grep processor | wc -l) +1 ))
#DEBUG
#make install
25th November 2013, 11:37 AM   |  #4  
Junior Member
Thanks Meter: 0
 
1 posts
Join Date:Joined: Nov 2011
Hi,

I have modified a line in the current script at function find_toolchain() as on my setup (having Ubuntu 32 bit) the host_arch field always comes x86.

I have downloaded latest NDK toolchain for API19 (or Kitkat). However when I am compiling ncurses library I am getting following error in configure. Any guesses?

checking how to run the C++ preprocessor... /lib/cpp
configure: error: C++ preprocessor "/lib/cpp" fails sanity check


The command I gave is

./ndk_autoconf.sh <path to ndk> 19

Thanks.
23rd February 2014, 02:51 AM   |  #5  
Junior Member
Thanks Meter: 2
 
28 posts
Join Date:Joined: Aug 2007
Quote:
Originally Posted by actiononmail

Hi,

I have modified a line in the current script at function find_toolchain() as on my setup (having Ubuntu 32 bit) the host_arch field always comes x86.

I have downloaded latest NDK toolchain for API19 (or Kitkat). However when I am compiling ncurses library I am getting following error in configure. Any guesses?

checking how to run the C++ preprocessor... /lib/cpp
configure: error: C++ preprocessor "/lib/cpp" fails sanity check


The command I gave is

./ndk_autoconf.sh <path to ndk> 19

Thanks.

You must include this line on script:
Code:
export CPP=${NDK_TOOLCHAIN}/${CROSS_COMPILE}-cpp
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Top Threads in Android Q&A, Help & Troubleshooting by ThreadRank