I want to use some function calls(commands) designed for linux. I can use them by enter the key words in adb(Android CML).
Here I found some works some people did.
wget (because it isn't included in most Android device )
Iperf
But after reading their methods or suggestions, I can only understand that I need to use Android NDK and write the correct makefile. I have no idea about building others source code (most of them are C/C++) for linux(only need to use 'make' command mentioned in their README file). The official NDK document is for Java environment to call C lib mainly.
Are there some HOWTO, courses or suggestions for this. Thanks!
I have compiled single cpp file program. I try to compile a alternative version iperf
https://github.com/tierney/iperf
It seems to be relative to lib ,some header files, and multiple c files. I failed to compile by enter all c files normally. Is there anything I missed?
yeah you need the NDK, it offers an C/C++ compiler for Android.
In general the steps are all the same:
Setting up the NDK (I wrote a small how-to, but it's for Arch-Linux and the fish-shell, Windows how-to)
Adjusting your make file (instead of gcc compiler use Android NDK compiler, and so on)
Remember that Android uses Bionic C library, so only use functions supported by it
Run make, push the program to your device and make it executable
Of course, this is just an overview how it is done. You should try it and then ask specific questions if you run into troubles.
NDK is mostly intended to extend the Java apps, but you can download NDK and create a standalone toolchain from it (see http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html). Now you have a cross-compilation environment which is very similar to standard Linux dev environment.
In addition, for small executables and for testing only, you can also cross-compile and link statically to the libc. This way you don't have to worry about Bionic which could be a loss of time.
Related
I'm trying to build an executable from a c file (not written by me) so that I can use it on my phone.
I'm using a toolchain directly from the Android NDK (that use Clang) built for arm64.
The c file needs, as expressed in the makefile, libpthread librtlsdr libmysqlclient, so I downloaded the arm64 versions of these libraries in my toolchain sysroot directory. First question: is this the right way of doing this?
Anyway, after doing this and executing make, it fails saying that it cannot find libpthread.so.0 and libpthread_nonshared.a.
To solve this I copy in the sysroot folder the file it wants from the libc6 arm 64 package (that are not only the two written above, but many others needed in cascade): this way the "make" seems to work fine but once I push it in my device, with the libraries it needs, and run it, I got a runtime error saying:
cannot find verneed/verdef for version index=32770 referenced by symbol "_res" at "/data/local/tmp/TEMP/libc6.so.6"
In this case the problem should be libc6, but I can't figure how to solve this.
This one is related to the libpthread I've downloaded, so the libc6 package, which is probably not suited for Android.
So the real problem is: is there a way to get rid of the first error I mentioned using just the pthread included in Android?
What I hope is that I'm just missing something or using in the wrong way.
Thanks
No, this is not how you are expected to build an executable for Android. You can either use the NDK toolchain, or an alternative toolchain, as described here: Cross compiling static C hello world for Android using arm-linux-gnueabi-gcc.
I have code I compiled already, and wonder if I can use the resulting executable, which is in elf format as a library in an APK and how please.
#Gabe Sechan; thanks. I did build a JNI project. I am having some issues importing the code from my other project into JNI. I wonder if you can suggest the best way to import it. I can post my Android.mk file if you like. Basically, it seems that project finds the first file, and an associated header file. That file, in turn uses variables, which are defined in another file. But there is no include statement.
You know? I figured if I can use a JAR file as a library, perhaps I could use an executable also. I need to learn more.
# Chris Straton - Thanks. I did edit my post with a comment to address the toolchain issue. But then, I modified it. Regardless, What I stated was if I use the toolchain that is recommended by my target platform, then I should be able to run it on my phone right?
Further; I re-read your comment about ABI and libc compatibility. The two devices are tegra t132 and Samsung S-N900P. So libc should not be an issue since both devices run Android; right? As far as I know both have an arm chip. Is there anything else I need to look into?
Elf is an executable, not a library. What you want to do is get a .so file and link to it via JNI.
You could possibly run it as a command line program and interact with it via its stdin and stdout, but that would be clunky when you can just use it as an actual library.
I used string.h header file in a c code that is to be compiled using arm-gcc compiler and the executable generated is to be executed on an android device.
When I pushed my executable on to the device and executed it, it presented me with linkage error.
How can I use functions from string.h on such a device?
Are these header files hardware specific?
The ARM EABI states that the header files for the ANSI C-library should be provided by the compiler independent from where the library comes. If you are cross-compiling you do however have to make sure you use the string.h coming with the compiler and not from the native system. What linkage error did you get?
It sounds like strstr is missing in the libc, which sounds weird and I don't have an explanation for it. If you have access to the library on the device, then I would check with for example objdump -t if it defines strstr.
How can I use functions from string.h on such a device?
It's worked for me in the past.
Are these header files hardware specific?
With the NDK tools, they are architecture and platform (e.g., Android 8, Android 14, et al.) specific.
Unless there's a serious bug in the NDK (which seems unlikely), I think you might want to check your build again.
I am trying to understand what a standalone toolchain means.
Following are my findings.
A toolchain which is ready to use with all the configuration that is the system headers and libraries in the correct path . For Android it will also have the API headers in the path where the toolchain can look it up. Why the term "standalone"? This probably will be different that the regular toolchain in the sense that the R T will need to be configured and made ready for android use withe sysroot and libc header paths given etc.
Please comment .
Hmm, I was compiling for android and one process was running a script called make-standalone-toolchain.sh a standalone toolchain is created .I was going through this script to understand what this is doing. Not really good at shell scripting. But made out certain things. ""Generate a customized Android toolchain installation that includes a working sysroot. The result is something that can more easily be used as a standalone cross-compiler, e.g. to run configure and make scripts." --toolchain arch ndk-dir package-dir system platform variables are set Compute source sysroot
SRC_SYSROOT="$NDK_DIR/platforms/$PLATFORM arch-$ARCH" Copying sysroot headers and libraries... libstdc++ headers and libraries... prebuilt binaries.all into a temporary folder then a copying from Tmp dir to install
dir creating a tar ie a package file to add the tmpdir wanted to know what exactly is happening here or a link or suggestion where to look.but ofcourse dont want to read very elaborate manuals.
~
This blog posting may answer your question:
http://clseto.mysinablog.com/index.php?op=ViewArticle&articleId=3480794
There is a recommended way to build native C/C++ program on Android: List your source files in a script (Android.mk) and run 'ndk-build'. It may not be a problem if you are writing new programs but if you already have a working build script (like those open source softwares) which can build you program nicely on Linux, it would be a headache to migrate your script to Android.mk. All you need in this situation is a C/C++ cross compiler and then replace the variables in your script (such as CC, CXX, AR, AS, RANLIB, ...) to something like 'arm-linux-androideabi-gcc', 'arm-linux-androideabi-g++', ...
Fortunatley, inside the 'Android NDK Dev Guide', there is a section 'Standalone Toolchain' which just describes what we need....
First of all, the best guide for stand alone toolchains in Android is here: https://developer.android.com/ndk/guides/standalone_toolchain.
I have used it several times for different devices and platform.
You need to download NDK and then run the script 'make-standalone-toolchain.sh' with a few parameters (as said in the link above) that will determine the API levels of your apps, the architecture of the device etc.
The output of the script will be a directory that you can use as a toolchain in order to cross compile native C/C++ code to run on Android devices. You need to put in your Makefile the path to the toolchain directory and add the architecture suffix for the binaries inside (for example 'arm-eabi-'). Something like:
CROSS_COMPILE = /path-to-toolchain-dir/bin/arm-eabi-
There should be files like '/path-to-toolchain-dir/bin/arm-eabi-gcc' in your toolchain directory.
Anyway, this will tell the Makefile to use your toolchain's binaries in order to compile the C/C++ native code and create the compatible executables for your target machine.
For example, this is the commands I used to create a stand alone tool chain for a certaion Android device:
./make-standalone-toolchain.shj --arch=arm --platform=android-21 --install-dir=<dest-dir> --toolchain=arm-linux-androideabi-4.9
I have a cross-platform C library that I need to compile for Android as a *.so file. The library consist of many .c and .h files, and it use autotools as it's buid system. (./configure && make dep && make). Afaik, the library does not depend on other libraries, other than libc and OpenSSL (which should be present on Andriod).
I'm trying to find the simplest (read fastest in terms or not needing to read hundreds of pages of manuals and then apply try && fail brute-force approaches to complete the task) way of getting the library off my machine in source code form, and into the Android phones as a .so. The library will eventually be accessed from Java's native library interface. For development, I have both Windows and Debian machines on my desk.
If you're lucky and the autotooled project is set up correctly, you can cross-compile by running (this example is cross-compiling for windows using mingw, I do not know what the prefix is for Android):
./configure --host=i586-pc-mingw32
This will then try to find compilers with a prefix of i586-pc-mingw32-, so i586-pc-mingw32-gcc will likely be the first one found and used. For your Android devkit, have a look at what your compiler binary is called and guess the host value from that.
We ended up manually creating an Android NDK project with all the required files.