Alternatives to the --sysroot switch of gcc?

别说谁变了你拦得住时间么 提交于 2019-12-17 23:38:57

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/2977182/alternatives-to-the-sysroot-switch-of-gcc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!