Mastering Android NDK
上QQ阅读APP看书,第一时间看更新

Chapter 2. Native Libraries

In this chapter, you will learn how to build popular C/C++ libraries and link them against your applications using Android NDK. These libraries are building blocks to implement feature-rich applications with images, videos, sounds, physical simulations, and networking entirely in C++. We will provide minimal samples to demonstrate the functionality of each library. Audio and networking libraries are discussed in greater detail in the subsequent chapters. We will show you how to compile libraries and, of course, give some short samples and hints on how to start using them.

Typical caveats for porting libraries across different processors and operating systems are memory access (structure alignment and padding), byte order (endianness), calling conventions, and floating-point issues. All the libraries described in the preceding sections address these issues quite well, and even if some of them do not officially support Android NDK, fixing this is just a matter of a few compiler switches.

To build the mentioned libraries, we need to create makefiles for Windows, Linux, and OS X and a pair of the Android.mk/Application.mk files for the NDK. The source files of the library are compiled to object files. A collection of object files is combined into an archive, which is also called a static library. Later, this static library can be passed as an input to the linker. We start with the desktop versions, first for Windows.

To build the Windows-specific version of libraries, we need a C++ compiler. We will use the command line compiler from MinGW with the GCC toolchain described in Chapter 1, Using Command-line Tools. For each library, we have a collection of source code files, and we need to get the static library, a file with the .a extension.