There is c++ library and I need to make function calls to this library from JavaScript running on the browser on the client side, the library resides in the client machine only. How can I load the library and access the interfaces (functions) provided by the c++ library? The library contains algorithms and rendering calls mainly.
There is c++ library and I need to make function calls to this library from JavaScript running on the browser on the client side, the library resides in the client machine only. How can I load the library and access the interfaces (functions) provided by the c++ library? The library contains algorithms and rendering calls mainly.
Share Improve this question edited Jul 15, 2022 at 20:41 AndrewL64 16.3k8 gold badges50 silver badges85 bronze badges asked May 7, 2015 at 12:32 AyanAyan 1631 silver badge7 bronze badges 9- 8 No, it is not possible. – RevanProdigalKnight Commented May 7, 2015 at 12:33
- From an native-enabled Extension, perhaps, otherwise No. – Alex K. Commented May 7, 2015 at 12:35
- 2 Short answer: usually. It depends on the javascript engine built into the browser. Each will have it's own way of doing things (consult developer docs). – Richard Hodges Commented May 7, 2015 at 12:39
- 7 This sounds like a security catastrophe waiting to happen. – Joost Commented May 7, 2015 at 12:45
- 1 If Chrome-only is fine, have a look at PNaCl – gd1 Commented Aug 26, 2015 at 22:04
4 Answers
Reset to default 2A few options I can think of:
1) Find a different library in JavaScript.
2) Port the C++ code over to JavaScript. How easy this is depends on how plex the C++ code is or how much of it you need.
3) Wrap the C++ code in a web service on a server and call it using AJAX. I'm not personally familiar with any C++ web service frameworks, but barring that, you could create Java, Python, or .NET bindings for the library (or just for the portion that you need), or bindings in another language. Then you would write your web service in whatever language you chose.
3B) Alternative to #3 - If the library has a mand-line interface, or if there exists a mand-line program that uses the library, your web service could call that and you wouldn't have to write a language binding. Note however that there are performance & security problems to be aware of with this option.
4) If you have the C++ source code for the library, you could try to pile the library to JavaScript using Emscripten.
I would probably try those in that order, roughly. I might try #3 last cause it could be the trickiest.
Also, if you do #3 or #3B, you'll want to be sure your use of the library is thread-safe.
Disclaimer: I've never tried any of these except #3B.
You'd be better off creating a 'C' wrapper for the C++ library and calling that from the Javascript.
C++ is notoriously difficult to interface with due to the language standard lacking a tight ABI definition. C does not have this limitation. For this reason, 'C' ends up being the lingua franca when you want two programming languages to talk with each other.
Usually browsers doesn't allow that, because that's very insecure. You might pile C++ in asm.js and use it as JavaScript library.
Alternatively you can create browser extension, which will run or call desired code.
Yes its possible but before you can call them from javascript. You do this by creating an interface file to tell javascript about the interface.