Search Unity

Nuget, IL2CPP, native libs

Discussion in 'Scripting' started by bgulanowski, Jul 3, 2019.

  1. bgulanowski

    bgulanowski

    Joined:
    Feb 9, 2018
    Posts:
    36
    I fell into a quagmire trying to get SQLite-net and SQLiteNetExtensions working with IL2CPP on Mac.

    There's a lot of complexity there. Extensions -> SQLite-net -> SQLitePCL.raw -> SQLite provider -> (e_)sqlite3.

    It was all working fine with mono. But in IL2CPP, it can't find the native library. I'm not sure if it's the Mono bug related to the library file extensions (.dylib vs .so: I tried both), or if it's looking in the wrong directory (I tried a lot of places, inside the app bundle and in my home folder), or if it's being blocked somehow. I tried renaming e_sqlite3.dylib to e_squlite3.bundle and putting it into the plugins folder.

    I'm facing a number of obstacles to understanding it and solving it. I don't know how to debug the built player. I don't understand the interactions between libraries and il2cpp. the SQLitePCL libs are supposed to be cross-platform. Unfortunately, the SQLitePCL.raw library does a lot of magic code generation, so it's not even easy to read the code.

    Are all the mono libraries converted into C++ at build time? Or are there pre-compiled versions of them, to avoid that redundant effort at build time?

    Is there a general way to get various C# libraries to work with Unity even if they weren't designed with that in mind? Or do they have to be aware of Unity or mono or something to work properly?

    What if I wanted to access other C libraries built into Mac or iOS? What if there are already C# wrappers for that? Is there a standard path to make them available to Unity? Will they work with il2cpp? How do I learn more details about the interoperability and dependencies of these tools?

    I've already read all the manual pages about il2cpp and native plugins. I've even written a simple native plugin at one time, and was able to access native Objective-C frameworks. But mixing in existing C# libraries seems to lead to incompatibilities.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    About a week ago there was a discussion in here about mixing C++ and CLI and other stuff. Check around in the pages just beyond this. I forget the exact title, and I'm not sure it is 100% relevant, but it rang a bell, and if I recall the guy wrote up a pretty good final summary of what he did.

    As for your actual problem, have you taken a look through the Unity asset store? There are several assets in there dealing with SQLite and some of them may already pre-integrate the extensions you're talking about. I know very little about SQL under Unity in general, but last year under Unity2017-something I used this package:

    https://github.com/codecoding/SQLite4Unity3d

    for read-only access to a binary-included database we put into Streaming Assets, and the entire process just worked, cross platform iOS (IL2CPP) and Android and PC, so that's also something to look at.

    EDIT: this is the post I mentioned in the first para:

    https://forum.unity.com/threads/is-c-cli-forbidden-in-unity.700115/
     
    bgulanowski likes this.
  3. bgulanowski

    bgulanowski

    Joined:
    Feb 9, 2018
    Posts:
    36
    @Kurt-Dekker thanks for the pointer. I read through it. It's interesting, although not directly related. Any info that gives me a deeper understanding of the build and runtime systems is appreciated, at least so I can make more progress and ask more intelligible/informed questions.
     
    Kurt-Dekker likes this.
  4. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    Anybody knows how to integrate .cpp files directly in IL2CPP back-end in Windows?
     
  5. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,926
  6. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    I read that page but I couldn't make it work, when I build a player it always fails.
    I filed a bug report.


    Failed running C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp/build/deploy/net471/il2cpp.exe --convert-to-cpp --emit-null-checks --enable-array-bounds-check --dotnetprofile="unityaot" --compile-cpp --libil2cpp-static --platform="WindowsDesktop" --architecture="x86" --configuration="Release" --outputpath="C:\Users\HP\Documents\Unity Projects\C11\Temp/StagingArea/Data\Native\GameAssembly.dll" --cachedirectory="C:\Users\HP\Documents\Unity Projects\C11\Assets\..\Library/il2cpp_cache" --incremental-g-c-time-slice=3 --profiler-report --map-file-parser="C:/Program Files/Unity 2020.1.0a12/Editor/Data/Tools/MapFileParser/MapFileParser.exe" --directory="C:/Users/HP/Documents/Unity Projects/C11/Temp/StagingArea/Data/Managed" --generatedcppdir="C:/Users/HP/Documents/Unity Projects/C11/Temp/StagingArea/Data/il2cppOutput"

    stdout:
    Building GameAssembly.dll with MsvcDesktopToolChain
    Msvc Install Version: 15.0
    Msvc Install SDK Directory: C:\Program Files (x86)\Windows Kits\10
    Msvc Linker Path: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX64\x86\link.exe
    Msvc Compiler Path: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX64\x86\cl.exe

    Output directory: C:\Users\HP\Documents\Unity Projects\C11\Temp\StagingArea\Data\Native
    Cache directory: C:\Users\HP\Documents\Unity Projects\C11\Library\il2cpp_cache
    il2cpp.exe didn't catch exception: Unity.IL2CPP.Building.BuilderFailedException: plug.c
    C:\Users\HP\Documents\Unity Projects\C11\Temp\StagingArea\Data\il2cppOutput\plug.c(4): error C2059: syntax error: 'string'


     
  7. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,926
    Can you share the source code for plug.c? It looks like this is a compiler error in that file.
     
  8. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    @JoshPeterson I doubt it, I tried running a console app, here is 3 different versions I tried.

    Code (CSharp):
    1. // plug.c
    2.  
    3. #include <math.h>
    4. #define EXPORT __stdcall __declspec(dllexport)
    5.  
    6. extern "C"{
    7. /* Power function*/
    8. EXPORT int power1(int n, int p)
    9. {
    10.     int r = pow(n, p);
    11.     return r;
    12. }
    13.  
    14. }
    Code (CSharp):
    1. #include <math.h>
    2. #define EXPORT __stdcall __declspec(dllexport)
    3.  
    4.  
    5. /* Power function*/
    6. EXPORT int power1(int n, int p)
    7. {
    8.     int r = pow(n, p);
    9.     return r;
    10. }
    11.  
    Code (CSharp):
    1. #include <math.h>
    2. #define EXPORT __declspec(dllexport)
    3.  
    4.  
    5. /* Power function*/
    6. EXPORT int power1(int n, int p)
    7. {
    8.     int r = pow(n, p);
    9.     return r;
    10. }
    11.  
    None it works, also tried with `cpp` extension.
     
  9. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    That was put in the Plugins folder;
    Now the C# side:

    Code (CSharp):
    1.     [DllImport("__Internal", EntryPoint ="power1")] static extern int power1(int n, int p);
    2.  
    Even if I delete the calling C# code, I would still get the error.

    I'm using 2020.1 alpha
     
  10. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,926
    Wow, that is odd. I don't see the text "string" in any of those code snippets. Can you look at the actual file where the error happens?

    C:\Users\HP\Documents\Unity Projects\C11\Temp\StagingArea\Data\il2cppOutput\plug.c

    Maybe something is mangling that file.
     
  11. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    @JoshPeterson I seem to have resolved the issue by building into a new folder instead of the same old one.
    Maybe it was dirtied by something erroneous.

    Rebuilding into the folder with a successful build doesn't reproduce the issue.
     
    JoshPeterson likes this.
  12. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    I think all is good now, can I take this chance to ask for Editor support of IL2CPP native source files.
     
  13. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,926
    We don't run any editor code with IL2CPP as the Unity Editor requires a JIT runtime (IL2CPP is AOT only). So I don't think this is something we will be able to support.
     
  14. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    Maybe Unity could automatically build the files into a dynamic link library like we currently do.
    Anyway thanks for the support.
     
  15. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,926
    That would be possible, yes. I don't think it is something Unity will implement soon though, to be honest.
     
  16. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    Would it be possible to unload the plugin so we can delete it or replace it without restarting the editor?
     
  17. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,926
    No, we don't have the ability to unload native plugins in the editor without starting the editor, unfortunately.