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