CMake : FIND_PACKAGE(Threads) for Android cross-compilation - android

I'm using the Android NDK and Cmake to generate shared libraries of my project.
I'm porting an existing project from Ubuntu to Android, and right now I need to port some executables files. I compile successfully all the executable file sexecpt one which needs the Threads library.
In CMakeList.txt, there is FIND_PACKAGE(Threads) which finds the library while compiling for Ubuntu, but not for Android.
I followed this cmake and libpthread but with no success.
I think I should write the FindThread.cmake file but I'm pretty new to CMake and don't really know how to do it, especially as I don't know where is located the thread library for Android.
Any help would be appreciated.
Thank you

Under Android, there is no need for FIND_PACKAGE(Threads) because the Android libc standard library (called "bionic") already includes the relevant functions in threads.h and pthread.h. That support is not yet completely POSIX compliant and differs by API level, but will be sufficient for most cases.
So you can just skip over finding and linking to an external threads library. If your CMake file is meant for cross-platform use, it would look like this:
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Android")
find_package(Threads REQUIRED)
target_link_libraries (your-target-name PRIVATE Threads::Threads)
endif()

You don't need to write your own FindThread.cmake. On a standard linux installation, it can be found within /usr/share/cmake-2.8/Modules/ .
Check where this Modules/ directory might be installed on your platform.

Related

Compile c++ for android/iOS

I have to compile a library (library BPG from Bellard.org) to create a .so or a dll that I can use with android/iOS.
I'm working with Visual Studio. With some researches, I found the project "Visual C++ -> Cross Platform -> Shared Library (Android, iOS)". But I am totally lost and can't do anything.
The downloaded library is organised with some folders but Visual don't allow to make tree, all files are sorted by filters (one for header and one for sources). So I can't build, I have more than 300 errors, "can't open source file", "undefined variable"...
Secondly, the README file from project says :
The following packages need to be installed: mingw64-gcc mingw64-libpng mingw64-libjpeg-turbo mingw64-SDL mingw64-SDL_image yasm
I found installed for mingw 32 bits but no 64 bits so I don't know if build can perform. I don't know how to find the libraries.
So my question is, what is the best way to compile a C/C++ library for android/iOS ? And where can I find a tutorial for beginners ?
Thank you
I have worked as cross compiling engineer for several years. The most suitable IDE for you I think, is the CLion with CMake inside.
CMake is a tool which can cross-compile the C/C++ library into ios\android\linux\etc.. using only one config file: "CMakeList.txt".
The main task of CMake is to translate CMakeList.txt to Makefile on every platform and provide you the .a and .so files.
CLion is very powerful IDE in code editing and debugging.
Furthermore, Android needs JNI (or JNA if performance is not concerned) to wrap your c++ interfaces to java classes. Here I would recommend SWIG. SWIG is a tool to wrap C++ interfaces to other languages, that means, not only java on android you can support , other days your lib can also support python\tcl\Go\etc.
Which os are u using to build the lib? macOS or win ?
For iOS : .a file
For Android: .so file
First you should check the README file
Edit the Makefile to change the compile options (the default
compile options should be OK). Type 'make' to compile and 'make
install' to install the compiled binaries.
Use 'make -j N' where N is the number of CPU cores to compile faster.
The following packages must be installed: SDL-devel
SDL_image-devel yasm. It is recommended to use yasm version >= 1.3.0
to have a faster compilation.
Only a 64 bit target is supported because x265 needs it for bit
depths > 8.
check this to install SDL packages
https://wiki.libsdl.org/Installation

How to Build CMake based project for Android/iOS using QtCreator

