Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to the --sysroot switch of gcc?

Tags:

path

gcc

header

The --sysroot switch is useful when you don't want the headers/libraries in the standard paths affect your build.

--sysroot=dir: Use dir as the logical root directory for headers and libraries. For example, if the compiler would normally search for headers in /usr/include and libraries in /usr/lib, it will instead search dir/usr/include and dir/usr/lib. [ref]

Can the same thing be accomplished through the use of environment variables, the gcc specs file, or any other methods that do not require command line switches?

like image 720
netvope Avatar asked Jun 04 '10 19:06

netvope


People also ask

What is Sysroot in GCC?

Using 'sysroots' A sysroot is a scaled down version of your target's filesystem, it need only contain the libraries and headers which you will compile/link against. There are many ways to set up a sysroot, one is to copy the /usr and /lib directories from your target to somewhere on your host filesystem.

What is Sysroot directory?

A sysroot is a directory which is considered to be the root directory for the purpose of locating headers and libraries. So for example if your build toolchain wants to find /usr/include/foo. h but you are cross-compilingcross-compilingA cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a compiler that runs on a PC but generates code that runs on an Android smartphone is a cross compiler.https://en.wikipedia.org › wiki › Cross_compilerCross compiler - Wikipedia and the appropriate foo. h is in /my/other/place/usr/include/foo.

Where is Sysroot in Linux?

If you are using Buildroot, you will find that the sysroot is in output/host/usr/<toolchain>/sysroot , and that there is a symbolic link to it in output/staging .


2 Answers

If you can use environment variables you can add --sysroot to CFLAGS.

like image 97
florin Avatar answered Dec 05 '22 01:12

florin


You can create a wrapper script around gcc that executes the actual gcc with the flags you wish. This works with Makefiles and complex builds that mess with environment variables. You only need to make sure your gcc script is earlier in the PATH than the actual gcc binary. The script itself is just two lines,

#!/bin/sh
exec /usr/bin/gcc --sysroot=/your/sysroot "$@"

and if $HOME/bin is early in your PATH, you can put the script in $HOME/bin, and it will not affect any other users.

If you have a configure script that expressly looks for gcc in /usr/bin/, you may need to rename /usr/bin/gcc to /usr/bin/gcc.bin, and name your script /usr/bin/gcc. This will affect all users. If you need this, but also want it to affect only certain user or users, use

#!/bin/sh
[ "$(id -un)" -eq "theuser" ] && exec /usr/bin/gcc.bin --sysroot=/your/sysroot "$@"
exec /usr/bin/gcc.bin "$@"

You can do variants, e.g. specific user accounts or group memberships to set a specific sysroot, using the same scheme.

like image 24
Nominal Animal Avatar answered Dec 05 '22 03:12

Nominal Animal