说个笑话-fatal error: iostream: No such file or directory /fatal error: stdio.h: No such file or direct

薛征
2023-12-01

环境

imx8xq

使用yocto bitbake编译出的meta-toolchain,安装后,进行source环境配置的脚本文件后,使用aarch64-poky-linux-gcc 、aarch64-poky-linux-g++ 进行简单的编译工作发现无法找到头文件。

譬如

编译一个test_cplusplus_sort.cpp

aarch64-poky-linux-g++ -v test_cplusplus_sort.cpp  -o t

打印

Using built-in specs.
COLLECT_GCC=aarch64-poky-linux-g++
COLLECT_LTO_WRAPPER=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/aarch64-poky-linux/gcc/aarch64-poky-linux/7.3.0/lto-wrapper
Target: aarch64-poky-linux
Configured with: ../../../../../../work-shared/gcc-7.3.0-r0/gcc-7.3.0/configure --build=x86_64-linux --host=x86_64-pokysdk-linux --target=aarch64-poky-linux --prefix=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr --exec_prefix=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr --bindir=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux --sbindir=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux --libexecdir=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/aarch64-poky-linux --datadir=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/share --sysconfdir=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/etc --sharedstatedir=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/com --localstatedir=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/var --libdir=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/lib/aarch64-poky-linux --includedir=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/include --oldincludedir=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/include --infodir=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/share/info --mandir=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/share/man --disable-silent-rules --disable-dependency-tracking --with-libtool-sysroot=/home/sung/yocto/poky/build/ci-meta-tq/tqma8xx_build/tmp/work/x86_64-nativesdk-pokysdk-linux/gcc-cross-canadian-aarch64/7.3.0-r0/recipe-sysroot --with-gnu-ld --enable-shared --enable-languages=c,c++ --enable-threads=posix --enable-multilib --enable-c99 --enable-long-long --enable-symvers=gnu --enable-libstdcxx-pch --program-prefix=aarch64-poky-linux- --without-local-prefix --enable-lto --enable-libssp --enable-libitm --disable-bootstrap --disable-libmudflap --with-system-zlib --with-linker-hash-style=gnu --enable-linker-build-id --with-ppl=no --with-cloog=no --enable-checking=release --enable-cheaders=c_global --without-isl --with-gxx-include-dir=/not/exist/usr/include/c++/7.3.0 --with-build-time-tools=/home/sung/yocto/poky/build/ci-meta-tq/tqma8xx_build/tmp/work/x86_64-nativesdk-pokysdk-linux/gcc-cross-canadian-aarch64/7.3.0-r0/recipe-sysroot-native/usr/aarch64-poky-linux/bin --with-sysroot=/not/exist --with-build-sysroot=/home/sung/yocto/poky/build/ci-meta-tq/tqma8xx_build/tmp/work/x86_64-nativesdk-pokysdk-linux/gcc-cross-canadian-aarch64/7.3.0-r0/recipe-sysroot --without-long-double-128 --enable-poison-system-directories --with-mpfr=/home/sung/yocto/poky/build/ci-meta-tq/tqma8xx_build/tmp/work/x86_64-nativesdk-pokysdk-linux/gcc-cross-canadian-aarch64/7.3.0-r0/recipe-sysroot --with-mpc=/home/sung/yocto/poky/build/ci-meta-tq/tqma8xx_build/tmp/work/x86_64-nativesdk-pokysdk-linux/gcc-cross-canadian-aarch64/7.3.0-r0/recipe-sysroot --enable-nls --enable-initfini-array --enable-__cxa_atexit
Thread model: posix
gcc version 7.3.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-o' 't' '-shared-libgcc' '-mlittle-endian' '-mabi=lp64'
 /opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/libexec/aarch64-poky-linux/gcc/aarch64-poky-linux/7.3.0/cc1plus -quiet -v -D_GNU_SOURCE test_cplusplus_sort.cpp -quiet -dumpbase test_cplusplus_sort.cpp -mlittle-endian -mabi=lp64 -auxbase test_cplusplus_sort -version -o /tmp/ccC2spoS.s
GNU C++14 (GCC) version 7.3.0 (aarch64-poky-linux)
	compiled by GNU C version 7.3.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.1.0, isl version none
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/not/exist/usr/include/c++/7.3.0"
ignoring nonexistent directory "/not/exist/usr/include/c++/7.3.0/aarch64-poky-linux"
ignoring nonexistent directory "/not/exist/usr/include/c++/7.3.0/backward"
ignoring nonexistent directory "/not/exist/usr/lib/gcc/aarch64-poky-linux/7.3.0/include"
ignoring nonexistent directory "/not/exist/usr/local/include"
ignoring nonexistent directory "/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/lib/aarch64-poky-linux/gcc/aarch64-poky-linux/7.3.0/../../../../../aarch64-poky-linux/include"
ignoring nonexistent directory "/not/exist/usr/include/"
#include "..." search starts here:
#include <...> search starts here:
 /opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/lib/aarch64-poky-linux/gcc/aarch64-poky-linux/7.3.0/include
 /opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/lib/aarch64-poky-linux/gcc/aarch64-poky-linux/7.3.0/include-fixed
End of search list.
GNU C++14 (GCC) version 7.3.0 (aarch64-poky-linux)
	compiled by GNU C version 7.3.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.1.0, isl version none
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 15150833fbb777aad6c8335796d7f548
test_cplusplus_sort.cpp:1:10: fatal error: iostream: No such file or directory
 #include <iostream>
          ^~~~~~~~~~
compilation terminated.

原因

原因是PATH没有设置对,从工具链查找的预设路径看,明显是位置不对。简单来说就是sysroot没设置正确。

 

正确的做法

使用工具链里的environment-setup-aarch64-poky-linux 进行环境变量设置,打开文件可以发现,该文件已经对CC CXX等变量进行设置预设值

# 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=/opt/fsl-imx-wayland/4.14-sumo/sysroots/aarch64-poky-linux
export PATH=/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin:/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/sbin:/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/bin:/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/sbin:/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/../x86_64-pokysdk-linux/bin:/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux:/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux-musl:$PATH
export PKG_CONFIG_SYSROOT_DIR=$SDKTARGETSYSROOT
export PKG_CONFIG_PATH=$SDKTARGETSYSROOT/usr/lib/pkgconfig:$SDKTARGETSYSROOT/usr/share/pkgconfig
export CONFIG_SITE=/opt/fsl-imx-wayland/4.14-sumo/site-config-aarch64-poky-linux
export OECORE_NATIVE_SYSROOT="/opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux"
export OECORE_TARGET_SYSROOT="$SDKTARGETSYSROOT"
export OECORE_ACLOCAL_OPTS="-I /opt/fsl-imx-wayland/4.14-sumo/sysroots/x86_64-pokysdk-linux/usr/share/aclocal"
unset command_not_found_handle
export CC="aarch64-poky-linux-gcc  --sysroot=$SDKTARGETSYSROOT"
export CXX="aarch64-poky-linux-g++  --sysroot=$SDKTARGETSYSROOT"
export CPP="aarch64-poky-linux-gcc -E  --sysroot=$SDKTARGETSYSROOT"
export AS="aarch64-poky-linux-as "
export LD="aarch64-poky-linux-ld  --sysroot=$SDKTARGETSYSROOT"
export GDB=aarch64-poky-linux-gdb
export STRIP=aarch64-poky-linux-strip
export RANLIB=aarch64-poky-linux-ranlib
export OBJCOPY=aarch64-poky-linux-objcopy
export OBJDUMP=aarch64-poky-linux-objdump
export AR=aarch64-poky-linux-ar
export NM=aarch64-poky-linux-nm
export M4=m4
export TARGET_PREFIX=aarch64-poky-linux-
export CONFIGURE_FLAGS="--target=aarch64-poky-linux --host=aarch64-poky-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="4.14-sumo"
export OECORE_SDK_VERSION="4.14-sumo"
export ARCH=arm64
export CROSS_COMPILE=aarch64-poky-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

编译代码时的指令需要使用CC CXX,而不是直接使用 aarch64-poky-linux-gcc 

 

 

 

 

 类似资料: