【问题标题】:Prefer sysroot headers over headers in the toolchain using gcc使用 gcc 优先使用 sysroot 标头而不是工具链中的标头
【发布时间】:2017-11-23 15:34:11
【问题描述】:

我正在尝试交叉编译一个简单的代码 sn-p

  1 #include <sys/socket.h>
  2 #include <stdio.h>
  3
  4 int main()
  5 {
  6     printf("%d\n", SOL_NETLINK);
  7     return 0;
  8 }

使用 arm-linux-gnueabihf-g++(来自 Ubuntu)对抗 Raspbian sysroot,--sysroot 开关设置为 $SYSROOT

编译失败并出现以下错误:

test.cpp:6:20: error: ‘SOL_NETLINK’ was not declared in this scope

即使$SYSROOT/usr/include/arm-linux-gnueabihf/bits/socket.h 包含 所需的定义。

所以我认为工具链也包含提到的标头,并且首先包含它。后一个标题不知何故没有这个 SOL_NETLINK 定义。所以我需要一种方法来告诉编译器更喜欢工具链的头文件而不是 sysroot 的头文件。

&gt; arm-linux-gnueabihf-g++ -v

Using built-in specs.
COLLECT_GCC=arm-linux-gnueabihf-g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/arm-linux-gnueabihf/5/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libquadmath --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-armhf-cross/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-armhf-cross --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-armhf-cross --with-arch-directory=arm --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libgcj --enable-objc-gc --enable-multiarch --enable-multilib --disable-sjlj-exceptions --with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard --with-mode=thumb --disable-werror --enable-multilib --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=arm-linux-gnueabihf --program-prefix=arm-linux-gnueabihf- --includedir=/usr/arm-linux-gnueabihf/include
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4)

【问题讨论】:

标签: c++ linux compiler-errors cross-compiling


【解决方案1】:

#include &lt;sys/socket.h&gt; 将包含一个位于文件夹{include path}/sys/socket.h 下的文件。

包含路径可以是您使用 gcc -I--includedir 等各种选项添加的任何文件夹。此外还有一些默认的包含路径,您应该可以通过gcc -xc++ -E -v - 看到它们。

使用 gcc "-I" 选项将首先搜索包含路径,在任何默认包含路径之前。

现在,这应该可以为您提供足够的信息来了解您提供的包含不能指向文件 $SYSROOT/usr/include/arm-linux-gnueabihf/bits/socket.h,因为它不以 {include path}/sys/socket.h 结尾

【讨论】:

  • 检查 --sysroot 选项的作用。
【解决方案2】:

在 $SYSROOT/usr/include/arm-linux-gnueabihf 中使用 -isystem 标志

尽管 GCC 可以解析路径中的三元组(arm-linux-gnueabihf 部分),但使用 --sysroot 选项时不会这样做。

【讨论】:

    猜你喜欢
    • 2013-12-17
    • 2014-09-20
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 2018-04-26
    • 2019-04-15
    • 1970-01-01
    • 2015-07-02
    相关资源
    最近更新 更多