214 lines
19 KiB
HTML
214 lines
19 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
<html>
|
||
<head>
|
||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||
<title>F.A.Q.</title>
|
||
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
|
||
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
|
||
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
|
||
<link rel="up" href="../boost_dll.html" title="Chapter 14. Boost.DLL">
|
||
<link rel="prev" href="limitations.html" title="Limitations">
|
||
<link rel="next" href="design_rationale.html" title="Design Rationale">
|
||
</head>
|
||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||
<table cellpadding="2" width="100%"><tr>
|
||
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
|
||
<td align="center"><a href="../../../index.html">Home</a></td>
|
||
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
|
||
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
|
||
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
|
||
<td align="center"><a href="../../../more/index.htm">More</a></td>
|
||
</tr></table>
|
||
<hr>
|
||
<div class="spirit-nav">
|
||
<a accesskey="p" href="limitations.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_dll.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="design_rationale.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
|
||
</div>
|
||
<div class="section">
|
||
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
||
<a name="boost_dll.f_a_q_"></a><a class="link" href="f_a_q_.html" title="F.A.Q.">F.A.Q.</a>
|
||
</h2></div></div></div>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||
<span class="bold"><strong>Question:</strong></span> Is Boost.DLL thread-safe?
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">
|
||
<span class="bold"><strong>Answer:</strong></span> Some platforms allow concurrent
|
||
calls to <code class="computeroutput"><span class="identifier">dlopen</span></code> like
|
||
functions. For those platforms Boost.DLL is safe in the manner as
|
||
all the C++ Standard Library containers are: it is safe to use different
|
||
instances of shared_library from different threads even if all the
|
||
instances loaded the same library. On other platforms it is not safe
|
||
to concurrently call any of the functions from Boost.DLL (even a
|
||
<code class="computeroutput"><span class="identifier">shared_library</span><span class="special">::</span><span class="identifier">location</span><span class="special">()</span></code>
|
||
call triggers a race condition). See <a class="link" href="limitations.html#boost_dll.limitations.multithread" title="Thread safe library loading (Fails on FreeBSD, MacOS, iOS and some other)">Limitations,
|
||
Thread safe library loading</a>.
|
||
</li></ul></div>
|
||
</li></ul></div>
|
||
<pre class="programlisting"></pre>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||
<span class="bold"><strong>Question:</strong></span> Why on Linux symbols from one
|
||
plugin are seen in another? Can't get symbol with same name from right
|
||
plugin!
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">
|
||
<span class="bold"><strong>Answer:</strong></span> You've run into the symbol
|
||
shadowing problem. Compile your plugins with "-fvisibility=hidden"
|
||
flag and take a look to the Tutorial section.
|
||
</li></ul></div>
|
||
</li></ul></div>
|
||
<pre class="programlisting"></pre>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||
<span class="bold"><strong>Question:</strong></span> How Unicode (Windows) is handled?
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">
|
||
<span class="bold"><strong>Answer:</strong></span> Boost.DLL supports Unicode,
|
||
so that you could provide Unicode paths to it.
|
||
</li></ul></div>
|
||
</li></ul></div>
|
||
<pre class="programlisting"></pre>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||
<span class="bold"><strong>Question:</strong></span> Can I open an executable file?
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">
|
||
<span class="bold"><strong>Answer:</strong></span> Yes, you can. Symbols need
|
||
be exported using in the executable using <code class="computeroutput"><span class="identifier">BOOST_SYMBOL_EXPORT</span></code>
|
||
or <code class="computeroutput"><span class="identifier">BOOST_DLL_ALIAS</span></code>.
|
||
You can call <code class="computeroutput"><span class="identifier">shared_library</span><span class="special">(</span><span class="identifier">program_location</span><span class="special">())</span></code> to load yourself. Refer to the
|
||
Tutorial section for more info. You can also query executables, just
|
||
provide a path to the executable to <code class="computeroutput"><span class="identifier">library_info</span></code>
|
||
class.
|
||
</li></ul></div>
|
||
</li></ul></div>
|
||
<pre class="programlisting"></pre>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||
<span class="bold"><strong>Question:</strong></span> What if I specify wrong type
|
||
in <code class="computeroutput"><span class="identifier">shared_library</span><span class="special">::</span><span class="identifier">get</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code>
|
||
or <code class="computeroutput"><span class="identifier">import</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code>?
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">
|
||
<span class="bold"><strong>Answer:</strong></span> Usually you'll end up with
|
||
<code class="computeroutput"><span class="identifier">Segmentation</span> <span class="identifier">Fault</span></code>.
|
||
However it is safe to make types more strict, for example making
|
||
<code class="computeroutput"><span class="keyword">const</span> <span class="keyword">int</span></code>
|
||
from an <code class="computeroutput"><span class="keyword">int</span></code> will not
|
||
harm.
|
||
</li></ul></div>
|
||
</li></ul></div>
|
||
<pre class="programlisting"></pre>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||
<span class="bold"><strong>Question:</strong></span> Does your library guarantee
|
||
ABI stability of methods?
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">
|
||
<span class="bold"><strong>Answer:</strong></span> Library only guarantees
|
||
that alias names created using the <code class="computeroutput"><span class="identifier">BOOST_DLL_ALIAS</span></code>
|
||
macros will not change with the change of compiler or platform. You
|
||
must take care of functions ABI and API stability by your own.
|
||
</li></ul></div>
|
||
</li></ul></div>
|
||
<pre class="programlisting"></pre>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||
<span class="bold"><strong>Question:</strong></span> Are there any function signature
|
||
restrictions for the exported/imported functions?
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">
|
||
<span class="bold"><strong>Answer:</strong></span> No. You may import/export
|
||
functions with any signature and any return parameter.
|
||
</li></ul></div>
|
||
</li></ul></div>
|
||
<pre class="programlisting"></pre>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||
<span class="bold"><strong>Question:</strong></span> I have 2 plugins that use same
|
||
shared library. Would be the shared library loaded twice?
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">
|
||
<span class="bold"><strong>Answer:</strong></span> No. Pugins will share the
|
||
shared library instance.
|
||
</li></ul></div>
|
||
</li></ul></div>
|
||
<pre class="programlisting"></pre>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||
<span class="bold"><strong>Question:</strong></span> I have 2 plugins each of them
|
||
must work with it's own version of <code class="computeroutput"><span class="identifier">libsome_library</span></code>
|
||
shared library. How to achieve that?
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">
|
||
<span class="bold"><strong>Answer:</strong></span> Avoid such situations by
|
||
statically linking in the libsome_library into each plugin and loading
|
||
plugins with <code class="computeroutput"><span class="identifier">load_mode</span><span class="special">::</span><span class="identifier">rtld_deepbind</span></code>.
|
||
</li></ul></div>
|
||
</li></ul></div>
|
||
<pre class="programlisting"></pre>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||
<span class="bold"><strong>Question:</strong></span> How to load a shared object
|
||
from memory??
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">
|
||
<span class="bold"><strong>Answer:</strong></span> All existing OS avoid loading
|
||
shared libraries directly from userspace memory, so you'll find no
|
||
syscall for such case. Currently Boost.DLL provides no means for
|
||
honest loading shared objects from memory. This requires reimplementing
|
||
dynamic linker logic in userspace for all the platforms, which is
|
||
a huge amount of work and very error-prone. However working patches
|
||
are welcomed!
|
||
</li></ul></div>
|
||
</li></ul></div>
|
||
<p>
|
||
Workaround would be to write plugin into a temporary file in RAM and load plugin
|
||
from it:
|
||
</p>
|
||
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">filesystem</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">dll</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">;</span>
|
||
|
||
<span class="identifier">dll</span><span class="special">:</span><span class="identifier">shared_library</span> <span class="identifier">load_from_memory</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">data</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">path</span><span class="special">&</span> <span class="identifier">tmp_plugin_path</span> <span class="special">=</span> <span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">unique_path</span><span class="special">()</span> <span class="special">/</span> <span class="string">"libplugin.so"</span><span class="special">)</span> <span class="special">{</span>
|
||
<span class="keyword">const</span> <span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">path</span> <span class="identifier">plugin_location</span> <span class="special">=</span> <span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">temp_directory_path</span><span class="special">()</span> <span class="special">/</span> <span class="identifier">tmp_plugin_path</span><span class="special">;</span>
|
||
<span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">create_directories</span><span class="special">(</span><span class="identifier">plugin_location</span><span class="special">.</span><span class="identifier">parent_path</span><span class="special">());</span>
|
||
<span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">ofstream</span> <span class="identifier">ofs</span><span class="special">(</span><span class="identifier">plugin_location</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ios</span><span class="special">::</span><span class="identifier">out</span><span class="special">|</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ios</span><span class="special">::</span><span class="identifier">bin</span><span class="special">|</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ios</span><span class="special">::</span><span class="identifier">trunc</span><span class="special">);</span>
|
||
<span class="identifier">ofs</span><span class="special">.</span><span class="identifier">write</span><span class="special">(</span><span class="identifier">data</span><span class="special">,</span> <span class="identifier">size</span><span class="special">);</span>
|
||
|
||
<span class="keyword">return</span> <span class="identifier">dll</span><span class="special">::</span><span class="identifier">shared_library</span><span class="special">(</span><span class="identifier">plugin_location</span><span class="special">);</span>
|
||
<span class="special">}</span>
|
||
</pre>
|
||
<p>
|
||
</p>
|
||
<p>
|
||
But there's no guarantee that <code class="computeroutput"><span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">temp_directory_path</span><span class="special">()</span></code> will actually write to RAM, that's very platform
|
||
dependent.
|
||
</p>
|
||
<pre class="programlisting"></pre>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||
<span class="bold"><strong>Question:</strong></span> Can I use function pointer type
|
||
(like <code class="computeroutput"><span class="keyword">void</span><span class="special">(*)()</span></code>)
|
||
instead of function type (like <code class="computeroutput"><span class="keyword">void</span><span class="special">()</span></code>) to import function?
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">
|
||
<span class="bold"><strong>Answer:</strong></span> No you can't because the
|
||
meaning of importing a function pointer is different:
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: square; ">
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">lib</span><span class="special">.</span><span class="identifier">get</span><span class="special"><</span><span class="keyword">void</span><span class="special">()>(</span><span class="string">"func"</span><span class="special">)</span></code>
|
||
imports a function <code class="computeroutput"><span class="keyword">void</span>
|
||
<span class="identifier">func</span><span class="special">()</span></code>
|
||
</li>
|
||
<li class="listitem">
|
||
<code class="computeroutput"><span class="identifier">lib</span><span class="special">.</span><span class="identifier">get</span><span class="special"><</span><span class="keyword">void</span><span class="special">(*)()>(</span><span class="string">"func_var"</span><span class="special">)</span></code>
|
||
imports a variable that stores a pointer to the function <code class="computeroutput"><span class="keyword">auto</span> <span class="identifier">func_var</span>
|
||
<span class="special">=</span> <span class="special">&</span><span class="identifier">func</span></code>
|
||
</li>
|
||
</ul></div>
|
||
</li></ul></div>
|
||
</li></ul></div>
|
||
<pre class="programlisting"></pre>
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||
<span class="bold"><strong>Question:</strong></span> I have found a bug, how do I
|
||
notify?
|
||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">
|
||
<span class="bold"><strong>Answer:</strong></span> Create an <a href="https://github.com/apolukhin/Boost.DLL/issues" target="_top">issue
|
||
at GitHub</a> with a detailed description.
|
||
</li></ul></div>
|
||
</li></ul></div>
|
||
</div>
|
||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||
<td align="left"></td>
|
||
<td align="right"><div class="copyright-footer">Copyright © 2014 Renato Tegon Forti, Antony Polukhin<br>Copyright © 2015 Antony Polukhin<br>Copyright © 2016 Antony Polukhin, Klemens Morgenstern<br>Copyright © 2017-2021 Antony Polukhin<p>
|
||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||
</p>
|
||
</div></td>
|
||
</tr></table>
|
||
<hr>
|
||
<div class="spirit-nav">
|
||
<a accesskey="p" href="limitations.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_dll.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="design_rationale.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
|
||
</div>
|
||
</body>
|
||
</html>
|