how to build dynamically linked arm binary

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

how to build dynamically linked arm binary

Post by Daniel Shawul »

I have tried many methods and failed( Cygwin + android-ndk, codesourcery, etc..). Jim Ablet told me how to do a cross platform compile for android on a windows machine but it uses a static build which increases the size of the binary significantly. I have about five binaries to package so size matters to me. How do you build a dynamically linked binary which uses libraries from the existing bionic (chopped up glibc) from the phone ?
Thanks
Michel
Posts: 2271
Joined: Mon Sep 29, 2008 1:50 am

Re: how to build dynamically linked arm binary

Post by Michel »

I can only say that it is possible since I did it.

The fastest way is to use the android-ndk to generate a gcc toolchain.
The android-ndk includes a script to do this. See e.g. here

http://www.srombauts.fr/android-ndk-r5b ... CHAIN.html

Once you have a gcc toolchain you can use gcc as a cross compiler in
the usual way. Everything will have been set up correctly.

If you really want to torment yourself you can use the standard gcc
but then you need a command line of about half a page.
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: how to build dynamically linked arm binary

Post by Daniel Shawul »

oh my finally compiled a hello.c dynamically linked. i had to torment myself with codesourcery before but this one with cygwin and ndk is much easier as you said. Exporting paths and taking care of paths with spaces is what I needed. Now I will try projects with larger system library needs.
thanks a lot
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: how to build dynamically linked arm binary

Post by Daniel Shawul »

The method doesn't work for cpp files. If I change from <stdio.h> to <cstdio> it fails to recognize it. I looked in the ndk include folder and can't find cstdio. It shoud be there somewhere I guess. If I just rename the file to hello.cpp and not change the include it works.
Michel
Posts: 2271
Joined: Mon Sep 29, 2008 1:50 am

Re: how to build dynamically linked arm binary

Post by Michel »

Did you try invoking g++ instead of gcc?
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: how to build dynamically linked arm binary

Post by Daniel Shawul »

Yes I did. But I think you mean arm-linux-androideabi-g++ rather than g++ which is in a similar but different directory. Here is what I did. First cygwin is started with paths of NDK set.

Cygwin starter batch file

Code: Select all

@echo off

set SYSROOT=C&#58;/Progra~1/Android/android-ndk-r8/platforms/android-8/arch-arm
set MYGCC=/cygdrive/c/Progra~1/Android/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-g++
set ARM=%MYGCC% --sysroot=%SYSROOT%

C&#58;
chdir C&#58;\cygwin\bin

bash --login -i
Then

Code: Select all


Daniel@Daniel-PC ~
$ cd "C&#58;\Users\Daniel\Desktop\Hello"

Daniel@Daniel-PC /cygdrive/c/Users/Daniel/Desktop/Hello
$ ls
hello.cpp

Daniel@Daniel-PC /cygdrive/c/Users/Daniel/Desktop/Hello
$ $ARM hello.cpp -o hello

Daniel@Daniel-PC /cygdrive/c/Users/Daniel/Desktop/Hello
$ file hello
hello&#58; ELF 32-bit LSB executable, ARM, version 1 &#40;SYSV&#41;, dynamically linked &#40;uses shared libs&#41;, not stripped

Daniel@Daniel-PC /cygdrive/c/Users/Daniel/Desktop/Hello
$ $ARM hello.cpp -o hello
hello.cpp&#58;1&#58;18&#58; error&#58; cstdio&#58; No such file or directory
hello.cpp&#58; In function 'int main&#40;int, char**)'&#58;
hello.cpp&#58;3&#58; error&#58; 'printf' was not declared in this scope

Daniel@Daniel-PC /cygdrive/c/Users/Daniel/Desktop/Hello
$
The frist run was correct with #include<stdio.h>. The second run fails because it can not find <cstdio>. I think ndk has limited support of c++ accoring to http://stackoverflow.com/questions/2105 ... port-for-c .
But I thought the limit was only for advanced stuff STL, exceptions and RTTI...
Michel
Posts: 2271
Joined: Mon Sep 29, 2008 1:50 am

Re: how to build dynamically linked arm binary

Post by Michel »

I think you did not fully read my suggestion....

You should first generate a gcc-toolchain with the script

Code: Select all

android-ndk-r5/build/tools/make-standalone-toolchain.sh --install-dir <whatever> 
After that no path setting whatsoever is required. As far as I can tell c++ is fully supported (I used it to compile your egbb.so).

Code: Select all

#include <iostream>
using namespace std;
int main () &#123;
  cout << "Hello World!";
  return 0;
&#125;

Code: Select all

$gcc-android/bin/arm-linux-androideabi-g++ hello.c  -ohello
$file hello
hello&#58; ELF 32-bit LSB executable, ARM, version 1 &#40;SYSV&#41;, dynamically linked &#40;uses shared libs&#41;, not stripped
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: how to build dynamically linked arm binary

Post by Daniel Shawul »

I used the first method (step 2) of the link you gave. I did not try the second method (step 3) which uses gcc toolchain script. I will try that now but bear in mind I am working on windows and I don't know if the shell scripts would work...
Michel
Posts: 2271
Joined: Mon Sep 29, 2008 1:50 am

Re: how to build dynamically linked arm binary

Post by Michel »

I will try that now but bear in mind I am working on windows and I don't know if the shell scripts would work..
I had not realized you were using windows.

I think in cygwin shell scripts should work as usual.
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: how to build dynamically linked arm binary

Post by Daniel Shawul »

Thanks. It works now since this one generated the necesessary c++ headers after I run the sh script ( Runs fine on cygwin too ). Don't know where the cstdio library was in the original ndk package. That must be an auto generated thing which is why the first method worked only for c codes.

Edit I compiled scorpio now. The only obstacle was that -lpthread is not included with the ndk but anyway single cpu is fine too.