title: Override/clear g++ default search path for libraries
body: <h2>Question</h2>
  
  <p>I want to override/clear g++ default search path for libraries, so that g++ does only search libraries under paths explicitly specified:</p>
  
  <pre><code>$ arm-linux-gnueabihf-g++ --print-search-dirs | grep libraries:
  libraries: =/usr/lib/gcc-cross/arm-linux-gnueabihf/6/:/usr/lib/gcc-cross/arm-lin
  ux-gnueabihf/6/../../../../arm-linux-gnueabihf/lib/arm-linux-gnueabihf/6/:/usr/l
  ib/gcc-cross/arm-linux-gnueabihf/6/../../../../arm-linux-gnueabihf/lib/arm-linux
  -gnueabihf/:/usr/lib/gcc-cross/arm-linux-gnueabihf/6/../../../../arm-linux-gnuea
  bihf/lib/../lib/:/lib/arm-linux-gnueabihf/6/:/lib/arm-linux-gnueabihf/:/lib/../l
  ib/:/usr/lib/arm-linux-gnueabihf/6/:/usr/lib/arm-linux-gnueabihf/:/usr/lib/../li
  b/:/usr/lib/gcc-cross/arm-linux-gnueabihf/6/../../../../arm-linux-gnueabihf/lib/
  :/lib/:/usr/lib/
  </code></pre>
  
  <p>Can this be done?</p>
  
  <h2>Background</h2>
  
  <p>I'm on Ubuntu 17.04 compiling c++ code for several cross-platform distributions.</p>
  
  <p>I've installed Ubuntu <code>g++-arm-linux-gnueabihf</code> package, and created a target image under /opt/jessie_root, in this case for Jessie armhf. I also fixed all the links under this jessie_root image to be relative and not absolute.</p>
  
  <p>I want to compile dynamic executables with the target rootfs libraries.</p>
  
  <p>Initially I was compiling "fine" but I realized that I was linking to symbols on the host cross-toolchain libstdc++.</p>
  
  <p>I'm using cmake, but for simplicity consider this commands:</p>
  
  <pre><code>/usr/bin/arm-linux-gnueabihf-g++ main.c
  </code></pre>
  
  <p>This will link to the host libstdc++ under <code>/usr/lib/gcc-cross/arm-linux-gnueabihf/6/libstdc++.so</code>, <strong>which is not desirable</strong>.</p>
  
  <pre><code>/usr/bin/arm-linux-gnueabihf-g++ main.cpp --sysroot=/opt/jessie_root -L=/usr/lib/gcc/arm-linux-gnueabihf/4.9 -D_GLIBCXX_USE_CXX11_ABI=0
  </code></pre>
  
  <p>This will link correctly to the target libstdc++ under <code>/opt/jessie_root/usr/lib/gcc/arm-linux-gnueabihf/4.9/libstdc++.so</code>, which I want.</p>
  
  <p>The issue is that this seems a disaster waiting to happen, as if one of the default libs are not found on the target, the compiler will haply take one from the host cross-toolchain.</p>
  
  <p>I could remove or rename them, but I don't want to mess on /usr/.</p>
  
  <p>I have also played with GCC <code>-nostdlib</code> and LD <code>-nostdlib</code>, which seem to have different meanings. GCC <code>-nostdlib</code> is for libraries, and LD <code>-nostdlib</code> is for the search directories. LD <code>-nostdlib</code> have no effect, and GCC <code>-nostdlib</code> just forces me to specify the libraries manually, but it still keeps the search paths.</p>
  
  <p>Naturally I could use another toolchain/compile my own, but I would prefer to stay on the packaged toolchain.</p>