<div dir="ltr"><font face="monospace, monospace">GetExecutablePath</font> does some funky stuff to get the name of a dynamically loaded shared object:<div><pre style="font-family:Consolas;color:gainsboro;background:rgb(30,30,30)"><span style="color:rgb(86,156,214)">if</span> <span style="color:rgb(180,180,180)">(!</span><span style="color:rgb(127,127,127)">CanonicalPrefixes</span><span style="color:rgb(180,180,180)">)</span>
    <span style="color:rgb(86,156,214)">return</span> <span style="color:rgb(127,127,127)">Argv0</span><span style="color:rgb(180,180,180)">;</span>

  <span style="color:rgb(87,166,74)">// This just needs to be some symbol in the binary; C++ doesn't</span>
  <span style="color:rgb(87,166,74)">// allow taking the address of ::main however.</span>
  <span style="color:rgb(86,156,214)">void</span> <span style="color:rgb(180,180,180)">*</span><span style="color:rgb(200,200,200)">P</span> <span style="color:rgb(180,180,180)">=</span> <span style="color:rgb(180,180,180)">(</span><span style="color:rgb(86,156,214)">void</span><span style="color:rgb(180,180,180)">*)</span> <span style="color:rgb(180,180,180)">(</span><span style="color:rgb(78,201,176)">intptr_t</span><span style="color:rgb(180,180,180)">)</span> <span style="color:rgb(200,200,200)">GetExecutablePath</span><span style="color:rgb(180,180,180)">;</span>
  <span style="color:rgb(86,156,214)">return</span> <span style="color:rgb(200,200,200)">llvm</span><span style="color:rgb(180,180,180)">::</span><span style="color:rgb(200,200,200)">sys</span><span style="color:rgb(180,180,180)">::</span><span style="color:rgb(200,200,200)">fs</span><span style="color:rgb(180,180,180)">::</span><span style="color:rgb(200,200,200)">getMainExecutable</span><span style="color:rgb(180,180,180)">(</span><span style="color:rgb(127,127,127)">Argv0</span><span style="color:rgb(180,180,180)">,</span> <span style="color:rgb(200,200,200)">P</span><span style="color:rgb(180,180,180)">);</span>

</pre></div><div>The <font face="monospace, monospace">getMainExecutable </font>function, declared as:</div><div><pre style="font-family:Consolas;color:gainsboro;background:rgb(30,30,30)">std::string getMainExecutable(const char *argv0, void *MainAddr)
</pre></div><div>...and uses <font face="monospace, monospace">dladdr</font> to get the name of the aforementioned shared object, if <font face="monospace, monospace">MainAddr</font> points to an address in a shared object:</div><div><pre style="font-family:Consolas;color:gainsboro;background:rgb(30,30,30)">#elif defined(HAVE_DLFCN_H)
  // Use dladdr to get executable path if available.
  Dl_info DLInfo;
  int err = dladdr(MainAddr, &DLInfo);
  if (err == 0)
    return "";

  // If the filename is a symlink, we need to resolve and return the location of
  // the actual executable.
  char link_path[MAXPATHLEN];
  if (realpath(DLInfo.dli_fname, link_path))
    return link_path;
#else</pre></div><div><font face="monospace, monospace">GetExecutablePath</font> is casted to a <font face="monospace, monospace">void*</font>, I think, because many other types of objects are passed in (<font face="monospace, monospace">ClangTool::run</font>, for example, passes an <font face="monospace, monospace">int</font>), but I find the explicit casting pretty confusing.</div><div><br></div><div>What's up with that? Is there some reason for the explicit casting?</div><div><br></div><div>I assume it's because <font face="monospace, monospace">getMainExecutable</font> is not <font face="monospace, monospace">const</font>-correct, but I can't try fixing it because linking fails miserably when I constify <font face="monospace, monospace">MainAddr</font>.</div><div><br></div><div><div><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><span style="font-size:12.8px">Sincerely,</span><br style="font-size:12.8px"><span style="font-size:12.8px">Alexander Riccio</span><br style="font-size:12.8px"><span style="font-size:12.8px">--</span><br style="font-size:12.8px"><span style="font-size:12.8px">"Change the world or go home."</span><div style="font-size:12.8px"><a href="http://about.me/ariccio" target="_blank">about.me/ariccio</a></div><div style="font-size:12.8px"><a href="http://about.me/ariccio" target="_blank"><br></a></div><div style="font-size:12.8px">If left to my own devices, I will build more.</div><div style="font-size:12.8px">⁂</div></div></div></div></div></div>
</div></div>