文章持續更新!
每編譯一個第三方庫,更新一回,完整記錄如何使用NDK將第三方C/C++庫編譯成動/靜態連接庫。html
在以前一篇文章《在Linux下進行NDK編譯》,咱們配製了Linux(CentOS 7)的NDK編譯環境,強調了在Linux環境行進行NDK編譯的重要性,而且逐步安裝了必要的組件,在文章最後,咱們還以FFmpeg爲例,完成了一次編譯。在這篇文章中,咱們繼續深刻細節去探討:linux
H.264是ITU(International Telecommunication Union,國際通訊聯盟)和MPEG(Motion Picture Experts Group,運動圖像專家組)聯合制定的視頻編碼標準。而x264是一個開源的[H.264/MPEG-4 AVC](baike.baidu.com/item/H.264%… AVC)視頻編碼函數庫,是最好的有損視頻編碼器之一。在FFmpeg中一樣實現了H.264的編碼,同時FFmpeg也可以集成X264。本次咱們將直接使用X264來進行視 頻編碼而不是FFmpeg。android
默認已經在Linux環境下配製好了相關組件,詳見在Linux下進行NDK編譯。git
進入你但願將源碼存放的位置,我選擇個人download文件夾;shell
cd download
複製代碼
開始git clone;vim
git clone https://code.videolan.org/videolan/x264.git
複製代碼
完成後進入x264文件夾,並查看文件夾內容;windows
cd x264
ll
複製代碼
文件夾內容以下所示。bash
[tongbo@localhost x264]$ ll
總用量 356
-rw-rw-r--. 1 tongbo tongbo 1998 12月 26 17:17 AUTHORS
-rw-rw-r--. 1 tongbo tongbo 10589 12月 26 17:17 autocomplete.c
drwxrwxr-x. 8 tongbo tongbo 4096 12月 26 17:17 common
-rwxrwxr-x. 1 tongbo tongbo 45003 12月 26 17:17 config.guess
-rwxrwxr-x. 1 tongbo tongbo 35758 12月 26 17:17 config.sub
-rwxrwxr-x. 1 tongbo tongbo 51871 12月 26 17:17 configure
-rw-rw-r--. 1 tongbo tongbo 17992 12月 26 17:17 COPYING
drwxrwxr-x. 2 tongbo tongbo 111 12月 26 17:17 doc
drwxrwxr-x. 2 tongbo tongbo 4096 12月 26 17:17 encoder
-rw-rw-r--. 1 tongbo tongbo 4172 12月 26 17:17 example.c
drwxrwxr-x. 2 tongbo tongbo 171 12月 26 17:17 extras
drwxrwxr-x. 3 tongbo tongbo 53 12月 26 17:17 filters
drwxrwxr-x. 2 tongbo tongbo 137 12月 26 17:17 input
-rw-rw-r--. 1 tongbo tongbo 13124 12月 26 17:17 Makefile
drwxrwxr-x. 2 tongbo tongbo 193 12月 26 17:17 output
drwxrwxr-x. 3 tongbo tongbo 273 12月 26 17:17 tools
-rwxrwxr-x. 1 tongbo tongbo 854 12月 26 17:17 version.sh
-rw-rw-r--. 1 tongbo tongbo 91346 12月 26 17:17 x264.c
-rw-rw-r--. 1 tongbo tongbo 3182 12月 26 17:17 x264cli.h
-rw-rw-r--. 1 tongbo tongbo 1910 12月 26 17:17 x264dll.c
-rw-rw-r--. 1 tongbo tongbo 47459 12月 26 17:17 x264.h
-rw-rw-r--. 1 tongbo tongbo 2656 12月 26 17:17 x264res.rc
複製代碼
注意!並非全部的庫都已經存在configure文件,之種狀況請首先閱讀庫的Readme文檔。有的庫可能只存在configure.ac和makefile.am。這種狀況須要藉助autoconf命令來生成configure。大概的流程以下 (後綴*表明執行過程,[]表明可選項),詳見Autoconf簡介。app
your source files --> [autoscan*] --> [configure.scan] --> configure.ac
configure.ac --·
| ·------> autoconf* -----> configure
[aclocal.m4] --+---+
| ·------> [autoheader*] --> [config.h.in]
[acsite.m4] ---·
Makefile.in
複製代碼
總之,在獲得configure文件以後,執行:cors
./configure --help
複製代碼
查看該配置文件開放的參數詳情,x264庫configure的參數狀況以下所示。
[tongbo@localhost x264]$ ./configure --help
Usage: ./configure [options]
Help:
-h, --help print this message
Standard options:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]
--bindir=DIR install binaries in DIR [EPREFIX/bin]
--libdir=DIR install libs in DIR [EPREFIX/lib]
--includedir=DIR install includes in DIR [PREFIX/include]
--extra-asflags=EASFLAGS add EASFLAGS to ASFLAGS
--extra-cflags=ECFLAGS add ECFLAGS to CFLAGS
--extra-ldflags=ELDFLAGS add ELDFLAGS to LDFLAGS
--extra-rcflags=ERCFLAGS add ERCFLAGS to RCFLAGS
Configuration options:
--disable-cli disable cli
--system-libx264 use system libx264 instead of internal
--enable-shared build shared library
--enable-static build static library
--disable-opencl disable OpenCL features
--disable-gpl disable GPL-only features
--disable-thread disable multithreaded encoding
--disable-win32thread disable win32threads (windows only)
--disable-interlaced disable interlaced encoding support
--bit-depth=BIT_DEPTH set output bit depth (8, 10, all) [all]
--chroma-format=FORMAT output chroma format (400, 420, 422, 444, all) [all]
Advanced options:
--disable-asm disable platform-specific assembly optimizations
--enable-lto enable link-time optimization
--enable-debug add -g
--enable-gprof add -pg
--enable-strip add -s
--enable-pic build position-independent code
Cross-compilation:
--host=HOST build programs to run on HOST
--cross-prefix=PREFIX use PREFIX for compilation tools
--sysroot=SYSROOT root of cross-build tree
External library support:
--disable-avs disable avisynth support
--disable-swscale disable swscale support
--disable-lavf disable libavformat support
--disable-ffms disable ffmpegsource support
--disable-gpac disable gpac support
--disable-lsmash disable lsmash support
[tongbo@localhost x264]$
複製代碼
__parameter_names | __parameter_introduction |
---|---|
--prefix | 通常規範的配置腳本都會存在這個參數,用於設置編譯結果的輸出目錄; |
--extra-cflags | 通常形如CFlags的參數,都是對編譯器設置參數的,好比「-L」參數和「-I」參數; |
--disable-cli | cli是命令行界面(Command-Line Interface, CLI)的意思,因此這個配置是關閉編譯命令行工具,咱們在Android中使用,是本身編寫代碼,而不是藉助命令行工具,因此能夠加上這個配置(不加也沒事,編譯出來不用就好了); |
--enable-shared | 是否生成動態連接庫; |
--enable-static | 是否生成靜態連接庫; |
--enable-debug | 爲編譯器添加「-g」參數; |
--enable-gprof | 爲編譯器添加「-pg」參數; |
--enable-strip | 爲編譯器添加「-s」參數; |
--enable-pic | 爲編譯器添加「-fPIC」參數,這裏咱們記住一點:若是編譯Android使用的動態庫,則使用PIC;若是編譯Android可用的命令行工具,使用PIE。 |
[tongbo@localhost x264]$ touch build_bash.sh
[tongbo@localhost x264]$ vim build_bash.sh
複製代碼
#!/bin/bash
#NDK目錄,使用以前環境變量中配置的NDKROOT
NDK_ROOT=$NDKROOT
#編譯後安裝位置 pwd表示當前目錄
PREFIX=`pwd`/android/armeabi-v7a
#目標平臺版本,咱們將兼容到android-21
API=21
#編譯工具鏈目錄
TOOLCHAIN=$NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64
#小技巧,建立一個AS的NDK工程,執行編譯,而後在 app/.cxx/cmake/debug(release)/本身要編譯的平臺/ 目錄下本身觀察 build.ninja與 rules.ninja
FLAGS="--target=armv7-none-linux-androideabi21 --gcc-toolchain=$TOOLCHAIN -g -DANDROID -fdatasections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -march=armv7-a -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -Oz -DNDEBUG -fPIC "
#雖然x264提供了交叉編譯配置:--cross-prefix,如--corss-prefix=/NDK/arm-linux-androideabi-
#那麼則會使用 /NDK/arm-linux-androideabi-gcc 來編譯
#然而ndk19開始gcc已經被移除,由clang替代。
#小常識:通常的庫都會使用$CC 變量來保存編譯器,咱們本身設置CC變量的值爲clang。
export CC=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang
export CXX=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++
#--extra-cflag會附加到CFLAGS 變量以後,做爲傳遞給編譯器的參數,因此就算有些庫沒有--extra-cflags配置,咱們也能夠本身建立變量CFLAGS傳參。
./configure \
--prefix=$PREFIX \
--disable-cli --enable-static \
--enable-pic \
--host=arm-linux \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--sysroot=$TOOLCHAIN/sysroot \
--extracflags="$FLAGS"
make install
複製代碼
[tongbo@localhost x264]$ chmod +x build_bash.sh
[tongbo@localhost x264]$ ./build_bash.sh
複製代碼
[tongbo@localhost x264]$ cd android
[tongbo@localhost android]$ sudo cp -r armeabi-v7a /samba
複製代碼
FAAC is an open-source MPEG-4 and MPEG2 AAC audio encoder. Note that the quality of FAAC is not up to par with the currently best AAC encoders available.
Features:
[tongbo@localhost download]$ wget https://nchc.dl.sourceforge.net/project/faac/faac-src/faac-1.29/faac-1.29.9.2.tar.gz
複製代碼
[tongbo@localhost download]$ tar xvf faac-1.29.9.2.tar.gz
複製代碼
[tongbo@localhost download]$ cd faac-1.29.9.2
複製代碼
[tongbo@localhost faac-1.29.9.2]$ ll
總用量 1512
-rw-r--r--. 1 tongbo tongbo 373964 11月 14 2017 aclocal.m4
-rw-r--r--. 1 tongbo tongbo 1297 9月 2 2017 AUTHORS
-rw-r--r--. 1 tongbo tongbo 2278 11月 3 2017 ChangeLog
drwxr-xr-x. 2 tongbo tongbo 44 11月 14 2017 common
-rwxr-xr-x. 1 tongbo tongbo 7333 9月 17 2017 compile
-rwxr-xr-x. 1 tongbo tongbo 43940 11月 13 2016 config.guess
-rw-r--r--. 1 tongbo tongbo 3530 11月 14 2017 config.h.in
-rwxr-xr-x. 1 tongbo tongbo 36339 11月 13 2016 config.sub
-rwxr-xr-x. 1 tongbo tongbo 603816 11月 14 2017 configure
-rw-r--r--. 1 tongbo tongbo 1691 11月 14 2017 configure.ac
-rw-r--r--. 1 tongbo tongbo 25292 7月 5 2017 COPYING
-rwxr-xr-x. 1 tongbo tongbo 23567 9月 17 2017 depcomp
drwxr-xr-x. 2 tongbo tongbo 20 11月 14 2017 docs
drwxr-xr-x. 2 tongbo tongbo 224 11月 14 2017 frontend
drwxr-xr-x. 2 tongbo tongbo 75 11月 14 2017 include
-rw-r--r--. 1 tongbo tongbo 757 9月 2 2017 INSTALL
-rwxr-xr-x. 1 tongbo tongbo 15155 9月 17 2017 install-sh
drwxr-xr-x. 3 tongbo tongbo 4096 11月 14 2017 libfaac
-rw-r--r--. 1 tongbo tongbo 324412 8月 20 2016 ltmain.sh
-rw-r--r--. 1 tongbo tongbo 42 9月 23 2017 Makefile.am
-rw-r--r--. 1 tongbo tongbo 25303 11月 14 2017 Makefile.in
-rwxr-xr-x. 1 tongbo tongbo 6872 9月 17 2017 missing
-rw-r--r--. 1 tongbo tongbo 196 11月 2 2017 NEWS
-rw-r--r--. 1 tongbo tongbo 3031 7月 24 2017 README
-rw-r--r--. 1 tongbo tongbo 274 11月 10 2017 TODO
[tongbo@localhost faac-1.29.9.2]$
複製代碼
[tongbo@localhost faac-1.29.9.2]$ ./configure --help
`configure' configures faac 1.29.9.2 to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
-h, --help display this help and exit
--help=short display options specific to this package
--help=recursive display the short help of all the included packages
-V, --version display version information and exit
-q, --quiet, --silent do not print `checking ...' messages
--cache-file=FILE cache test results in FILE [disabled]
-C, --config-cache alias for `--cache-file=config.cache'
-n, --no-create do not create output files
--srcdir=DIR find the sources in DIR [configure dir or `..']
Installation directories:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]
By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc. You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.
For better control, use the options below.
Fine tuning of the installation directories:
--bindir=DIR user executables [EPREFIX/bin]
--sbindir=DIR system admin executables [EPREFIX/sbin]
--libexecdir=DIR program executables [EPREFIX/libexec]
--sysconfdir=DIR read-only single-machine data [PREFIX/etc]
--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
--localstatedir=DIR modifiable single-machine data [PREFIX/var]
--runstatedir=DIR modifiable per-process data [LOCALSTATEDIR/run]
--libdir=DIR object code libraries [EPREFIX/lib]
--includedir=DIR C header files [PREFIX/include]
--oldincludedir=DIR C header files for non-gcc [/usr/include]
--datarootdir=DIR read-only arch.-independent data root [PREFIX/share]
--datadir=DIR read-only architecture-independent data [DATAROOTDIR]
--infodir=DIR info documentation [DATAROOTDIR/info]
--localedir=DIR locale-dependent data [DATAROOTDIR/locale]
--mandir=DIR man documentation [DATAROOTDIR/man]
--docdir=DIR documentation root [DATAROOTDIR/doc/faac]
--htmldir=DIR html documentation [DOCDIR]
--dvidir=DIR dvi documentation [DOCDIR]
--pdfdir=DIR pdf documentation [DOCDIR]
--psdir=DIR ps documentation [DOCDIR]
Program names:
--program-prefix=PREFIX prepend PREFIX to installed program names
--program-suffix=SUFFIX append SUFFIX to installed program names
--program-transform-name=PROGRAM run sed PROGRAM on installed program names
System types:
--build=BUILD configure for building on BUILD [guessed]
--host=HOST cross-compile to build programs to run on HOST [BUILD]
Optional Features:
--disable-option-checking ignore unrecognized --enable/--with options
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]
--enable-silent-rules less verbose build output (undo: "make V=1")
--disable-silent-rules verbose build output (undo: "make V=0")
--enable-drm Digital Radio Mondiale support
--enable-dependency-tracking
do not reject slow dependency extractors
--disable-dependency-tracking
speeds up one-time build
--enable-shared[=PKGS] build shared libraries [default=yes]
--enable-static[=PKGS] build static libraries [default=yes]
--enable-fast-install[=PKGS]
optimize for fast installation [default=yes]
--disable-libtool-lock avoid locking (might break parallel builds)
--disable-largefile omit support for large files
Optional Packages:
--with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
--without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
--with-pic[=PKGS] try to use only PIC/non-PIC objects [default=use
both]
--with-aix-soname=aix|svr4|both
shared library versioning (aka "SONAME") variant to
provide on AIX, [default=aix].
--with-gnu-ld assume the C compiler uses GNU ld [default=no]
--with-sysroot[=DIR] Search for dependent libraries within DIR (or the
compiler's sysroot if not specified).
Some influential environment variables:
CC C compiler command
CFLAGS C compiler flags
LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
nonstandard directory <lib dir>
LIBS libraries to pass to the linker, e.g. -l<library>
CPPFLAGS (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
you have headers in a nonstandard directory <include dir>
CXX C++ compiler command
CXXFLAGS C++ compiler flags
LT_SYS_LIBRARY_PATH
User-defined run-time library search path.
CPP C preprocessor
CXXCPP C++ preprocessor
Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.
Report bugs to <https://sourceforge.net/p/faac/bugs/>.
[tongbo@localhost faac-1.29.9.2]$
複製代碼
[tongbo@localhost faac-1.29.9.2]$ touch build_bash.sh
[tongbo@localhost faac-1.29.9.2]$ vim build_bash.sh
複製代碼
#!/bin/bash
#NDK目錄,使用以前環境變量中配置的NDKROOT
NDK_ROOT=$NDKROOT
#編譯後安裝位置 pwd表示當前目錄
PREFIX=`pwd`/android/armeabi-v7a
#目標平臺版本,咱們將兼容到android-21
API=21
#編譯工具鏈目錄
TOOLCHAIN=$NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64
#小技巧,建立一個AS的NDK工程,執行編譯,而後在 app/.cxx/cmake/debug(release)/本身要編譯的平臺/ 目錄下本身觀察 build.ninja與 rules.ninja
FLAGS="--target=armv7-none-linux-androideabi21 --gcc-toolchain=$TOOLCHAIN -g -DANDROID -fdatasections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -march=armv7-a -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -Oz -DNDEBUG -fPIC "
#雖然x264提供了交叉編譯配置:--cross-prefix,如--corss-prefix=/NDK/arm-linux-androideabi-
#那麼則會使用 /NDK/arm-linux-androideabi-gcc 來編譯
#然而ndk19開始gcc已經被移除,由clang替代。
#小常識:通常的庫都會使用$CC 變量來保存編譯器,咱們本身設置CC變量的值爲clang。
export CC=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang
export CXX=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++
./configure --prefix=$PREFIX \
--enable-static=yes \
--enable-shared=no \
--host=arm-linux
make install
複製代碼
[tongbo@localhost faac-1.29.9.2]$ chmod +x build_bash.sh
複製代碼
[tongbo@localhost faac-1.29.9.2]$ ./build_bash.sh
複製代碼
[tongbo@localhost faac-1.29.9.2]$ cd android
[tongbo@localhost android]$ ll
總用量 0
drwxrwxr-x. 6 tongbo tongbo 56 12月 28 21:32 armeabi-v7a
[tongbo@localhost android]$ cd armeabi-v7a
[tongbo@localhost armeabi-v7a]$ ll
總用量 0
drwxrwxr-x. 2 tongbo tongbo 18 12月 28 21:32 bin
drwxrwxr-x. 2 tongbo tongbo 37 12月 28 21:32 include
drwxrwxr-x. 2 tongbo tongbo 41 12月 28 21:32 lib
drwxrwxr-x. 3 tongbo tongbo 17 12月 28 21:32 share
[tongbo@localhost armeabi-v7a]$ cd lib
[tongbo@localhost lib]$ ll
總用量 84
-rw-r--r--. 1 tongbo tongbo 80264 12月 28 21:32 libfaac.a
-rwxr-xr-x. 1 tongbo tongbo 928 12月 28 21:32 libfaac.la
[tongbo@localhost lib]$
複製代碼
[tongbo@localhost android]$ mkdir faac
[tongbo@localhost android]$ cp -r armeabi-v7a faac
[tongbo@localhost android]$ cp -r faac /samba
複製代碼