【分享】MPSoC交叉編譯例子


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.

GCC文檔關於sysroot的描述

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM