Search Unity

Include C++ DLL in WebGL Build

Discussion in 'Web' started by davebutlerenator2, Dec 1, 2015.

  1. davebutlerenator2

    davebutlerenator2

    Joined:
    Nov 8, 2014
    Posts:
    26
    Hi,

    I'm having trouble creating a WebGL Build that references a C++ DLL. It seems like it should be possible (http://forum.unity3d.com/threads/include-native-c-code-into-webgl-build-how.307262/), but I can't seem to do it without creating an error.

    Here is MyMathFuncs.h (included in my C++ DLL)
    Code (CSharp):
    1. extern "C" __declspec(dllexport) double Add_Static(double a, double b) {
    2.     return a + b;
    3. }
    Here is MoveBall.cs (added to a sphere GameObject in Unity)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Runtime.InteropServices;
    4.  
    5. public class MoveBall : MonoBehaviour {
    6.     [DllImport("__Internal")]
    7.     private static extern double Add_Static(double a, double b);
    8.  
    9.     void Update()
    10.     {
    11.         float addAmount = .01f;
    12.         Vector3 pos = transform.position;
    13.         pos.x = (float)Add_Static(pos.x, addAmount);
    14.         transform.position = pos;
    15.     }
    16. }
    17.  
    When I try to run the output WebGL build I get the following errors in my Firefox console:

    and then


    Any ideas what I'm doing wrong? Is using a C++ DLL possible in a WebGL build?

    I appreciate any help you can give me.
    David
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    No, this is not possible.

    It is possible to include C/C++ code in your WebGL build, but not in compiled dll form, only in source form (so that it can be compiled to JS along with all the other code).

    Check out the docs here: http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
     
  3. jeffjostar

    jeffjostar

    Joined:
    Mar 19, 2021
    Posts:
    1
    Hello, I have two questions about this topic:
    1 now, it's 2021, it is still impossible to use C++ dll in a WebGL build?
    2 if it is impossible, I need to build my C++ source in the format .a or .bc, then, I can use them in a WebGL build?

    Thank you for your confirmation. Thank you very much.
     
  4. BradHest

    BradHest

    Joined:
    May 25, 2021
    Posts:
    1
    Also curious about this now that it has been a few years.

    Is it still the case that a C++ DLL cannot be put in to a WebGL project?
     
  5. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    That is unfortunately the case, and always will be. C++ DLLs, or more precisely, DLLs containing native x86 compiled code, cannot be used with WebAssembly, since those are DLLs that have been already compiled to target the wrong platform.

    In order to use such DLLs, Unity would need to reverse-engineer/decompile them from x86 back to source, and then recompile them over to WebAssembly. This kind of technology is not feasible. The proper way is to take the original C++ source and directly "cross-compile" that to WebAssembly.
     
  6. C1wan

    C1wan

    Joined:
    May 5, 2022
    Posts:
    14
    is it now possible somehow ?