1. 编译并使用sysroot
在Xilinx SDK可以开发Linux应用程序。GCC自带一部分头文件,库文件。如果自己单板有额外的功能,比如显示,GCC就没有提供相关头文件、库文件,编译时会报告错误。
这时候需要给自己单板编译sysroot,通过sysroot提供自己单板的所有头文件,库文件。
1.1. GCC文档3.15 Options for Directory Search
-I dir
-iquote dir
-isystem dir
-idirafter dir
Add the directory dir to the list of directories to be searched for header files during preprocessing. If dir begins with ‘=’ or $SYSROOT, then the ‘=’ or $SYSROOT is replaced by the sysroot prefix; see --sysroot and -isysroot.
--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.
1.2. PetaLinux编译mubianba目标板SDK
使用命令“petalinux-build --sdk”编译目标板SDK,含有sysroot。
编译成功后,在目录images/linux/能看到sdk.sh。hankf@xszgs4:/proj/hankf/zcu106/v183/zcu106-v2018.3-bsp-egl$ petalinux-build --sdk
[INFO] building project
[INFO] sourcing bitbake
INFO: bitbake petalinux-user-image -c do_populate_sdk
Loading cache: 100% |#################################################################################################| Time: 0:00:00
Loaded 3460 entries from dependency cache.
Parsing recipes: 100% |###############################################################################################| Time: 0:00:03
Parsing of 2569 .bb files complete (2534 cached, 35 parsed). 3461 targets, 135 skipped, 0 masked, 0 errors.
NOTE: Resolving any missing task queue dependencies
Initialising tasks: 100% |############################################################################################| Time: 0:00:14
WARNING: The glib-2.0:do_install sig is computed to be 6d1616bc32584bce93c31e05f2957c43, but the sig is locked to db443bb86424a65223b97130408b8bb3 in SIGGEN_LOCKEDSIGS_t-aarch64
NOTE: Executing SetScene Tasks
NOTE: Executing RunQueue Tasks
NOTE: Tasks Summary: Attempted 5896 tasks of which 5895 didn't need to be rerun and all succeeded.
Summary: There was 1 WARNING message shown.
[INFO] Copying SDK Installer...
[INFO] successfully built project
hankf@xszgs4:/proj/hankf/zcu106/v183/zcu106-v2018.3-bsp-egl$ ls images/linux/ -l -h
-rwxr-xr-x 1 hankf hankf 386M Sep 30 15:47 sdk.sh
2.1.3. 安装SDK
使用命令“petalinux-package --sysroot”,或者执行“sdk.sh”,就能安装目标板SDK。hankf@xszgs4:/proj/hankf/zcu106/v183/zcu106-v2018.3-bsp-egl$ petalinux-package --sysroot
PetaLinux SDK installer version 2018.3
======================================
You are about to install the SDK to "/proj/hankf/zcu106/v183/zcu106-v2018.3-bsp-egl/images/linux/sdk". Proceed[Y/n]? Y
Extracting SDK.........................................................................................done
Setting it up...done
SDK has been successfully set up and is ready to be used.
Each time you wish to use the SDK in a new shell session, you need to source the environment setup script e.g.
$ . /proj/hankf/zcu106/v183/zcu106-v2018.3-bsp-egl/images/linux/sdk/environment-setup-aarch64-xilinx-linuxhankf@xszgs4:/proj/hankf/zcu106/v183/zcu106-v2018.3-bsp-egl$ ./sdk.sh
PetaLinux SDK installer version 2018.3
======================================
Enter target directory for SDK (default: /opt/petalinux/2018.3): /xilinxtool/peta/2018.3/zcu106sdk
You are about to install the SDK to "/xilinxtool/peta/2018.3/zcu106sdk". Proceed[Y/n]? y
[sudo] password for hankf:
Extracting SDK.........................................................................................done
Setting it up...done
SDK has been successfully set up and is ready to be used.
Each time you wish to use the SDK in a new shell session, you need to source the environment setup script e.g.
$ . /xilinxtool/peta/2018.3/zcu106sdk/environment-setup-aarch64-xilinx-linux
安装目标板SDK后的目录结构:hankf@xszgs4:/xilinxtool/peta/2018.3/zcu106sdk$ ls -l
total 28
-rw-r--r-- 1 root root 3538 Sep 30 16:14 environment-setup-aarch64-xilinx-linux
-rw-r--r-- 1 root root 13899 Sep 30 16:14 site-config-aarch64-xilinx-linux
drwxr-xr-x 4 root root 4096 Sep 29 17:00 sysroots
-rw-r--r-- 1 root root 96 Sep 30 16:14 version-aarch64-xilinx-linux
hankf@xszgs4:/xilinxtool/peta/2018.3/zcu106sdk$ pwd
/xilinxtool/peta/2018.3/zcu106sdk
hankf@xszgs4:/xilinxtool/peta/2018.3/zcu106sdk$ cd sysroots/aarch64-xilinx-linux/
hankf@xszgs4:/xilinxtool/peta/2018.3/zcu106sdk/sysroots/aarch64-xilinx-linux$ pwd
/xilinxtool/peta/2018.3/zcu106sdk/sysroots/aarch64-xilinx-linux
hankf@xszgs4:/xilinxtool/peta/2018.3/zcu106sdk/sysroots/aarch64-xilinx-linux$ ls -l
total 60
drwxr-xr-x 3 root root 4096 Sep 29 17:53 bin
drwxr-xr-x 2 root root 4096 Sep 29 16:15 boot
drwxr-xr-x 2 root root 4096 Sep 29 16:15 dev
drwxr-xr-x 47 root root 4096 Sep 29 17:53 etc
drwxr-xr-x 3 root root 4096 Sep 29 17:42 home
drwxr-xr-x 9 root root 4096 Sep 29 16:26 lib
drwxr-xr-x 2 root root 4096 Sep 29 16:15 media
drwxr-xr-x 2 root root 4096 Sep 29 16:15 mnt
drwxr-xr-x 2 root root 4096 Sep 29 16:15 proc
drwxr-xr-x 2 root root 4096 Sep 29 16:15 run
drwxr-xr-x 3 root root 4096 Sep 29 17:53 sbin
drwxr-xr-x 2 root root 4096 Sep 29 16:15 sys
drwxrwxrwt 2 root root 4096 Sep 29 16:15 tmp
drwxr-xr-x 10 root root 4096 Sep 29 16:33 usr
drwxr-xr-x 8 root root 4096 Sep 29 16:25 var
hankf@xszgs4:/xilinxtool/peta/2018.3/zcu106sdk/sysroots/aarch64-xilinx-linux$ ls -l /usr
total 128
drwxr-xr-x 2 root root 65536 Sep 30 09:16 bin
drwxr-xr-x 2 root root 4096 Feb 27 2019 games
drwxr-xr-x 53 root root 4096 Sep 24 13:41 include
drwxr-xr-x 144 root root 4096 Sep 24 13:41 lib
drwxr-xr-x 3 root root 4096 Jul 31 14:51 lib32
drwxr-xr-x 2 root root 4096 Aug 12 12:07 libexec
drwxr-xr-x 3 root root 4096 Jul 31 14:51 libx32
drwxr-xr-x 10 root root 4096 Feb 27 2019 local
drwxr-xr-x 3 root root 4096 Feb 27 2019 locale
drwxr-xr-x 2 root root 12288 Sep 24 13:41 sbin
drwxr-xr-x 316 root root 12288 Sep 24 13:41 share
drwxr-xr-x 9 root root 4096 Sep 24 13:41 src
文件夹“sysroots/aarch64-xilinx-linux”里,含有目标板的头文件和库文件。
每次需要执行命令“. /xilinxtool/peta/2018.3/zcu106sdk/environment-setup-aarch64-xilinx-linux”,设置环境变量。hankf@xszgs4:/xilinxtool/peta/2018.3/zcu106sdk$ cat environment-setup-aarch64-xilinx-linux
# Check for LD_LIBRARY_PATH being set, which can break SDK and generally is a bad practice
# http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN80
# http://xahlee.info/UnixResource_dir/_/ldpath.html
# Only disable this check if you are absolutely know what you are doing!
if [ ! -z "$LD_LIBRARY_PATH" ]; then
echo "Your environment is misconfigured, you probably need to 'unset LD_LIBRARY_PATH'"
echo "but please check why this was set in the first place and that it's safe to unset."
echo "The SDK will not operate correctly in most cases when LD_LIBRARY_PATH is set."
echo "For more references see:"
echo " http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN80"
echo " http://xahlee.info/UnixResource_dir/_/ldpath.html"
return 1
fi
export SDKTARGETSYSROOT=/xilinxtool/peta/2018.3/zcu106sdk/sysroots/aarch64-xilinx-linux
export PATH=/xilinxtool/peta/2018.3/zcu106sdk/sysroots/x86_64-petalinux-linux/usr/bin:/xilinxtool/peta/2018.3/zcu106sdk/sysroots/x86_64-petalinux-linux/usr/sbin:/xilinxtool/peta/2018.3/zcu106sdk/sysroots/x86_64-petalinux-linux/bin:/xilinxtool/peta/2018.3/zcu106sdk/sysroots/x86_64-petalinux-linux/sbin:/xilinxtool/peta/2018.3/zcu106sdk/sysroots/x86_64-petalinux-linux/usr/bin/../x86_64-petalinux-linux/bin:/xilinxtool/peta/2018.3/zcu106sdk/sysroots/x86_64-petalinux-linux/usr/bin/aarch64-xilinx-linux:/xilinxtool/peta/2018.3/zcu106sdk/sysroots/x86_64-petalinux-linux/usr/bin/aarch64-xilinx-linux-musl:$PATH
export PKG_CONFIG_SYSROOT_DIR=$SDKTARGETSYSROOT
export PKG_CONFIG_PATH=$SDKTARGETSYSROOT/usr/lib/pkgconfig:$SDKTARGETSYSROOT/usr/share/pkgconfig
export CONFIG_SITE=/xilinxtool/peta/2018.3/zcu106sdk/site-config-aarch64-xilinx-linux
export OECORE_NATIVE_SYSROOT="/xilinxtool/peta/2018.3/zcu106sdk/sysroots/x86_64-petalinux-linux"
export OECORE_TARGET_SYSROOT="$SDKTARGETSYSROOT"
export OECORE_ACLOCAL_OPTS="-I /xilinxtool/peta/2018.3/zcu106sdk/sysroots/x86_64-petalinux-linux/usr/share/aclocal"
unset command_not_found_handle
export CC="aarch64-xilinx-linux-gcc --sysroot=$SDKTARGETSYSROOT"
export CXX="aarch64-xilinx-linux-g++ --sysroot=$SDKTARGETSYSROOT"
export CPP="aarch64-xilinx-linux-gcc -E --sysroot=$SDKTARGETSYSROOT"
export AS="aarch64-xilinx-linux-as "
export LD="aarch64-xilinx-linux-ld --sysroot=$SDKTARGETSYSROOT"
export GDB=aarch64-xilinx-linux-gdb
export STRIP=aarch64-xilinx-linux-strip
export RANLIB=aarch64-xilinx-linux-ranlib
export OBJCOPY=aarch64-xilinx-linux-objcopy
export OBJDUMP=aarch64-xilinx-linux-objdump
export AR=aarch64-xilinx-linux-ar
export NM=aarch64-xilinx-linux-nm
export M4=m4
export TARGET_PREFIX=aarch64-xilinx-linux-
export CONFIGURE_FLAGS="--target=aarch64-xilinx-linux --host=aarch64-xilinx-linux --build=x86_64-linux --with-libtool-sysroot=$SDKTARGETSYSROOT"
export CFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types "
export CXXFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types "
export LDFLAGS="-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed"
export CPPFLAGS=""
export KCFLAGS="--sysroot=$SDKTARGETSYSROOT"
export OECORE_DISTRO_VERSION="2018.3"
export OECORE_SDK_VERSION="2018.3"
export ARCH=arm64
export CROSS_COMPILE=aarch64-xilinx-linux-
# Append environment subscripts
if [ -d "$OECORE_TARGET_SYSROOT/environment-setup.d" ]; then
for envfile in $OECORE_TARGET_SYSROOT/environment-setup.d/*.sh; do
. $envfile
done
fi
if [ -d "$OECORE_NATIVE_SYSROOT/environment-setup.d" ]; then
for envfile in $OECORE_NATIVE_SYSROOT/environment-setup.d/*.sh; do
. $envfile
done
fi
安装后,文件属于root用户。可以使用命令“sudo chown -R hankf *”更改为普通用户。
1.4. 在Makefile里使用sysroot
sysroot安装在/opt/petalinux/2018.3/zcu106bspsdk。
下列用法能编译成功。MPSOC_ROOTFS=/opt/petalinux/2018.3/zcu106bspsdk/sysroots/aarch64-xilinx-linux
CC = aarch64-linux-gnu-g++ --sysroot=$(MPSOC_ROOTFS)
CFLAGS += -g -Wall -O2 -fpic -fpermissive
CFLAGS += -I =/usr/include \
-I =/usr/include/drm \
-I =/usr/include/libdrm
LDFLAGS += -L =/usr/lib/ -lpthread -ldl -lrt -Wl,-rpath-link, -Wl,-O1 -Wl,--hash-style=gnu
LDFLAGS += -ldrm -lm -lEGL
使用的实际命令:aarch64-linux-gnu-g++ --sysroot=/xilinxtool/peta/2018.3/zcu106sdk/sysroots/aarch64-xilinx-linux -c test.cpp -g -Wall -O2 -fpic -fpermissive -I =/usr/include -I =/usr/include/drm -I =/usr/include/libdrm -I =/usr/src/debug/libdrm/2.4.83-r0/libdrm-2.4.83 -I =/usr/src/debug/libdrm/2.4.83-r0/libdrm-2.4.83 -I =/usr/src/debug/libdrm/2.4.83-r0/libdrm-2.4.83/tests -I =/usr/include/GBM -o test.o