Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inhibit default library paths with gcc

Tags:

c

gcc

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.

like image 946
R.. GitHub STOP HELPING ICE Avatar asked Sep 21 '11 01:09

R.. GitHub STOP HELPING ICE


Video Answer


2 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.

like image 76
Matthew Slattery Avatar answered Sep 25 '22 06:09

Matthew Slattery


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.

like image 26
Jens Gustedt Avatar answered Sep 25 '22 06:09

Jens Gustedt