Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I strongly suggest you read the <a href="http://www.akkadia.org/drepper/dsohowto.pdf" rel="nofollow">following</a>. Afterwards, you will understand everything about shared libraries in Linux. As said by others, the quick answer is that the <code>static</code> keyword will limit the scope of the global variable to the translation unit (and thus, to the executable or shared library). Using the keyword <code>extern</code> in the header, and compiling a cpp containing the same global variable in only one of the modules (exe or dll/so) will make the global variable unique and shared amongst all the modules.</p> <p>EDIT: The behaviour on Windows is not the same as on Linux when you use the <code>extern</code> pattern because Windows' method to load dynamic link libraries (dlls) is not the same and is basically incapable of linking global variables dynamically (such that only one exists). If you can use a static loading of the DLL (not using <code>LoadLibrary</code>), then you can use the following:</p> <pre><code>//In one module which has the actual global variable: __declspec(dllexport) myClass myGlobalObject; //In all other modules: __declspec(dllimport) myClass myGlobalObject; </code></pre> <p>This will make the <code>myGlobalObject</code> unique and shared amongst all modules that are using the DLL in which the first version of the above is used.</p> <p>If you want each module to have its own instance of the global variable, then use the static keyword, the behaviour will be the same for Linux or Windows.</p> <p>If you want one unique instance of the global variable AND require dynamic loading (<code>LoadLibrary</code> or <code>dlopen</code>), you will have to make an initialization function to provide every loaded DLL with a pointer to the global variable (before it is used). You will also have to keep a reference count (you can use a <code>shared_ptr</code> for that) such that you can create a new one when none exist, increment the count otherwise, and be able to delete it when the count goes to zero as DLLs are being unloaded. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload