Background: 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.
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.
Problem: 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 DllNotFoundException
message.
To give you an idea of what I did, here is a simplified impression of the C file:
int end=0;
extern int main(int argc, char *argv[])
{
while(!end) {
printf("PosX : %lf\n",(double)xyz[0]);
printf("PosY : %lf\n",(double)xyz[1]);
usleep(50000);
}
return 0;
}
Initially I tried I turned this into a .so file and placed it in in Assets/Plugins/Linux
and created a simple script in the scene that contains the following:
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class PluginImportTest : MonoBehaviour
{
[DllImport("TurtlePlugin", CallingConvention = CallingConvention.Cdecl)]
private static extern int main(int argc, string[] argv);
void Start()
{
string[] argv = {"2"};
int argc = argv.Length;
Debug.Log(main(argc, argv));
}
}
However I always just seem to get DllNotFoundException: <pluginname>
. 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.
However with many thanks to this repo of theirs: https://github.com/Unity-Technologies/DesktopSamples 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 DB_init()
, which comes from rtdb_api.h
. This header file sits in /usr/include/rtdb
. I have tried placing it in /usr/local/include/rtdb
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.
Generally this leads me to the following questions, for which answers to either of them is highly appreciated:
- 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)
- 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.
- 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)?
Recap:
OS: Ubuntu 20.04
Unity version: 2020.3.13f1 LTS
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.