7

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?

5
  • 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. Commented Dec 18, 2010 at 21:16
  • 1
    Did you try compiling separate object files (gcc -c) and then linking them manually? Commented Dec 18, 2010 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... Commented Dec 18, 2010 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). Commented Dec 18, 2010 at 23:39
  • Looks like I found an answer myself.. :-) Commented Dec 20, 2010 at 21:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.