Loading C++ plugin compiled to .bc

Creating a simple C++ plugin from source:

#include <iostream>

struct S {
    S() {
        std::cout << "S constructed" << std::endl;
    }
};

S s;

extern "C" void abc_func() {
    std::cout << "abc_func" << std::endl;
}

em++ main.cpp -o libfoo.bc

Using it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;


public class Script : MonoBehaviour {
    [DllImport("foo")]
    public static extern void abc_func();

    // Use this for initialization
    void Start () {
        abc_func();
    }
   
    // Update is called once per frame
    void Update () {
    }
}

Then adding it to Assets in a clean Unity Project, ticking WebGL only and building.

Output:

I remember it working earlier. Using Unity 5.6.1 on Linux, emscripten 1.37.5. Chrome 59.

Before Unity 5.6 or 5.6.1?

I just tried it with 5.5.2 and got the same issue. I am sure I was able to call a C++ function and print something on the console without any issues at some point. It is also printing “S constructed” on startup, which means the library has been linked successfully to the asm.js build. Not sure where the “dlopen” comes from:

UnityLoader.js:3262 run() called, but dependencies remain, so not running
Module.printErr @ UnityLoader.js:3262
run @ blob:http://127.0.0.1:8080/3c883641-0857-46a7-a467-cc7a4d8483e9:17939
(anonymous) @ blob:http://127.0.0.1:8080/3c883641-0857-46a7-a467-cc7a4d8483e9:18027
UnityLoader.js:3258 S constructed
UnityLoader.js:3262 pre-main prep time: 73 ms

If I use [DllImport(“__Internal”)] it works. Kind of unintentional though.

Second problem: My .bc contains embedded JS using --pre-js. It never gets to run.

How did you embed JS? Have you tried to to add your JS to Unity as a .jspre file instead ?