Android:交叉编译总结

总结一下交叉编译

如何用交叉编译链命令行编译

基本通过下面两句可以完成helloworld编译,参考(本方法已经过时了,请不要使用,要使用独立编译链的方法)https://www.cnblogs.com/bingghost/p/5721423.html

/home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc  -I/home/liwenfeng/work/android-ndk-r13b/platforms/android-24/arch-arm/usr/include  -c hello.c -o hello.o
/home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc  -L/home/liwenfeng/work/android-ndk-r13b/platforms/android-24/arch-arm/usr/lib -nostdlib -fPIE -pie -lc hello.o /home/liwenfeng/work/android-ndk-r13b/platforms/android-24/arch-arm/usr/lib/crtbegin_dynamic.o /home/liwenfeng/work/android-ndk-r13b/platforms/android-24/arch-arm/usr/lib/crtend_android.o  -o hello

交叉编译链的默认配置是怎样的?

echo 'main(){}'|arm-linux-androideabi-gcc -E -v -
Using built-in specs.
COLLECT_GCC=arm-linux-androideabi-gcc
Target: arm-linux-androideabi
Configured with: /usr/local/google/buildbot/src/android/gcc/toolchain/build/../gcc/gcc-4.9/configure --prefix=/tmp/59719db9ae19ff43aef46bbcb79596b6 --target=arm-linux-androideabi --host=x86_64-linux-gnu --build=x86_64-linux-gnu --with-gnu-as --with-gnu-ld --enable-languages=c,c++ --with-gmp=/buildbot/tmp/build/toolchain/temp-install --with-mpfr=/buildbot/tmp/build/toolchain/temp-install --with-mpc=/buildbot/tmp/build/toolchain/temp-install --with-cloog=/buildbot/tmp/build/toolchain/temp-install --with-isl=/buildbot/tmp/build/toolchain/temp-install --with-ppl=/buildbot/tmp/build/toolchain/temp-install --disable-ppl-version-check --disable-cloog-version-check --disable-isl-version-check --enable-cloog-backend=isl --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --disable-libssp --enable-threads --disable-nls --disable-libmudflap --disable-libgomp --disable-libstdc__-v3 --disable-sjlj-exceptions --disable-shared --disable-tls --disable-libitm --with-float=soft --with-fpu=vfp --with-arch=armv5te --enable-target-optspace --enable-bionic-libs --enable-libatomic-ifuncs=no --enable-initfini-array --disable-nls --prefix=/tmp/59719db9ae19ff43aef46bbcb79596b6 --with-sysroot=/tmp/59719db9ae19ff43aef46bbcb79596b6/sysroot --with-binutils-version=2.25 --with-mpfr-version=3.1.1 --with-mpc-version=1.0.1 --with-gmp-version=5.0.5 --with-gcc-version=4.9 --with-gdb-version=none --with-gxx-include-dir=/tmp/59719db9ae19ff43aef46bbcb79596b6/include/c++/4.9.x --with-bugurl=http://source.android.com/source/report-bugs.html --enable-languages=c,c++ --disable-bootstrap --enable-plugins --enable-libgomp --enable-gnu-indirect-function --disable-libsanitizer --enable-gold --enable-threads --enable-eh-frame-hdr-for-static --enable-graphite=yes --with-isl-version=0.11.1 --with-cloog-version=0.18.0 --with-arch=armv5te --program-transform-name='s&^&arm-linux-androideabi-&' --enable-gold=default
Thread model: posix
gcc version 4.9.x 20150123 (prerelease) (GCC) 
COLLECT_GCC_OPTIONS='-E' '-v' '-march=armv5te' '-mfloat-abi=soft' '-mfpu=vfp' '-mtls-dialect=gnu'
 /home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../libexec/gcc/arm-linux-androideabi/4.9.x/cc1 -E -quiet -v -iprefix /home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/ - -mbionic -fpic -march=armv5te -mfloat-abi=soft -mfpu=vfp -mtls-dialect=gnu
ignoring nonexistent directory "/home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/include"
ignoring duplicate directory "/home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/../../lib/gcc/arm-linux-androideabi/4.9.x/include"
ignoring nonexistent directory "/tmp/59719db9ae19ff43aef46bbcb79596b6/sysroot/usr/local/include"
ignoring duplicate directory "/home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/../../lib/gcc/arm-linux-androideabi/4.9.x/include-fixed"
ignoring nonexistent directory "/home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/../../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/include"
ignoring nonexistent directory "/tmp/59719db9ae19ff43aef46bbcb79596b6/sysroot/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/include
 /home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/include-fixed
End of search list.
# 1 ""
# 1 ""
# 1 ""
# 1 ""
main(){}
COMPILER_PATH=/home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../libexec/gcc/arm-linux-androideabi/4.9.x/:/home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../libexec/gcc/:/home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/
LIBRARY_PATH=/home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/:/home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/:/home/liwenfeng/work/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/
COLLECT_GCC_OPTIONS='-E' '-v' '-march=armv5te' '-mfloat-abi=soft' '-mfpu=vfp' '-mtls-dialect=gnu'

如何选择stl头文件的路径?

选择gnustl会报错如下,去路径看了下,的确没有这个问题,没得搞:

/home/liwenfeng/work/android-ndk-r13b/sources/cxx-stl/gnu-libstdc++/4.9/include/string:38:28: fatal error: bits/c++config.h: No such file or directory
 #include 

用llvm的头文件就会报下面的错

In file included from /home/liwenfeng/work/android-ndk-r13b/sources/cxx-stl/llvm-libc++/include/complex:246:0,
                 from ../../webrtc/common_audio/lapped_transform.h:14,
                 from lapped_transform.cc:11:
/home/liwenfeng/work/android-ndk-r13b/sources/cxx-stl/llvm-libc++/include/cmath:313:9: error: '::signbit' has not been declared
 using ::signbit;

Bionic C与 Glibc的区别?

Bionic C是Android搞得,为了不开源和低端cpu的优化。占用体积更小。

gcc可以编译cpp吗?

可以,但不能自动链接cpp的库。g++实际是调用了gcc的。g++是gcc的超集。

如何使用交叉编译链和makefile编译helloworld


NDK_ROOT=/home/liwenfeng/work/android-ndk-r13b

TOOLCHAINS_ROOT=$(NDK_ROOT)/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64

TOOLCHAINS_PREFIX=$(TOOLCHAINS_ROOT)/bin/arm-linux-androideabi

TOOLCHAINS_INCLUDE=$(TOOLCHAINS_ROOT)/lib/gcc/arm-linux-androideabi/4.9.x/include-fixed

PLATFROM_ROOT=$(NDK_ROOT)/platforms/android-24/arch-arm

PLATFROM_INCLUDE=$(PLATFROM_ROOT)/usr/include

PLATFROM_LIB=$(PLATFROM_ROOT)/usr/lib

MODALE_NAME=hello

RM=del

FLAGS=-I$(TOOLCHAINS_INCLUDE) \
      -I$(PLATFROM_INCLUDE)   \
      -L$(PLATFROM_LIB) \
      -nostdlib \
      -fPIE \
      -pie \
      -lgcc \
      -Bdynamic \
      -lc

OBJS=$(MODALE_NAME).o \
     $(PLATFROM_LIB)/crtbegin_dynamic.o \
     $(PLATFROM_LIB)/crtend_android.o 

all:
    $(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -c $(MODALE_NAME).c -o $(MODALE_NAME).o
    $(TOOLCHAINS_PREFIX)-gcc $(FLAGS) $(OBJS) -o $(MODALE_NAME)

clean:
    $(RM) *.o

如何使用独立工具链编译c++

arm-linux-androideabi-g++ --sysroot=/home/liwenfeng/work/StandaloneTool/sysroot -v -pie -fPIE  hello_world.cpp -o hello_world

有什么可以编译成功的库吗

有,如下
https://freedesktop.org/software/pulseaudio/webrtc-audio-processing/
配置方法:

./configure --host=arm-linux-androideabi CFLAGS="--sysroot=/home/liwenfeng/work/StandaloneToolStlport/sysroot"

如何创建独立的编译链

//gnust
python make_standalone_toolchain.py  --arch arm  --api 24 --stl gnustl  --install-dir /home/liwenfeng/work/StandaloneTool --force

//stlport
python make_standalone_toolchain.py  --arch arm  --api 24 --stl stlport --install-dir /home/liwenfeng/work/StandaloneToolStlport --force

如何单独使用so库

#include 
#include "webrtc/modules/audio_processing/include/audio_processing.h"
using namespace std;
using namespace webrtc;
using namespace rtc;
int main() {
   webrtc::AudioProcessing::Create();
   return 0;
}


arm-linux-androideabi-g++ --sysroot=/home/liwenfeng/work/StandaloneToolStlport/sysroot -I/home/liwenfeng/work/nano/webrtc-audio-processing-0.3.1/install/include/webrtc_audio_processing -DWEBRTC_POSIX  HelloAudio.cpp -L. -lwebrtc_audio_processing -o HelloAudio

如何同时链接静态库和动态库

参考下面,是同时链接了webrtc_audio_processing静态库和log动态库

 arm-linux-androideabi-g++ --sysroot=/home/liwenfeng/work/StandaloneToolStlport/sysroot -I/home/liwenfeng/work/nano/webrtc-audio-processing-0.3.1/install/include/webrtc_audio_processing -DWEBRTC_POSIX -std=c++11 -Fpie -pie  HelloAudio.cpp -L. -Wl,-Bstatic -lwebrtc_audio_processing -Wl,-Bdynamic -llog -o HelloAudio

因为log库只有动态库,所以必须使用-Wl,-Bdynamic -llog来链接,否则会提示找不到log库,报错如下

/home/liwenfeng/work/StandaloneToolStlport/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -llog
/home/liwenfeng/work/StandaloneToolStlport/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -ldl

如何打包静态库并且使用?

下面是静态库,打包成动态库,然后再使用的例子
demo.c

#include "demo.h"
#include 
#define  LOG_TAG    "fengtou"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
int demoSay() {
  LOGI("121");
  return 1;
}

main.c

#include "demo.h"
#include 

int main() {
  demoSay();
  printf(" qu ni mei de\n");
  return 1;
}

命令行:

编译.o
arm-linux-androideabi-gcc --sysroot=/home/liwenfeng/work/StandaloneToolStlport/sysroot  -c demo.cpp -o demo.o

打包.a
arm-linux-androideabi-ar   -rc libdemo.a demo.o

生产可执行文件
arm-linux-androideabi-gcc --sysroot=/home/liwenfeng/work/StandaloneToolStlport/sysroot  main.cpp -L. -Wl,-Bstatic -ldemo -Wl,-Bdynamic  -llog -pie -fPIE   -o main

运行结果如下两图


image.png
image.png

动态库链接在什么阶段完成?

你可能感兴趣的:(Android:交叉编译总结)