To use cwrap(), you must export it. See https://emscripten.org/docs/api_reference/preamble.js.html?highlight=cwrap
You can also run an example from the tutorial, which I verified now, https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html?highlight=cwrap#interacting-with-an-api-written-in-c-c-from-nodejs It does have one typo which I'll fix now, the require should be of "api_example.js". On Fri, Jul 2, 2021 at 11:25 AM Gary Stuart <[email protected]> wrote: > Hi, > > I am trying to use emscripten on Windows 10. > I have an existing Javascript client application that works. > I want to connect in c++ code, and be able to call those c++ functions > when needed. > I have spent 1.5 days and can't even make a simple example work. > > I am using emcc version: 2.0.25 (Online the latest version seems to be > 2.0.21???) > I am using Chrome > > My example: > function factorial() > { > console.log("MT:F") > Module.onRuntimeInitialized = _ => { > console.log("A") > > const factorialCpp = Module.cwrap('factorial', 'number', ['number']); > console.log(factorialCpp) > var result = factorialCpp(10); > console.log(result) > }; > console.log("MT:F-E") > } > > When I call this function, I get only the "MT:F" and "MT:F-E", and no > errors in browser console. > > My compile line: > emcc -O3 -s WASM=1 -s EXPORTED_RUNTIME_METHODS='["cwrap"]' -s > EXPORTED_FUNCTIONS="['_factorial']" factorial.cpp factorial-service.cpp -o > factorial.js > > If optimization is -O0, it complains about the cwrap export > > I have also tried: > function factorial2() > { > Module['onRuntimeInitialized'] = onRuntimeInitializedFactorial; > const factorialCpp = Module.cwrap('factorial', 'number', ['number']); > > function onRuntimeInitializedFactorial() > { > console.log("MT:F") > console.log("A") > var result = factorialCpp(10); > console.log(result) > console.log("MT:F-E") > } > } > > and: > function factorial3() > { > var factorialCpp = Module.cwrap("factorial", "number", ["number"]); > var result = factorialCpp(10); > } > > Both produce an error: Uncaught TypeError: Module.cwrap is not a function > > I have found over 20 examples, that say just do this or that, but nothing > works. > Maybe my environment has a config issue? > > I would appreciate any any guidance on this. > Thanks, > Gary > > > > > -- > You received this message because you are subscribed to the Google Groups > "emscripten-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/emscripten-discuss/56840ae5-46b1-458d-b30f-27ee5433fd39n%40googlegroups.com > <https://groups.google.com/d/msgid/emscripten-discuss/56840ae5-46b1-458d-b30f-27ee5433fd39n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAEX4NpQ21Yoyvy_xzrLkBTWmvEQoyVWc0fG3kAMmgx%3DmrhxE9g%40mail.gmail.com.
