Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to call c++ function from javascript. Is it possible or not?

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.

like image 682
Ayan Avatar asked May 07 '15 12:05

Ayan


People also ask

Can you call C from JavaScript?

The easiest way to call compiled C functions from JavaScript is to use ccall() or cwrap() . ccall() calls a compiled C function with specified parameters and returns the result, while cwrap() “wraps” a compiled C function and returns a JavaScript function you can call normally.

How do you call my function in JavaScript?

The JavaScript call() Method It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

What is C in JS?

JavaScript hides this power. C is commonly used for embedded computers and applications that require high performance such as operating systems. JavaScript was first embedded only in web pages, but it is finding a new role in server applications developed through Node. js.

Can you call a JS function in HTML?

The first method is to call the JavaScript function in HTML. For this, you have to create a function then define this function either in the head section or body section of the HTML document. You can either create a link or a button and then an onclick() event is associated with them in order to call this function.


Video Answer


3 Answers

A 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 complex 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 command-line interface, or if there exists a command-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 compile 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.

like image 96
xdhmoore Avatar answered Oct 03 '22 18:10

xdhmoore


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.

like image 33
Barry Gackle Avatar answered Oct 05 '22 18:10

Barry Gackle


Usually browsers doesn't allow that, because that's very insecure. You might compile C++ in asm.js and use it as JavaScript library.

Alternatively you can create browser extension, which will run or call desired code.

like image 30
EvgeniyZh Avatar answered Oct 05 '22 18:10

EvgeniyZh