Is there a way to inhibit the default library path search with gcc? -nostdinc does this for the include path search, but -nostdlib, either by omission or by design, only inhibits the -lc -lgcc etc. but not the library search paths.
4 Answers
You should be able to do this with spec files (although fiddling with these seems like something of a dark art to me...).
If you look at the output of gcc -dumpspecs, the link_command spec is the one that builds the actual command that is invoked. Digging through some of the other specs it references, the link_libgcc spec, which is usually defined (for native compilers at least) as:
*link_libgcc: %D is the culprit:
%DDump out a -L option for each directory that GCC believes might contain startup files. If the target supports multilibs then the current multilib directory will be prepended to each of these paths.
You can override it by creating a file (e.g. my.specs) which substitutes paths of your choice:
*link_libgcc: -L/foo/bar -L/blah/blah and then passing -specs=my.specs to gcc.
3 Comments
/dev/fd/3 and a "here file" (3<<EOF) as the argument to -specs, so you can get by without even using an explicit temp file. This should make it possible to fix lots of other issues for the type of wrapping I'm doing.%D rather than hard-coded, and (2) that you could pass a partial specfile with just a few things overridden rather than having to make a complete specfile that defines everything. Your answer greatly expanded my understanding of how to customize gcc's behavior.Supposing the underlying loader is ld you might be able to redirect its whole load path with
--sysroot=directory (I don't remember the option that you have to use to pass loader arguments to gcc, but there is one...)
You could either have "directory" be something bogus, where no libraries are found, or mimic the directory layout for your own project.
2 Comments
--sysroot is that it affects any -L options you pass manually too (so this approach would basically just make it so there's never any library path).--sysroot doesn't affect -L options.You can try -nodefaultlibs to avoid all the default libraries, then use -L and -l to add-back the libraries you want in the directories you want. Directories specified on the command-line with the -L option should have priority over the default directories.
5 Comments
How about just setting the LIBRARY_PATH environment variable?
If I understand the question correctly, you want to do something like forcing the linker to look at a local library path before the default path, so you can just explicitly set that variable to control the order.
1 Comment
gcc "$@" -L/new/path.
lddirectly for the link step?