DEV Community

GW
GW

Posted on

Help using Node addons to call c++

I'm currently working with trying to call c++ in js. I've been successful using nan for passing and returning primitives.

But when I tried passing/returning std::string it got harder. I read in the addon docs, "Unless there is a need for direct access to functionality which is not exposed by N-API, use N-API."

Any advise on how to proceed? Should I stick with nan or switch to N-API? I'm eventually going to work with c++ classes and structs.

Top comments (9)

Collapse
Β 
adam_cyclones profile image
Adam Crockett πŸŒ€ β€’

Hello, I'm not really so clued up on the node C++ extensions but what are you trying to achieve overall, I'd suggest web assembly depending on this.

Collapse
Β 
gwsounddsg profile image
GW β€’ β€’ Edited

Thanks for the reply Adam,

I'm building a cross-platform desktop audio app using Electron and React. The audio part needs to be in c++. I haven't used web assembly before, would it be a better solution for me? I can give you more details if you need.

Collapse
Β 
adam_cyclones profile image
Adam Crockett πŸŒ€ β€’

Yes absolutely, Wasm compiles from a variety of languages including c++. The compiler you need is called emacripten, go download it to get started. Emacripten looks scary but it's not to bad I swear and I can help you. WASM typically runs in browsers but works in node too.

Thread Thread
Β 
gwsounddsg profile image
GW β€’

Amazing! I just might take you up on your offer. Much appricated.

Thread Thread
Β 
adam_cyclones profile image
Adam Crockett πŸŒ€ β€’

If you get a repo up and running I'm sure I can help.

Thread Thread
Β 
gwsounddsg profile image
GW β€’

After a bunch of reading up on wasm and emscripten, I've been hitting a wall with just installing emscripten. Or more specifically, including emscripten.h. Any ideas? I'm on macOS.

Also, is there a better platform I can bug you with questions rather than this dev.to post?

Thread Thread
Β 
adam_cyclones profile image
Adam Crockett πŸŒ€ β€’

I think il have to clone what you have, I'm macos as well. Honestly github repo private or public will be the best way to colab

Thread Thread
Β 
gwsounddsg profile image
GW β€’

Great! Just created a repo and sent you a request.

Thread Thread
Β 
adam_cyclones profile image
Adam Crockett πŸŒ€ β€’

Thanks, I might not be able to help tonight, I'm moving house soon so agreed to sit down and make some lists with my partner il be able to clone tomorrow.