Hello, You may want to look at: https://www.mathworks.com/matlabcentral/fileexchange/69973-generate-javascript-using-matlab-coder There is an update to be released soon that will resolve the issues shown in the discussion. The existing add-on should work so long as you install emscripten using the following flag:
./emsdk install 1.38.45 Hope this helps. On Wednesday, April 14, 2021 at 7:43:02 AM UTC-4 Floh wrote: > Hi, I quickly cobbled together a "simplest possible sample" for calling a > C function from JS here: > > https://github.com/floooh/emsc-interop-demo > > This is running in the browser though and it's a bit quick'n'dirty, but it > should get you started at least > for your own experimentations. The important things are the > EMSCRIPTEN_KEEPALIVE annotation in the > hello.c file, and the underscore in front of the addc() function call in > Javascript in shell.html. > > If you cannot annotate the original source files with > EMSCRIPTEN_KEEPALIVE, you can also provide the > C functions names to make visible to Javascript with a compiler option > instead, see here: > > > https://emscripten.org/docs/getting_started/FAQ.html#why-do-functions-in-my-c-c-source-code-vanish-when-i-compile-to-javascript-and-or-i-get-no-functions-to-process > > I prefer to work with simple C-APIs instead of C++ APIs, because with > C-APIs you > can (more or less) call the functions directly from Javascript instead of > creating a "language binding" > between C++ and Javascript interfaces. For this, emscripten has a tool > called "embind" (which adds a whole > new level of complexity and more things that can break, but I guess that's > personal opinion heh). > There are some caveats with non-trivial function arguments (for instance > pointers to data on the WASM heap), > maybe embind makes this case easier, don't know. > > In any case, here's the embind documentation: > > > https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html > > Hope this helps! > > On Wednesday, 14 April 2021 at 02:43:49 UTC+2 HJ wrote: > >> I am a beginner and I'm sure all the information is there, but I am >> struggling to get even a basic example working. I can run the tutorial >> example, but I can't get anything for the following: >> >> I have converted some MATLAB .m script files to C (MATLAB can convert to >> C or CPP. I've tried both C and CPP in the below information.) >> >> MATLAB has 1 function per 1 file, so now I have two .c files with one c >> function in each. >> >> I've run emcc to convert .c to .js >> When I look in the a.out.js and search for "myfunct", all I see for my >> function is the following and I'm not sure how to call it in my js main >> program: >> >> /** @type {function(...*):?} */ var _myFunc = Module["_myFunc"] = >> createExportWrapper("myFunc"); >> >> I've played around with many different command line parameters that I've >> found on various Q&A forums, but still can't figure out how to create such >> that there is a function called "myFunc" that I can call from my other js >> files (actually typescript, but just assume js for this.) >> >> This code will be run on the server side, node.js, and not on a browser. >> Backend code that will call these converted MATLAB functions to generate >> data. >> >> It would be nice to have an example from beginning to end (I've been >> searching and searching, but I only find answers that will say things like, >> "You just need to use the xxxx option", but there is more to the puzzle >> that I am missing. >> >> It seems like the examples on emscripten site are all self contained: you >> build and run them, but no example of how to convert a function and how to >> integrate it (call it) from other js code. I have read the many options, >> but again I am not sure exactly what to do. I've also read the comments in >> the produced js file about creating a module and searched, but I am not >> sure how to define a module for a function. >> >> - What are the emcc parameters do I use? >> - Can I convert just this one function or do I need to have a main() >> that calls it? >> - Once converted, it looks like I need to define a module for >> "myfunc", but I am not sure how it is defined, when to define, do I add >> after into the .js? >> - What about using the WASM=0 option to only produce js and no WASM. >> Maybe that is a route to take? (I tried and it produced some different >> code >> in .js, but i'm still stuck on what to do with it. >> >> I created the below simple function that takes a double and returns a >> double for trying this. If anyone can show me the steps with this or any >> HelloWorld that takes a parameter and returns it would be greatly >> appreciated. (or even more complex example.) >> >> Thank helping this newbie! :-) >> >> *myFunc.c:* >> >> #include "myfunc.h" >> double myfunc(double myInt) { >> return myInt + 50.0; >> } >> >> *myFunc.h:* >> >> #ifndef MYFUNC_H >> #define MYFUNC_H >> >> /* Include Files */ >> #include "rtwtypes.h" >> #include <stddef.h> >> #include <stdlib.h> >> >> #ifdef __cplusplus >> extern "C" { >> #endif >> >> /* Function Declarations */ >> extern double myFunc(double myInt); >> >> #ifdef __cplusplus >> } >> #endif >> >> #endif >> > -- 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/0314fafc-ff94-4556-8d73-25e2bab29886n%40googlegroups.com.
