Tuesday, February 12, 2013

How to build the gcc Fortran cross-compiler for Android (ARM and x86)

If you need to cross-compile for Android a program written in Fortran, you know already that the official Android NDK does not come with the gfortran compiler, and if like me you need to port to Android code that depends on Fortran (such as the lapack libraries), you are out of luck.
Fortunately I managed to compile the gcc Fortran cross-compiler with the help of  Mike Long's blog. As an added bonus, I managed to build gfortran 4.8.0 not only for the ARM but also for the x86 toolchain, as well as update the script to the latest Android NDK (currently r8d).
What you need to do is the following:
  1. download the fortran4android shell script.
  2. download the ndk-r8d fortran patch.
  3. run the script in your directory:

    $ fortran4android
  4. wait, wait, wait, ... and you will see in the android-ndk-r8d/toolchains directory two new toolchains called arm-linux-androideabi-4.8.0 and x86-4.8.0
  5. Enjoy your new gfortran compiler.

Note that I've successfully tested the script only on Ubuntu 12.04.

Addendum of August 30, 2013

I now have available a patch for the newer android-ndk-r9. To use this instead of the patch above:
  1. download the ndk-r9-fortran-patch
  2. change the fortran4android script by replacing "r8d" with "r9" everywhere
  3. run the fortran4android script in your directory.
 Note that r9 of the NDK already comes with a 4.8 gcc toolchain and to keep it separated, the fortran4android script will still generate in output a "4.8.0" toolchain.


  1. Thanks for the script, works great!

    I'm curious if you know why gcc builds --without-quadmath for ARM. I'm trying to compile the R project, but it seems R is reliant on libquadmath.

    1. Joshua,
      I'm sure we can tweak the gcc configure to build with quadmath support. In alternative you can checkout this post http://rwiki.sciviews.org/doku.php?id=getting-started:installation:android where they seems to have ported R to Android and there is also a link to download a gcc toolchain to install on the device itself.
      It's possible that in the future I will experiment myself with porting R to Android, and if so I will post on this blog. Stay tuned.

  2. Great work! It really makes me happy to see the amount of interest there is in giving Fortran a chance to shine on new platforms. Any chance you've managed to get OpenMP support enabled for ARM on Android? I've been putting off doing that for a while, so... ;)

    1. No, thank you for your initial hard work! Regarding OpenMP I don't have any porting plans for Android (for now).

    2. Maybe MPI would be a better alternative? Or even better, Coarray fortran! :D

  3. Excellent. Thank-you for this. I work in an industry that relies heavily on models written in Fortran. What you have done opens a lot of doors.

    1. Hi, i am an engineer working in offshore oil/gas field.. i am good with fortran 95 and have many self written programs written in f95 for my work related calculations.. i was searching for option to make them work on my android phone and a link from google directed me here.. any one could please suggest how/whether it is possible to make use of my fortran skills for andorid app development.. thanks

  4. Hi,

    I have tried running the script and got the following error message:

    /bin/ksh: Defective interpreter: File or directory not found

    I am running Kubuntu 13.04. Could that be the reason, and any idea how to fix it?


    1. You can either try to install the Korn shell (ksh) package on your system or in alternative you can change the script and make it bash compatible (the only changes should be convert the first line from !#/bin/ksh to !#/bin/bash and see what happens).

    2. Editing the first line to !#/bin/bash worked like a charm. Thank you very much :-)

  5. Hi,
    Thanks for writing this report.
    I tried this on Ubuntu 14.04 and it builds gcc and g++ but not gfortran. Any idea?

    1. What kind of error are you getting? I tried right now and it fails in the config step complaining that the m4 library is missing. Try to install m4 (a macro processing language) from the Ubuntu Software Center and run the script from scratch. I'll try the same and let you know if I bump into other problems.

  6. Hi ,
    Thank you for posting this, I am able to generate toolchain for 32 bit please let me know what changes should i perform to generate toolchain for 64 bit platform.

    Vineet Pant

    1. Vineet,
      I never tried to generate the toolchain for a 64 bit platform, and off the bat I don't know what kind of modifications to the script is necessary to do that. Just for clarification what are you trying to accomplish, generate a 64-bit executable to run on Android?

    2. Hi Danilo,
      You are right, I am trying to generate 64 bit android executable.

  7. Hi Danilo ,

    I am facing some errors due to missing header files , please let me know if there is some particular way to resolve it.

    test.cpp:1:20: fatal error: iostream: No such file or directory

    Vineet Pant

  8. Hi, Danilo.
    Thank for the best guide for fortran addition to arm-android toolchain.
    Unfortunately i have problem with getting google build directory
    > wget https://dl.dropbox.com/u/97874334/build_tools_ndk_r6b.tgz

    Building with latest from google repo really breaks things, as you mentitioned in comment in your script.
    I think we can modify script with checking out to certain revision from git repo, just i need to understand what is really goes wrong there.
    Regards, Victor.

    1. Victor,

      it's been a while since I was playing around with the Fortran compiler. As you mentioned, you could try to get the code from the official Google repo but dated back in February 2013.

  9. Hi, i am an engineer working in offshore oil/gas field.. i am good with fortran 95 and have many self written programs written in f95 for my work related calculations.. i was searching for option to make them work on my android phone and a link from google directed me here.. any one could please suggest how/whether it is possible to make use of my fortran skills for andorid app development.. thanks

  10. Great post. Could you recommend how to crosscompile fortran for the Pandaboad (Omap ARM)?

    1. Dave,
      I have access to a Panda board but I never had the chance/time to play with cross-compiling Fortran. Sorry.

  11. This comment has been removed by the author.

  12. Hi there Danilo,

    I'm having a bit of trouble building fortran4android in Ubuntu 12.04 and I wondered if you could give me a little help?

    I've run your build exactly as shown on http://danilogiulianelli.blogspot.co.uk/2013/02/how-to-build-gcc-fortran-cross-compiler.html .

    After installing ksh, and invoking the fortran4android command, all the build tools appear to be downloaded successfully (ndk, binutils, gdb, gmp, mpc, mpfr).

    In the build I get this with several modules:
    ERROR: Missing gdb sources: /home/ubuntuuser/Downloads/android-ndk-r8d/src/gdb/gdb-7.3.x

    Obviously I have tried correcting the versions, but then I get:
    Error while trying to configure toolchain build. See /tmp/ndk-ubuntuuser/build/toolchain/config.log

    I'd appreciate it if you could possibly retry the build and/or possibly advise me as to what I'm doing wrong?

    1. Greg,
      not sure what's going on in your case. My suggestion is try to look at the log files to see if you spot something obvious. Perhaps a mismatch with the required gdb version used by the script.

  13. This comment has been removed by the author.

  14. Thanks a lot for the post!
    I was able to get through to building of toolchain but im getting errors after that. could you please advice on what I should do

    Sysroot : Copying: /home/mukta/and/android-ndk-r8d/platforms/android-9/arch-x86 --> /tmp/ndk-root/build/toolchain/prefix/sysroot
    Configure: x86-4.8.0 toolchain build
    Building : x86-4.8.0 toolchain [this can take a long time].
    Error while building toolchain. See /tmp/ndk-root/build/toolchain/config.log

    1. Mukta,
      sorry but without you posting any specific errors from the listed log file, I cannot guess what could have possibly gone wrong.

    2. Hi,
      I have copied a section of the log file where the first error pops up. I am not sure how to correct this.

      if [ xinfo = xinfo ]; then \
      rm -f doc/gfortran.info-*; \
      makeinfo --split-size=5000000 --split-size=5000000 -I /home/mukta/cross/and/android-ndk-r8d/src/build/../gcc/gcc-4.8.0/gcc/doc/include -I /home/mukta/cross/and/android-ndk-r8d/src/build/../gcc/gcc-4.8.0/gcc/fortran \
      -o doc/gfortran.info /home/mukta/cross/and/android-ndk-r8d/src/build/../gcc/gcc-4.8.0/gcc/fortran/gfortran.texi; \
      else true; fi
      /home/mukta/cross/and/android-ndk-r8d/src/build/../gcc/gcc-4.8.0/gcc/doc/cppopts.texi:808: @itemx must follow @item
      Makefile:4292: recipe for target 'doc/cpp.info' failed
      make[3]: *** [doc/cpp.info] Error 1
      make[3]: *** Waiting for unfinished jobs....

  15. how pass " -mfloat-abi=softfp -mfpu=vfp" to the compiler
    /home/pcl/android-ndk-r9/toolchains/arm-linux-androideabi-4.8.0/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gfortran. Since LOCAL_CFLAGS is not working.

  16. try to install a fresh setup of this, sadly, it is not working now.
    https://dl.dropbox.com/u/97874334/build_tools_ndk_r6b.tgz seems not there. There seems some bugs in the gfortan compilers as described by

    The problem of fortran for me is:
    gcc-4.8.0/libgfortran/io/unix.c:1817: error: undefined reference to 'ttyname_r' when i try to link openblas with gfortran