Cpp compilation from python fail but not in the shell
<p>I have a <code>cpp</code> file that compiles fine with <code>g++</code> by using the shell:</p>

<pre><code>extern "C"{
  #include &lt;quadmath.h&gt;
}

inline char* print( const __float128&amp; val)
{
  char* buffer = new char[128];
  quadmath_snprintf(buffer,128,"%+.34Qe", val);
  return buffer;
}
int main(){
    __float128 a = 1.0;
    print(a);
    return 0;
}
</code></pre>

<p>However, when I try to compile it via a <code>python</code> scrit, it fails with the following error:</p>

<blockquote>
  <p>"undefined reference to quadmath_snprintf"</p>
</blockquote>

<p>Here the code of the <code>python</code> script:</p>

<pre><code>import commands
import string
import os
(status, output) = commands.getstatusoutput("(g++ test/*.c -O3 -lquadmath -m64)")
</code></pre>

<p>Any idea how to solve this? Thanks.</p>

<p>When you open a shell a whole of stuff is silently initialized for you, and most important for your issue, environment variables are set. What you most likely miss is the definition of <code>LIBRARY_PATH</code>, which is the variable used by the linker to look for libraries matching the ones you instruct it to link using the <code>-lNAME</code> flags. </p>

<p>What the linker needs is a list of directories where it will search for files matching <code>libNAME.{a,so}</code>. You can also pass these directories directly using the <code>-L</code> flag, but in general, you should probably try to use a program like CMake, Make or any other build tool.</p>

<p>This will give you access to commands like <code>find_package</code> and <code>target_link_libraries</code> (CMake), to find, respectively add libraries to your build targets, instead of having to maintain your python to compile your stuff.</p>

python c++ g++