0

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?

hoksly
  • 1
  • C++ is very different from C. It has it’s own runtime and you must mess around with settings to get it to work with c#. Check out https://stackoverflow.com/questions/772041/using-c-library-in-c-sharp#772062. There are many related posts. – SupaMaggie70 b Feb 02 '23 at 19:07
  • 1
    What supa wrote doesn't matter here as you don't compile to native but wasm(also link is unrelated, too)... You need to find a switch to compile to relocatable maybe it's `-r` but I'm not familiar with em++ – Selvin Feb 02 '23 at 19:38

0 Answers0