使用 emscripten 将 c++ 代码编译为 javascript 以计算两个 numbers.Practice 的总和
Using emscripten to compile c++ code to javascript for making the sum of two numbers.Practice
我想做一个简短的例子来理解 emscripten 是如何工作的。
我想制作一个 html ,我可以在两个不同的文本框中添加两个数字。我还添加了一个按钮和第三个文本框,在我输入上面两个数字并按下按钮后应该打印结果。
我发现您的项目存在一些问题。首先,我认为你应该像这样用 EMSCRIPTEN_KEEPALIVE
标记 C++ 函数:
int EMSCRIPTEN_KEEPALIVE int_sum_of_two_numbers(int number1, int number2)
{
int sum;
sum = number1 + number2;
return sum;
}
If your function is used in other functions, LLVM may inline it and it will not appear as a unique function in the JavaScript. Prevent inlining by defining the function with EMSCRIPTEN_KEEPALIVE
这允许 Javascript 代码找到您的 C++ 函数。
除此之外,当您提到导出函数 _int_sum_of_two_numbers
=> int_sum_of_two_numbers
时,您用于编译项目的命令似乎有一个额外的下划线 _
字符。所以你应该使用:
EXPORTED_FUNCTIONS='["int_sum_of_two_numbers"]'
最后一点,您可以将 main()
函数留空。该函数中的代码与您的 Web 应用程序无关。
我前一段时间写了一篇关于 integrating WebAssembly with Angular 的文章,它与您想要实现的目标非常相似。我认为这值得一读。
我想做一个简短的例子来理解 emscripten 是如何工作的。 我想制作一个 html ,我可以在两个不同的文本框中添加两个数字。我还添加了一个按钮和第三个文本框,在我输入上面两个数字并按下按钮后应该打印结果。
我发现您的项目存在一些问题。首先,我认为你应该像这样用 EMSCRIPTEN_KEEPALIVE
标记 C++ 函数:
int EMSCRIPTEN_KEEPALIVE int_sum_of_two_numbers(int number1, int number2)
{
int sum;
sum = number1 + number2;
return sum;
}
If your function is used in other functions, LLVM may inline it and it will not appear as a unique function in the JavaScript. Prevent inlining by defining the function with EMSCRIPTEN_KEEPALIVE
这允许 Javascript 代码找到您的 C++ 函数。
除此之外,当您提到导出函数 _int_sum_of_two_numbers
=> int_sum_of_two_numbers
时,您用于编译项目的命令似乎有一个额外的下划线 _
字符。所以你应该使用:
EXPORTED_FUNCTIONS='["int_sum_of_two_numbers"]'
最后一点,您可以将 main()
函数留空。该函数中的代码与您的 Web 应用程序无关。
我前一段时间写了一篇关于 integrating WebAssembly with Angular 的文章,它与您想要实现的目标非常相似。我认为这值得一读。