I've been trying to use a c++ library in my blazor project, so I firstly created a small function to test if it is possible:
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
int mult(int i)
{
return i * 4;
}
i compiled it using em++ with this command from a guide:
em++ test.cpp -o lib.wasm -Oz -s SIDE_MODULE=1 -s WASM=1 -s "STANDALONE_WASM=1"
Also I added reference to my csproj:
<NativeFileReference Include="lib.wasm" />
And now I am getting an error when try to buil it: lib.wasm: not a relocatable wasm file
Also I tried compile this file as C code with emcc test.c -shared -o lib.o
and it worked fine with added reference: <NativeFileReference Include="lib.o" />
When I changed emcc to em++ I got an runtime blazor error when tried to use this function: missing function: mult
Page code where I tested both of them:
@page "/counter"
@using Microsoft.AspNetCore.Components.Web
@using System.Runtime.InteropServices
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status"> <i>Current count: </i> @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 2;
private void IncrementCount()
{
[DllImport("lib")]
static extern int mult(int n);
currentCount = mult(currentCount);
}
}
What am I doing wrong?