如何阻止gcc通过标准库路径传递-L到链接器

R..*_*R.. 6 c linker gcc path

我有一个脚本需要阻止gcc传递-L标准库路径ld.使用-nostdlib抑制-lc -lgcc等但不是-L.使用-Wl,-nostdlib可防止链接器使用自己的标准路径,但不会阻止gcc传递-L标准路径.有没有办法确保gcc在库路径中没有任何内容调用链接器期望我在命令行上显式写入的目录?

R..*_*R.. 4

我找到了一个解决方案,但它取决于 gcc 4.4 或更高版本的选项-wrapper(脚本的版本略有更新):

inc=/path/to/alt/incl
lib=/path/to/alt/libs
crt=/path/to/alt/crt1.o
gcc -wrapper sh,-c,'
x= ; z= ; s= ; for i ; do
[ "$z" ] || set -- ; z=1
case "$i" in
-shared) s=1 ; set -- "$@" "$i" ;;
-Lxxxxxx) x=1 ;;
-xxxxxx) x= ; [ "$s" ] || set -- "$@" '"'$crt'"' ;;
*) [ "$x" ] || set -- "$@" "$i" ;;
esac
done
exec "$0" "$@"
' -nostdinc -nostdlib -isystem "$inc" -Wl,-xxxxxx "$@" -L"$lib" -Lxxxxxx -Wl,-nostdlib -lc -lgcc
Run Code Online (Sandbox Code Playgroud)

我的这个包装器版本经过调整,可以重新添加备用文件crt1.o和文件来代替它libc阻止libgcc访问的文件,但如果需要,您可以轻松地忽略它们。