I have a script that needs to prevent gcc from passing -L
with the standard library paths to ld
. Using -nostdlib
inhibits the -lc -lgcc
etc. but not the -L
. Using -Wl,-nostdlib
prevents the linker from using its own standard path, but doesn't stop gcc from passing -L
with the standard paths. Is there any way to ensure that gcc calls the linker with nothing in the library path expect the directories I explicitly write on the command line?
Asked
Active
Viewed 1,994 times
7

R.. GitHub STOP HELPING ICE
- 208,859
- 35
- 376
- 711
-
How about calling the linker directly with no arguments except the ones you want? I'm almost tempted to ask 'why do you need this'? I assume you have a complete replacement standard library of some sort, but then you should be able to pick up your library in preference to the standard ones. – Jonathan Leffler Dec 18 '10 at 21:16
-
1Did you try compiling separate object files (gcc -c) and then linking them manually? – ulidtko Dec 18 '10 at 21:19
-
@Jonathan: Indeed it's no problem picking up the replacement library instead of the standard one. The problem is configure scripts which look for other libraries, and wrongly find ones in the standard paths built against the standard library instead of determining that the particular library is not present. I can't just call `ld` manually unless I re-implement the whole gcc command line logic, which I'd rather not do in a shell script... – R.. GitHub STOP HELPING ICE Dec 18 '10 at 21:29
-
@ulidtko: Of course I can link manually just fine. I'm trying to make a wrapper script for gcc that will work for compiling arbitrary programs though (without having to edit their build scripts). – R.. GitHub STOP HELPING ICE Dec 18 '10 at 23:39
-
Looks like I found an answer myself.. :-) – R.. GitHub STOP HELPING ICE Dec 20 '10 at 21:38
1 Answers
4
I found a solution but it depends on gcc 4.4 or later for the -wrapper
option (slightly updated version of the script):
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
My version of this wrapper is tuned to re-add alternate crt1.o
and libc
and libgcc
files in place of the ones it prevents access to, but you could just as easily omit them if needed.

R.. GitHub STOP HELPING ICE
- 208,859
- 35
- 376
- 711