0x00 前言
演示用操作系统:Ubuntu 18.04.6 LTS \n \l
aarch64-poky-linux-gcc
版本:gcc version 10.2.0 (GCC)
最后更新日期:2023-10-19
0x01 解决方案
aarch64-poky-linux-gcc
交叉编译时报错缺少头文件.h
可能是因为没有设置--sysroot
编译选项,sysroot
被称为逻辑根目录,只在链接过程中起作用,作为交叉编译工具链搜寻头文件、库文件的根路径,即配置该选项后交叉编译工具会从--sysroot
指定目录下搜寻.h
头文件及lib
库文件。
一般来说,编译的时候会从/usr/include
中搜寻头文件、从/usr/lib
中搜寻依赖库,当设置了--sysroot=dir
后则会从dir/usr/include
搜索头文件、从dir/usr/lib
中搜索依赖库。
--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
编译选项指定aarch64-poky-linux
路径位置解决该问题。
例如使用aarch64-poky-linux
将udp_client_ipv4.c
交叉编译成可执行文件udp_test
,则使--sysroot
配置到aarch64-poky-linux
安装目录中的***/sysroots/***-poky-linux
位置上:
aarch64-poky-linux-gcc -o udp_test udp_client_ipv4.c --sysroot=/opt/nxp-real-time-edge/2.1/sysroots/aarch64-poky-linux
以上。
参考文档:
1.https://blog.csdn.net/zvvzxzko2006/article/details/110467542
2.https://blog.csdn.net/c_he_n/article/details/125433504
3.https://blog.csdn.net/u011192270/article/details/106176333?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-7-106176333-blog-110467542.235%5Ev38%5Epc_relevant_sort_base1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-7-106176333-blog-110467542.235%5Ev38%5Epc_relevant_sort_base1