How to remove libusb from Android standalone toolchain? - android

I'm trying to compile a tool that uses libusb with modified libusb for android.
Modified libusb was compiled and installed to /usr/local/lib as expected.
The problem is that standalone toolchain already contains libusb and it hurts linking/compilation.
./configure
Returns:
...
checking for usb_get_string_simple in -lusb... yes
...
So libusb if found okay.
But with standalone toolchain:
export CROSS_COMPILE=arm-linux-androideabi
export CC=${CROSS_COMPILE}-gcc
export CXX=${CROSS_COMPILE}=g++
export NDK=/softdev/android-ndk-r8e
export ANDROID_NDK_ROOT=/softdev/android-ndk-r8e
export SYSROOT=$NDK/platforms/android-8/arch-arm
export PATH=/softdev/arm-toolchain/bin:$PATH:/softdev/android-ndk-r8e/platforms/android-8/arch-arm
./configure --build=x86-unknown-linux-gnu --host=arm-linux-androideabi --target=arm-linux-androideabi
It returns:
...
checking for usb_get_string_simple in -lusb... no
...
how to make it working with standalone toolchain?
libusb-0.1 is used

Well, finally i was able to fix it.
the tool configure.ac file was not written in account to using libusb-compat (but for only libusb-0.1).
So i solved it by adding dependency while searching libusb-0.1 (-lusb-1.0):
AC_CHECK_LIB([usb], [usb_get_string_simple], [have_libusb=yes], [], [-lusb-1.0])
2. Another problem was to make AC_CHECK find libs not in standard system /usr/local/lib folder as modified library interface was a bit different
and it tried to gcc with system one while searching with AC_CHECK_LIB. I used CPPFLAGS to set the right libs path

Related

How to compile a simple OpenCL program in Android?

I've spent many hours today trying to accomplish this seemingly very simple task so my frustration may seem strange to you.
I want to "run"/execute a program that contains OpenCL functions on my Samsung Galaxy S7 (Exynos SoC). Support for OpenCL shouldn't be an issue because:
1) It's a well supported flagship phone from a well-known manufacturer
2) libOpenCL.so is present in /system/vendor/lib/libOpenCL.so
3) OpenCL-Z reports a valid platform and device (GPU only for some reason)
Things that I tried:
Rooted the phone.
Installed cppdroid.
Tried to compile a simple program that contains references to standard OpenCL functions like clGetPlatformIDs().
Header files should be included somewhere, but I could not for the life of me find where to put them (using the same path where cppdroid stores standard header files did not work), so I manually included the headers in the .c file itself, making it barely editable on the phone itself.
My understanding is that I should link the library (libOpenCL.so) to the output of gcc during the linking phase. There seems to be an option to do that in cppdroid, under "Settings" "Project settings" "Link options" so I add:
-L/system/vendor/lib/libOpenCL.so
to that field. However compilation still fails and I'm getting "undefined references to clGetPlatformIDs()" as if I hadn't included the library at all.
After searching online for a solution, a possible remedy (see below) proved useless:
-Wl,--whole-archive -L/system/vendor/lib/libOpenCL.so
I thought I'd get gcc on its own since this is the compiler used by cppdroid and try to compile that way. Found out that it's supposed to be deprecated, and replaced by "clang". But cppdroid does include gcc, so I navigated to the folder containing the executable (I believe it's an executable, there seem to be no extensions here, and I'm a Windows person) which appears to be:
/data/data/name.antonsmirnov.android.cppdroid/sdk/gcc#4.8#2/bin
and tried to "execute" gcc-4.8 (which appears to be the executable compiler) from termux (a terminal emulator for Android). Much to my surprise I got the error message:
sush: gcc-4.8: not found
Even though ls -1 lists the file normally!
I tried various other stuff that did not work, but here's my question:
How do I go about compiling a simple OpenCL program that I've written in C, so as to execute it on my Android mobile device?
Why do I get the
sush: gcc-4.8: not found
error message?
My understanding is that I should link the library (libOpenCL.so) to the output of gcc during the linking phase. There seems to be an option to do that in cppdroid, under "Settings" "Project settings" "Link options" so I add:
-L/system/vendor/lib/libOpenCL.so
to that field. However compilation still fails and I'm getting "undefined references to clGetPlatformIDs()" as if I hadn't included the library at all.
You need to add -lOpenCL and -L/system/vendor/lib to the linker flags. The second option may not be required, as this directory should be in a default search path already.
First -L provides a search directory for libraries to link, you should be using -L/system/vendor/lib you can use -l to link to a library in your case -lOpenCL without the lib prefix.
I got it working on my Samsung S9+ Exynos phone. Here is what I did to use OpenCL in the Termux app:
apt update
apt upgrade
apt autoremove
apt install vim openssh clang git
vim ~/.bashrc
# press the i key, then paste the line below, then save and exit with :wq
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/system/vendor/lib64:/system/vendor/lib64/egl
# restart Termux
exit
And finally, to compile a C++ OpenCL program with headers as described here:
cd Test/src
g++ *.cpp -o Test.exe -std=c++11 -pthread -w -I./OpenCL/include -L/system/vendor/lib64 -lOpenCL
./Test.exe
To install clinfo do the following:
apt install cmake make
cd ~
mkdir opencl
cd ~/opencl
git clone https://github.com/KhronosGroup/OpenCL-Headers
cd ~/opencl/OpenCL-Headers
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=$PREFIX
cmake --build build --target install
cd ~/opencl
git clone https://github.com/Oblomov/clinfo
cd ~/opencl/clinfo
make OS=Android
./clinfo

Android cross compiling C with no configure file

I've recently started to cross compile a couple of C applications for Android. I've successfully done this for image conversion software using the following pseudo-instructions:
Build the Android toolchain
Compile Automake and Autoconf
Set the CC and CXX environment variables
Build the source package (delete configure, configure.sub, configure.guess, run autoconf configure.ac > configure, run configure with CFLAGS using arm-linux-androideabi, make and make install)
I am now trying to do this for an OCR library written in C. The problem that I am running into is that the library has no configure, configure.guess, or configure.sub. It only has a makefile.
I am not too familiar with C, but is there any different process that I should be following in this situation?
The ./configure step:
CFLAGS=’-march=armv7-a –mfloat-abi=softfp’ ./configure –prefix=/home/user/Downloads/install/usr/local –host=arm-eabi –enable-shared=no –with-modules --enable-delegate-build
is there any different process that I should be following in this situation?
Not really. I mean, if you plan to work with the Android Standalone Toolchain, then all you need to do is:
create the toolchain with the ad hoc target, e.g --platform=android-9
add the toolchain directory to the PATH (for convenience)
export the sysroot path, e.g. export SYSROOT=/tmp/my-android-toolchain/sysroot (for convenience)
set your C compiler (export CC="arm-linux-androideabi-gcc --sysroot $SYSROOT") and archiver, if you target a static library (export AR=arm-linux-androideabi-ar)
Then it all depends about how your Makefile has been written, i.e what are the targets? influential environment variables? etc. But in practice, if the default target fits your need, all you need to do is to run make while overriding the C flags to set the target architecture (ARMv7 here):
make CFLAGS="-march=armv7-a"
That's it. Repeat with other archs (armeabi, x86, etc) if needed.
You can see a complete example for jsmn - a lightweight JSON parse written in C, here: http://git.io/ndk-jsmn.
I've also a set of slides that cover this topic that you may find useful.
According to this article: What does a typical ./configure do in Linux?
configure only checks for system dependencies (if they are missing it exits), checks the type of system you are on, adn then creates a makefile. since you already have a makefile, you should already be able to execute make install.

Using libxml for Android

We have an application working fine for Mac and Windows platform, now targeting for Android and iOS,
Application using libxml for parsing of XML data, and my question is ?
1 -- Do i need to build libxml for Android platform or its already there as a part of NDK ?
if need to build any pointers how to start ?
You have two options:
Write an Android.mk that can successfully build libxml, and depend on the result of that in your main Android.mk. You can try to build libxml natively (using the configure script and make) on your Mac to get an idea of what files need to be built, and if any special flags need to be passed.
Or, Use libxml's configure script and build libxml for Android using the NDK's toolchain. You'll have to do something like this:
NDK_ROOT=/path/to/ndk
API_LEVEL=YOUR_API_LEVEL
SYSROOT=$NDK_ROOT/platforms/android-$API_LEVEL/arch-arm
export CC="$NDK_ROOT/toolchains/arm-linux-androideabi-4.4.3/Darwin-x86/bin/arm-linux-androideabi-gcc"
export CFLAGS="--sysroot=$SYSROOT"
export LDFLAGS="--sysroot=$SYSROOT"
./configure --host=arm-linux-gnueabi --enable-static --disable-shared
make libxml2.la
Technically the host triplet for Android is arm-linux-androideabi, but most configure scripts don't recognize it, and for our purposes here, -gnueabi is close enough.
I ended up suggesting make libxml2.la rather than just make because the tests weren't building properly. The resulting library will end up in the .libs/ folder.
The second method might be a little easier, but if you intend to build your app for multiple architectures (arm, armv7-a, maybe even x86 and mips), writing your own Android.mk will save you time in the long run.

Android GCC Makefile, want to build shared lib and linking in static lib, without "ndk-build"

I had some experience of android but know almost nothing of GCC Makefile. Here is what I need,
working on mac osx or linux, using android ndk toolchain, build an android shared library .so (a jni lib), linking in a prebuilt static library (already built for android). In static lib, I need logcat functions and C funcs such as sprintf. So no "ndk-build" in the process. The reason I have to do this, when linking in the static lib, a special linker is required to replace the standard arm-eabi-gcc, and the standard linker will be rolled back later.
I know that I need to set following environment variables such as:
ANDROID_NDK,
PATH=$PATH:"$ANDROID_NDK/build/prebuilt/darwin-x86/toolchain/arm-eabi-4.4.0/bin"
MACHINE=armv7l
SYSTEM=android
ARCH=arm
CROSS_COMPILE="arm-eabi-"
ANDROID_DEV="$ANDROID_NDK/platforms/android-8/arch-arm/usr"
HOSTCC=gcc
All I need is a sample Makefile, so that I can run "make" to build a .so by linking in a .a, and other necessary android and c libs.
Thanks
Try running ndk_build V=1 and recording the commands it issues in verbose mode. Then issue comparable commands to your custom linker.
Beware the commands might change for a different ndk version.

How to use C libraries from different path with GCC

I am using Android NDK GCC toolchain to compile a project as command-line.
My gcc location is
/opt/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc
And I am using the gcc option
--sysroot=/opt/android-ndk/platforms/android-8/arch-arm/
There is no problem when building Hello World. But for this project, during linking, it produces the error:
/opt/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld:
crtbegin_static.o: No such file: No such file or directory
Actually, the "crtbegin_static.o" is located in
/opt/android-ndk/platforms/android-8/arch-arm/usr/lib
If I put it to
/opt/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/lib/gcc/arm-linux-androideabi/4.4.3
then this problem can be solved.
Is there anyway to make gcc to locate the crtbegin_static.o correctly.
Finally, I have found the solution. It can be solved by passing the "--sysroot=" option again to the linker, not compiler only, using
-Wl,--sysroot=/opt/android-ndk/platforms/android-8/arch-arm/
There is a -L option which specifies the directories searched for -l.
http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
http://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html

Categories

Resources