作者:付汉杰,hankf@xilinx.com,文章转载自:赛灵思中文社区论坛
1. 介绍
有工程师反馈,使用A53交叉编译器直接编译最简单的C文件,编译器也报告。"stdio.h: No such file or directory"
aarch64-xilinx-linux-gcc -c hello.c -DPLATFORM_ZCU106 -std=c11 -o hello.o hello.c:3:10: fatal error: stdio.h: No such file or directory #include <stdio.h>
2. sysroot
GCC编译代码时,缺省是在目录/usr/include查找头文件,在目录/usr/lib查找库文件。如果是交叉编译,就不能在主机的目录下查找头文件和库文件,因为它们包含的是主机的应用程序的文件。我们需要指定目标单板的头文件和库文件。对于这种需求,GCC使用选项sysroot来实现。
GCC文档关于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.
3. 使用sysroot的编译
使用sysroot指定目录后,编译文件成功,没有任何问题。
aarch64-xilinx-linux-gcc --sysroot=/opt/petalinux/2019.2/sysroots/aarch64-xilinx-linux -c hello.c -DPLATFORM_ZCU106 -std=c11 -o hello.o aarch64-xilinx-linux-gcc --sysroot=/opt/petalinux/2019.2/sysroots/aarch64-xilinx-linux -o hello hello.o -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed `pkg-config --cflags --libs glib-2.0 gstreamer-1.0 gstreamer-app-1.0` -lpthread -ldl -lrt -Wl,-rpath-link, -Wl,-O1 -Wl,--hash-style=gnu -Wl,--allow-multiple-definition
4. Makefile
在命令行下指定sysroot目录,只能适用于非常简单的代码。对于应用程序的开发,建议使用Makefile。下面提供一个Makefile的例子。
#source "$dir_build/environment-setup-aarch64-xilinx-linux" PLNX_AARCH_PATH=/opt/petalinux/2019.2/sysroots/aarch64-xilinx-linux WARNING_CFLAGS = -Wall CC = aarch64-xilinx-linux-gcc --sysroot=$(PLNX_AARCH_PATH) export PKG_CONFIG_PATH=$(PLNX_AARCH_PATH)/usr/lib/pkgconfig/ LDFLAGS += -lpthread -ldl -lrt -Wl,-rpath-link, -Wl,-O1 -Wl,--hash-style=gnu SOURCES = $(wildcard *.c) OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) CFLAGS = -DPLATFORM_ZCU106 -std=c11 all: hello hello: hello.o $(CC) -o $@ hello.o $(LDFLAGS) %.o: %.c $(CC) -c $< $(CFLAGS) -o $@ clean: rm -f $(wildcard *.o) hello
5. 得到sysroot和交叉编译器
在petalinux工程中,执行petalinux-build –sdk,编译完成后能得到sdk.sh。
安装sdk.sh,后得到文件environment-setup-aarch64-xilinx-linux。执行source environment-setup-aarch64-xilinx-linux,配置好交叉编译器的路径。
在Makefile里,设置sysroot为其中的sysroots/aarch64-xilinx-linux/,GCC就能找到正确的头文件和库文件。
hankf@xszgs4:/opt/petalinux/2019.2$ ls -l total 28 -rw-r--r-- 1 root root 3502 Jan 17 2020 environment-setup-aarch64-xilinx-linux -rw-r--r-- 1 root root 13970 Jan 17 2020 site-config-aarch64-xilinx-linux drwxr-xr-x 4 root root 4096 Oct 5 2019 sysroots -rw-r--r-- 1 root root 96 Jan 17 2020 version-aarch64-xilinx-linux hankf@xszgs4:/opt/petalinux/2019.2$ ls -l sysroots/aarch64-xilinx-linux/ total 60 drwxr-xr-x 3 xilinx root 4096 Jan 13 2020 bin drwxr-xr-x 2 xilinx root 4096 Jan 13 2020 boot drwxr-xr-x 2 xilinx root 4096 Jan 13 2020 dev drwxr-xr-x 46 xilinx root 4096 Jan 13 2020 etc drwxr-xr-x 3 xilinx root 4096 Jan 13 2020 home drwxr-xr-x 9 xilinx root 4096 Oct 5 2019 lib drwxr-xr-x 2 xilinx root 4096 Jan 13 2020 media drwxr-xr-x 2 xilinx root 4096 Jan 13 2020 mnt dr-xr-xr-x 2 xilinx root 4096 Jan 13 2020 proc drwxr-xr-x 2 xilinx root 4096 Jan 13 2020 run drwxr-xr-x 3 xilinx root 4096 Jan 13 2020 sbin dr-xr-xr-x 2 xilinx root 4096 Jan 13 2020 sys drwxrwxrwt 2 xilinx root 4096 Jan 13 2020 tmp drwxr-xr-x 10 xilinx root 4096 Oct 5 2019 usr drwxr-xr-x 9 xilinx root 4096 Oct 5 2019 var hankf@xszgs4:/opt/petalinux/2019.2$ source environment-setup-aarch64-xilinx-linux hankf@xszgs4:/opt/petalinux/2019.2$ aarch64-xilinx-linux-gcc --version aarch64-xilinx-linux-gcc (GCC) 8.2.0 Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. hankf@xszgs4:/opt/petalinux/2019.2$ which aarch64-xilinx-linux-gcc /opt/petalinux/2019.2/sysroots/x86_64-petalinux-linux/usr/bin/aarch64-xilinx-linux/aarch64-xilinx-linux-gcc