简体   繁体   English

我可以从 JavaScript 调用 c++ 函数吗?

[英]Can I call a c++ function from JavaScript?

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.有 c++ 库,我需要从客户端浏览器上运行的 JavaScript 对该库进行函数调用,该库仅驻留在客户端计算机中。 How can I load the library and access the interfaces (functions) provided by the c++ library?如何加载库并访问 c++ 库提供的接口(函数)? The library contains algorithms and rendering calls mainly.该库主要包含算法和渲染调用。

You'd be better off creating a 'C' wrapper for the C++ library and calling that from the Javascript.您最好为 C++ 库创建一个“C”包装器并从 Javascript 中调用它。

C++ is notoriously difficult to interface with due to the language standard lacking a tight ABI definition.由于语言标准缺乏严格的 ABI 定义,C++ 是出了名的难以交互。 C does not have this limitation. C 没有这个限制。 For this reason, 'C' ends up being the lingua franca when you want two programming languages to talk with each other.出于这个原因,当您希望两种编程语言相互交谈时,“C”最终成为通用语。

A few options I can think of:我能想到的几个选项:

1) Find a different library in JavaScript. 1) 在 JavaScript 中找到不同的库。

2) Port the C++ code over to JavaScript. 2) 将 C++ 代码移植到 JavaScript。 How easy this is depends on how complex the C++ code is or how much of it you need.这有多容易取决于 C++ 代码的复杂程度或您需要多少。

3) Wrap the C++ code in a web service on a server and call it using AJAX. 3) 将 C++ 代码包装在服务器上的 Web 服务中,并使用 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.我个人不熟悉任何 C++ Web 服务框架,但除此之外,您可以为库创建JavaPython.NET绑定(或仅用于您需要的部分),或其他语言的绑定。 Then you would write your web service in whatever language you chose.然后,您可以用您选择的任何语言编写您的 Web 服务。

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. 3B) #3 的替代方案 - 如果库具有命令行界面,或者存在使用该库的命令行程序,您的 Web 服务可以调用它,而您不必编写语言绑定。 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 . 4) 如果您有该库的 C++ 源代码,您可以尝试使用Emscripten将该库编译为 JavaScript。

I would probably try those in that order, roughly.我可能会按这个顺序大致尝试这些。 I might try #3 last cause it could be the trickiest.最后我可能会尝试#3,因为它可能是最棘手的。

Also, if you do #3 or #3B, you'll want to be sure your use of the library is thread-safe.此外,如果您执行#3 或#3B,您需要确保您使用的库是线程安全的。

Disclaimer: I've never tried any of these except #3B.免责声明:除了#3B,我从未尝试过任何这些。

Usually browsers doesn't allow that, because that's very insecure.通常浏览器不允许这样做,因为那非常不安全。 You might compile C++ in asm.js and use it as JavaScript library.您可以在 asm.js 中编译 C++ 并将其用作 JavaScript 库。

Alternatively you can create browser extension, which will run or call desired code.或者,您可以创建浏览器扩展,它将运行或调用所需的代码。

Yes its possible but before you can call them from javascript.是的,它可能,但在你可以从 javascript 调用它们之前。 You do this by creating an interface file to tell javascript about the interface.为此,您可以创建一个接口文件来告诉 javascript 有关该接口的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM