2

I'm trying to cross-compile a simple code snippet

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

with arm-linux-gnueabihf-g++ (from Ubuntu) against Raspbian sysroot with --sysroot switch set to $SYSROOT

The compilation fails with this error:

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

Even though $SYSROOT/usr/include/arm-linux-gnueabihf/bits/socket.h contains the needed define.

So I figured that toolchain contains the mentioned header as well and it's included first. The latter header somehow doesn't have this SOL_NETLINK define. So I need a way to tell the compiler to prefer the toolchain's headers to sysroot's ones.

> 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)
MD XF
  • 7,860
  • 7
  • 40
  • 71
staroselskii
  • 365
  • 1
  • 3
  • 16
  • 1
    Possible duplicate of [Cross compilation: GCC ignores --sysroot](https://stackoverflow.com/questions/17603213/cross-compilation-gcc-ignores-sysroot) – Victor Sergienko Sep 12 '19 at 00:19
  • @VictorSergienko not a duplicate of this question, since the command-line posted by the author shows that his toolchain is compiled with the "with-sysroot" option. – Étienne Apr 21 '23 at 09:10

2 Answers2

0

Use -isystem flag with $SYSROOT/usr/include/arm-linux-gnueabihf

Albeit GCC can resolve triple (arm-linux-gnueabihf part) in the path, but it won't do so with --sysroot option.

damkrat
  • 355
  • 3
  • 6
-1

#include <sys/socket.h> will include a file which is under the folder {include path}/sys/socket.h.

Include path can be any folder that you add with various options like gcc -I or --includedir. In addition there are some default include path, which you should be able to see with gcc -xc++ -E -v -.

Using gcc "-I" option will put the include path to search first, before any of the default include paths.

Now, this should give you enough information to understand that the include you give can't point to the file $SYSROOT/usr/include/arm-linux-gnueabihf/bits/socket.h as it isn't end with {include path}/sys/socket.h

OriBS
  • 722
  • 5
  • 9