I have created a Non-Qt C++ (CMake) project using Qt, and I am able to build it using MinGW, MSVC compiler.
So in short, when I am opening my test project I can select the generators under Run CMake Window. I have issue with other platforms.
When I am trying to Add a kit for Android, there are no generators available in the list. I tried the same thing on Macbook, there also the same problem.
I need help on this issue, I couldn't get the proper steps to build the CMake based project for Android/iOS using QtCreator.
P.S. Installed CMake version is 3.2.1 and Qt Version is 5.5, I have installed Android SDK, NDK , and and Java
I would love to tell you "just check this box in the options dialog and it will work", but, unfortunately, there is no generator that you can use to build an Android/iOS project from a CMakeLists.txt file.
I found alternatives, in all cases, I don't think your have a chance to port a whole huge CMake-based project that used to work on Windows (with lots of libraries and 3rd party libraries) work on Android in less than a few days of work....;-)
Personnaly, I wrote a small CMake function generating the .pro file manually from my CMake scripts. It started small but is now huge and it's difficult to share it with you. But, basically, I started from this post on a Qt forum. It creates a simple .pro file that does not work that bad and then you can extend it for your own needs. I like this solution because I have full control on generated .pro file (mine can now build on PC, Android and iOS...but I had a hard time to have this fully work).
Apparently, there's also a way to add a Qt-android CMake support using this open source stuff: https://github.com/LaurentGomila/qt-android-cmake. But I never tried it. You may want to have a look to it. If it works, it may be more convenient that writting your own script generating .pro files. Apparently, it builds an Android apk using androidqtdeploy but without using QtCreator. There's also an iOS support.
Finally, the best may be to have CMake propose a QtCreator "generator" (it would generate .pro files, like CMake generates sln/vcproj files when using Visual Studio generator or makefiles for g++ generator....), but there is no such generator supported. I reported this to CMake team some time ago hoping they could fix that. I understood that there was and would be no plan to do that because CMake targets only "compilers" as "generators" and "QtCreator" is not really a "compiler", it's a "IDE" using external "compilers" to build (MinGW, MSVC, CLang, Android's NDK g++...). It's a shame because CMake known all your project information and could easily generate a .pro file....so, as, CMake is opensource, one may extend CMake with a custom QtCreator file generator...and share it with the whole world,it would be wonderful!
Hope this will help you!

How to build from src to binary for Android

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.

Building/finding shared libraries from Android source code

I wish to back port the Android RTP APIs introduced in version 3.1(Honeycomb) to earlier versions. I downloaded the source of version 4.0 and found that it these APIs had both java and native code. In order to build the native code with the NDK, certain shared libraries are required.
According the Android.mk file, these are libnativehelper, libcutils, libutils, and libmedia. Though the source of all of these are present in the source code, building them was difficult. Each required many other shared libraries. For eg, libmedia requires these shared libraries: libui, libcutils, libutils, libbinder, libsonivox, libicuuc, libexpat, libcamera_client, libstagefright_foundation, libgui and libdl.
So my question is, is there some way of obtaining the original 4 shared libs? Does it involve building the entire source?
Say I need to build a piece of native code which is going to use standard Android shared libraries such as libutils, libcutlis, libmedia. I would perform following steps:
Install AOSP repository with target version.
Add my source code to appropriate directories under ./frameworks/base. In your case it might be easier to create a separate folder and put proper Android.mk of course.
You might get compile errors if required functions from those standard shared libraries are not present in the previous version.
When you build the code as part of AOSP it will build required libraries and link them for you automatically.
P.S. To accomplish that you're better to use a Linux-based build host.
using cygwin terminal, build native part i.e. jni folder. To build using cygwin, goto jni folder using cygdrive command. Then type ndk-build. After successful completion, shared libraries i.e. .so files will be created in libs folder.
I can understand your problem, you can pull the libraries from /system/lib of device or emulator. But you need a system permission. But you can do it by installing application.
Otherwise build your source code on linux platfor. Building process is very easy, just using 2 or 3 command. First time it is needed long time to build. After that you need very short time to build, it will build only according to the timestamp of modified code.
Please have a look here

Simplest way to compile an autotools based shared C library for Android?

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.

Categories

Resources