title: SQLAPI++: Get path to shared library loaded by executable
body: <p>SQLAPI++ has an unusual feature where you set a string to tell it where to find the ODBC shared library.  In my case this is <code>libtdsodbc.so</code>, and my application actually links that library at build time, but at runtime this is not enough for SQLAPI++ to work.</p>
  
  <p>My code is:</p>
  
  <pre><code>  SAConnection conn;
    conn.setOption("ODBC.LIBS") = "libtdsodbc.so";
    conn.Connect("SERVER=...", "", "", SA_ODBC_Client);
  </code></pre>
  
  <p><code>ODBC.LIBS</code> is <a href="http://www.sqlapi.com/ServerSpecific/ODBC.html" rel="nofollow noreferrer">documented</a> like this:</p>
  
  <blockquote>
    <p>Forces SQLAPI++ Library to use specified ODBC manager library. </p>
  </blockquote>
  
  <p>The above code works if you set <code>LD_LIBRARY_PATH</code> to a directory containing <code>libtdsodbc.so</code>.  But if you don't, <code>Connect()</code> fails:</p>
  
  <pre><code>libtdsodbc.so: cannot open shared object file: No such file or directory
  
  DBMS API Library 'libtdsodbc.so' loading fails
  This library is a part of DBMS client installation, not SQLAPI++
  Make sure DBMS client is installed and
  this required library is available for dynamic loading
  
  Linux/Unix:
  1) The directories in the user's LD_LIBRARY_PATH environment variable
  2) The list of libraries cached in /etc/ld.so.cache
  3) /usr/lib, followed by /lib
  </code></pre>
  
  <p>It works again if you set <code>ODBC.LIBS</code> to a full path rather than just a filename.  But how can the application know which path?</p>
  
  <p>My application (outside of SQLAPI++) finds <code>libtdsodbc.so</code> via its <code>RUNPATH</code> which is set at build time.  This path is not a system path like <code>/usr/lib</code>.  I'd like to have SQLAPI++ use the same library which is loaded in the application at runtime.</p>
  
  <p>One idea is for the application to <a href="https://stackoverflow.com/a/2846736/4323">inspect its own <code>RUNPATH</code></a>, search for <code>libtdsobc.so</code>, and use that path.  But this requires quite a bit of fiddly code to basically reimplement what <code>ld.so</code> already does.</p>
  
  <p>I don't want to bake the path into the executable at build time separately from <code>RUNPATH</code>, because I sometimes edit <code>RUNPATH</code> before deployment (and then I'd need to edit two things).</p>
  
  <p>Ideally I would like to tell SQLAPI++ to just use the library which is already loaded.  I can figure this path out by running <code>lsof -p PID | grep libtdsodbc.so</code> but running shell commands from within the executable is not a good solution (and again I would rather not reimplement <code>lsof</code>).</p>