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.

- 208,859
- 35
- 376
- 711
-
Why not just use the compiler what it is made for, compiling :) and using `ld` directly for the link step? – Jens Gustedt Sep 21 '11 at 06:02
-
@Jens: That means re-implementing the entire compiler-driver behavior (the "gcc" command) in either C or shell script. Not impossible, but also quite a bit of work to do something gcc should already be able to do. – R.. GitHub STOP HELPING ICE Sep 21 '11 at 06:16
-
possible duplicate of [How to stop gcc from passing -L with standard library paths to the linker](http://stackoverflow.com/questions/4480112/how-to-stop-gcc-from-passing-l-with-standard-library-paths-to-the-linker) – Ciro Santilli OurBigBook.com Jun 09 '15 at 11:55
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:
%D
Dump 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
.

- 45,290
- 8
- 103
- 119
-
1Wow, I think you found the "culprit"! Best of all, it seems gcc accepts `/dev/fd/3` and a "here file" (3<
– R.. GitHub STOP HELPING ICE Sep 21 '11 at 20:14 -
BTW if this turns out to solve all my needs, I'm adding a bounty to this question and assigning it to you. – R.. GitHub STOP HELPING ICE Sep 21 '11 at 20:15
-
Yeah, somehow I never realized (1) *exactly where* in the spec file those paths were coming from, since they were hidden in `%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. – R.. GitHub STOP HELPING ICE Sep 24 '11 at 15:45
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.

- 76,821
- 6
- 102
- 177
-
The problem with `--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). – R.. GitHub STOP HELPING ICE Sep 21 '11 at 07:05
-
2
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.

- 21,379
- 8
- 78
- 117
-
It doesn't remove the library paths, just the libraries. And adding back directories doesn't help, because configure scripts will still happily find libraries in the old default path and try to use them... – R.. GitHub STOP HELPING ICE Sep 21 '11 at 01:46
-
Do you have a library in a directory specified by -L on the gcc command line that is being ignored by a directory in the default directory or are your configure scripts picking them up? – David Nehme Sep 21 '11 at 02:02
-
My problem is building programs against an alternate library ecosystem using an existing gcc installation with a wrapper script. As long as the default search paths are there, various programs' configure scripts can find libraries which are part of the host's library ecosystem and happily try to use them, only to fail later due to incompatibilities... – R.. GitHub STOP HELPING ICE Sep 21 '11 at 02:08
-
R, your question is about gcc and where it searches for libs, but you are now talking about configure scripts. I'm saying that if you put list your alternate directories with -L on the gcc command line, then gcc will use those directories. If you don't have any alternate libs in those alternate directories, then gcc will use the default directories. Are you trying to get a link failure instead? – David Nehme Sep 21 '11 at 02:18
-
I am not trying to use gcc for a specific problem, I'm trying to wrap gcc to solve a general problem. My question is in the interest of implementing the solution to the general problem. – R.. GitHub STOP HELPING ICE Sep 21 '11 at 03:07
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.

- 25,660
- 5
- 55
- 79
-
Not just before; I want to make sure the default library path is not searched at all. Just searching a new path before the default one is easy, using a wrapper script that does `gcc "$@" -L/new/path`. – R.. GitHub STOP HELPING ICE Sep 21 '11 at 03:29