[FIXED] Failing to cross-compile c++ library with Android NDK

Issue

The library compiles perfectly in linux (Ubuntu 20.04.2 LTS 64), but it fails when trying to compile it for android.
Used procedure:

$ git https://github.com/ufz/iphreeqc 
$ cd iphreeqc 
$ mkdir output 
$ export ANDROID_NDK=$ANDROID_SDK_ROOT/ndk/22.1.7171670 
$ PATH=$PATH:$ANDROID_NDK 
$ export TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64 
$ export TARGET=armv7a-linux-androideabi 
$ export API=21 
$ export AR=$TOOLCHAIN/bin/llvm-ar 
$ export CC=$TOOLCHAIN/bin/$TARGET$API-clang 
$ export AS=$CC 
$ export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++ 
$ export LD=$TOOLCHAIN/bin/ld 
$ export RANLIB=$TOOLCHAIN/bin/llvm-ranlib 
$ export STRIP=$TOOLCHAIN/bin/llvm-strip 
$ ./configure --prefix=$(pwd)/output --host $TARGET --disable-shared 
$ make       

Hereafter the output and error. Any help is highly appreciated.

    Making all in src
    make[1]: Entering directory '/home/hery/Downloads/iphreeqc/src'
      CXX      CSelectedOutput.lo
      CXX      IPhreeqc.lo
      CXX      IPhreeqc_interface_F.lo
      CXX      IPhreeqcLib.lo
      CXX      phreeqcpp/advection.lo
      CXX      phreeqcpp/basicsubs.lo
      CXX      phreeqcpp/cl1.lo
      CXX      phreeqcpp/common/Parser.lo
      CXX      phreeqcpp/common/PHRQ_base.lo
      CXX      phreeqcpp/common/PHRQ_io.lo
      CXX      phreeqcpp/common/Utils.lo
      CXX      phreeqcpp/cvdense.lo
    phreeqcpp/cvdense.cpp:76:9: warning: 'malloc' macro redefined [-Wmacro-redefined]
    #define malloc PHRQ_malloc
            ^
    <command line>:31:9: note: previous definition is here
    #define malloc rpl_malloc
            ^
    1 warning generated.
      CXX      phreeqcpp/cvode.lo
    phreeqcpp/cvode.cpp:76:9: warning: 'malloc' macro redefined [-Wmacro-redefined]
    #define malloc MACHENV_MALLOC PHRQ_malloc
            ^
    <command line>:31:9: note: previous definition is here
    #define malloc rpl_malloc
            ^
    1 warning generated.
      CXX      phreeqcpp/cxxKinetics.lo
      CXX      phreeqcpp/cxxMix.lo
      CXX      phreeqcpp/dense.lo
      CXX      phreeqcpp/Dictionary.lo
      CXX      phreeqcpp/dumper.lo
      CXX      phreeqcpp/Exchange.lo
      CXX      phreeqcpp/ExchComp.lo
      CXX      phreeqcpp/GasComp.lo
      CXX      phreeqcpp/gases.lo
      CXX      phreeqcpp/GasPhase.lo
      CXX      phreeqcpp/input.lo
      CXX      phreeqcpp/integrate.lo
      CXX      phreeqcpp/inverse.lo
    phreeqcpp/inverse.cpp:5319:9: warning: result of comparison of constant -1 with expression of type 'char' is always true [-Wtautological-constant-out-of-range-compare]
                    if (c != EOF && c != '\n' && i != MAX_LINE)
                        ~ ^  ~~~
    phreeqcpp/inverse.cpp:5343:9: warning: result of comparison of constant -1 with expression of type 'char' is always false [-Wtautological-constant-out-of-range-compare]
                    if (c == EOF)
                        ~ ^  ~~~
    2 warnings generated.
      CXX      phreeqcpp/ISolution.lo
      CXX      phreeqcpp/ISolutionComp.lo
      CXX      phreeqcpp/isotopes.lo
      CXX      phreeqcpp/kinetics.lo
      CXX      phreeqcpp/KineticsComp.lo
      CXX      phreeqcpp/mainsubs.lo
      CXX      phreeqcpp/model.lo
    phreeqcpp/model.cpp:3364:8: error: no member named 'isfinite' in namespace 'std'; did you mean 'finite'?
                    if (!PHR_ISFINITE((double) delta[i]))
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ../src/phreeqcpp/Phreeqc.h:2041:29: note: expanded from macro 'PHR_ISFINITE'
    #    define PHR_ISFINITE(x) std::isfinite(x)
                                ^~~~~
    /home/hery/Android/Sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/math.h:373:5: note: 'finite' declared here
    int finite(double __x) __attribute_const__;
        ^
    phreeqcpp/model.cpp:3559:8: error: no member named 'isfinite' in namespace 'std'; did you mean 'finite'?
                    if (!PHR_ISFINITE((double) delta[i]))
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ../src/phreeqcpp/Phreeqc.h:2041:29: note: expanded from macro 'PHR_ISFINITE'
    #    define PHR_ISFINITE(x) std::isfinite(x)
                                ^~~~~
    /home/hery/Android/Sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/math.h:373:5: note: 'finite' declared here
    int finite(double __x) __attribute_const__;
        ^
    phreeqcpp/model.cpp:4990:10: error: no member named 'isfinite' in namespace 'std'; did you mean 'finite'?
                                    if (!PHR_ISFINITE((double) f))
                                         ^~~~~~~~~~~~~~~~~~~~~~~~
    ../src/phreeqcpp/Phreeqc.h:2041:29: note: expanded from macro 'PHR_ISFINITE'
    #    define PHR_ISFINITE(x) std::isfinite(x)
                                ^~~~~
    /home/hery/Android/Sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/math.h:373:5: note: 'finite' declared here
    int finite(double __x) __attribute_const__;
        ^
    phreeqcpp/model.cpp:5050:12: error: no member named 'isfinite' in namespace 'std'; did you mean 'finite'?
                                                            if (PHR_ISFINITE((double) d1) /*&& d1 < 5.0*/)
                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~
    ../src/phreeqcpp/Phreeqc.h:2041:29: note: expanded from macro 'PHR_ISFINITE'
    #    define PHR_ISFINITE(x) std::isfinite(x)
                                ^~~~~
    /home/hery/Android/Sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/math.h:373:5: note: 'finite' declared here
    int finite(double __x) __attribute_const__;
        ^
    phreeqcpp/model.cpp:5733:10: error: no member named 'isfinite' in namespace 'std'; did you mean 'finite'?
                                    if (!PHR_ISFINITE(my_array[j * (count_unknowns + 1) + i]))
                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ../src/phreeqcpp/Phreeqc.h:2041:29: note: expanded from macro 'PHR_ISFINITE'
    #    define PHR_ISFINITE(x) std::isfinite(x)
                                ^~~~~
    /home/hery/Android/Sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/math.h:373:5: note: 'finite' declared here
    int finite(double __x) __attribute_const__;
        ^
    5 errors generated.
    make[1]: *** [Makefile:1056: phreeqcpp/model.lo] Error 1
    make[1]: Leaving directory '/home/hery/Downloads/iphreeqc/src'
    make: *** [Makefile:400: all-recursive] Error 1

Solution

Editing src/phreeqcpp/Phreeqc.h to add an #include <cmath> below #include <math.h> made it compile here.

Please pay it forward and submit this as a pull request to the project!

Answered By – Botje

Answer Checked By – David Goodson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published