title: .so files for Unity plugins: header files causing DllNotFoundException?
body: <p><strong>Background</strong>: I am in the process of creating a visualization for a robot in Unity. The robot's software (and simulator in this case) is all implemented in MATLAB Simulink models and C/C++ code in MEX files.</p>
  <p>In order to aid communication with other applications they make use of shared memory. Retrieving and pushing relevant data is thus fairly straightforward, as one can just use a DB get/put to interact with the local real time database. To not get much in the irrelevant details here, this leads to a quite simple single C file, in which I am now just printing the x and y coordinate of one of the robots. Since this all runs on Ubuntu 20.04, I have turned this into an executable that properly prints the robots location in a terminal.</p>
  <p><strong>Problem</strong>: However, I am now faced with the challenge of getting this data into Unity. I have attempted to create a .so file from this C file such that it can be used as a plugin in Unity. However I always seem to be presented with a <code>DllNotFoundException</code> message.</p>
  <p>To give you an idea of what I did, here is a simplified impression of the C file:</p>
  <pre><code>int end=0;
  
  extern int main(int argc, char *argv[])
  {
      while(!end) {
          printf(&quot;PosX : %lf\n&quot;,(double)xyz[0]);
          printf(&quot;PosY : %lf\n&quot;,(double)xyz[1]);
          usleep(50000);
      }
      return 0;
  }
  </code></pre>
  <p>Initially I tried I turned this into a .so file and placed it in in <code>Assets/Plugins/Linux</code> and created a simple script in the scene that contains the following:</p>
  <pre><code>using System;
  using System.Runtime.InteropServices;
  using UnityEngine;
  
  public class PluginImportTest : MonoBehaviour
  {
      [DllImport(&quot;TurtlePlugin&quot;, CallingConvention = CallingConvention.Cdecl)]
      private static extern int main(int argc, string[] argv);
  
      void Start()
      {
          string[] argv = {&quot;2&quot;};
          int argc = argv.Length;
          Debug.Log(main(argc, argv));
      }
  }
  </code></pre>
  <p>However I always just seem to get <code>DllNotFoundException: &lt;pluginname&gt;</code>. Most other cases that run into this issue seem to have included the .so filetype when passing it in the C# file or placed it in the wrong folder.</p>
  <p>However with many thanks to this repo of theirs: <a href="https://github.com/Unity-Technologies/DesktopSamples" rel="nofollow noreferrer">https://github.com/Unity-Technologies/DesktopSamples</a> I have actually found an example that does work. Building from this and adding in my code piece by piece, I have found out that once I get to the part where I am using something from the included header files that it starts throwing a DllNotFoundException again. For example, at some point the local RTDB get initialized by calling <code>DB_init()</code>, which comes from <code>rtdb_api.h</code>. This header file sits in <code>/usr/include/rtdb</code>. I have tried placing it in <code>/usr/local/include/rtdb</code> as well, but to no avail. I tried to more explicitly state the path to the header files as well but no change there either.</p>
  <p>Generally this leads me to the following questions, for which answers to either of them is highly appreciated:</p>
  <ol>
  <li>How would one go about tackling this issue caused by the .so file's dependencies? (ideally without having to do a lot of magic with the header files themselves/location, as I wish to implement this without changing their current codebase)</li>
  <li>Is there any way to get some more verbose feedback ? Currently it seems that building the .so file incorrectly, not referencing it correctly in the C# file, the issue with the header files etc. now all always lead to a DllNotFoundException. It would be nice to have some more detailed error reporting than the same exception for everything.</li>
  <li>Are there better/alternative ways of achieving what I wish to do here (like just starting the executable as a process in the background and feeding/pushing data from here)?</li>
  </ol>
  <p><strong>Recap:</strong></p>
  <p><em>OS</em>: Ubuntu 20.04</p>
  <p><em>Unity version</em>: 2020.3.13f1 LTS</p>
  <p>Looking to get data from a C/C++ file into Unity, either using .so files or another approach. Current attempt leads to issues with included header files.</p>