gcc交叉编译时设置了“--sysroot“会产生哪些影响

gcc交叉编译时设置了"–sysroot"会产生哪些影响

在做交叉编译的时候,常常涉及到一个gcc编译选项--sysroot,这个选项是用来设置目标平台根目录的。--sysroot选项的官方说明如下

--sysroot=dir
Use dir as the logical root directory for headers and libraries. For example, if the compiler normally searches for headers in /usr/include and libraries in /usr/lib, it instead searches dir/usr/include and dir/usr/lib.

If you use both this option and the -isysroot option, then the --sysroot option applies to libraries, but the -isysroot option applies to header files.

The GNU linker (beginning with version 2.16) has the necessary support for this option. If your linker does not support this option, the header file aspect of --sysroot still works, but the library aspect does not.

--sysroot的说明可以看出,其会对编译和链接过程中,查找头文件和链接库造成影响。

例如:
原本默认会从/usr/include目录中搜索头文件、从/usr/lib中搜索依赖库,
当设置了--sysroot=dir后则会从dir/usr/include搜索头文件、从dir/usr/lib中搜索依赖库。

未设置--sysroot 设置了--sysroot=dir
头文件搜索路径 /usr/include dir/usr/include
依赖库搜索路径 /usr/lib dir/usr/lib

通过gcc -print-search-dirs查看默认动态库搜索路径

$ ./aarch64-linux-gnu-gcc --sysroot=/home/admin/tx2-rootfs -print-search-dirs |
 grep libraries | sed 's/libraries: =//g' | tr ':' '\n' | xargs readlink -f
/usr/local/lib/linaro-7.3.1/lib/gcc/aarch64-linux-gnu/7.3.1
/usr/local/lib/linaro-7.3.1/lib/gcc/aarch64-linux-gnu
/usr/local/lib/linaro-7.3.1/lib/gcc
/usr/local/lib/linaro-7.3.1/aarch64-linux-gnu/lib/aarch64-linux-gnu
/usr/local/lib/linaro-7.3.1/aarch64-linux-gnu/lib64
/home/admin/tx2-rootfs/lib/aarch64-linux-gnu/7.3.1
/home/admin/tx2-rootfs/lib/aarch64-linux-gnu
/home/admin/tx2-rootfs/lib64
/home/admin/tx2-rootfs/usr/lib/aarch64-linux-gnu/7.3.1
/home/admin/tx2-rootfs/usr/lib/aarch64-linux-gnu
/home/admin/tx2-rootfs/usr/lib64
/usr/local/lib/linaro-7.3.1/aarch64-linux-gnu/lib
/home/admin/tx2-rootfs/lib
/home/admin/tx2-rootfs/usr/lib

gcc选项--sysroot-I的影响

gcc官方文档关于-I依赖库搜索路径的介绍如下:

-I dir
-iquote dir
-isystem dir
-idirafter dir
Add the directory dir to the list of directorie
以下是在 Ubuntu 18.04 操作系统下,使用 gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf 交叉编译 Qt 5.6.2 的流程: 1. 下载 gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf 工具链,并解压到某个目录下。 2. 安装 Qt 5.6.2 的依赖库: ``` sudo apt-get update sudo apt-get install build-essential perl python git sudo apt-get install libfontconfig1-dev libfreetype6-dev libx11-dev libxext-dev libxfixes-dev libxi-dev libxrender-dev libxcb1-dev libx11-xcb-dev libxcb-glx0-dev libxcb-keysyms1-dev libxcb-image0-dev libxcb-shm0-dev libxcb-icccm4-dev libxcb-sync-dev libxcb-xfixes0-dev libxcb-shape0-dev libxcb-randr0-dev libxcb-render-util0-dev libxcb-xinerama0-dev libxcb-xkb-dev libxslt1-dev libgl1-mesa-dev libglu1-mesa-dev libsqlite3-dev libmysqlclient-dev libpq-dev libiodbc2-dev libasound2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libxrandr-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgstreamer-plugins-good1.0-dev libgstreamer-plugins-bad1.0-dev libgstreamer-plugins-ugly1.0-dev gstreamer1.0-libav gstreamer1.0-alsa gstreamer1.0-pulseaudio libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-good1.0-dev libgstreamer-plugins-bad1.0-dev libgstreamer-plugins-ugly1.0-dev ``` 3. 下载 Qt 5.6.2 的源代码,并解压到某个目录下。 4. 进入 Qt 5.6.2 的源代码目录,执行以下命令: ``` ./configure -release -opengl es2 -device linux-rasp-pi2-g++ -device-option CROSS_COMPILE=/path/to/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf- -sysroot /path/to/sysroot -opensource -confirm-license -make libs -prefix /usr/local/qt5arm -no-xcb -no-pch -no-icu -no-gif -no-libjpeg -no-libpng -no-harfbuzz -no-openssl -no-qml-debug -nomake examples -nomake tests ``` 其中,/path/to/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf 是 gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf 工具链的路径,/path/to/sysroot交叉编译环境的根文件系统路径。 5. 执行 make 命令进行编译: ``` make -j4 ``` 其中,-j4 表示使用 4 个线程进行编译。 6. 执行 make install 命令进行安装: ``` sudo make install ``` 7. 完成后,在 /usr/local/qt5arm 目录下会生成 Qt 5.6.2 的安装文件。把该目录添加到交叉编译环境的 PATH 环境变量中即可使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值