简体   繁体   English

如何将我的库打包到一个自执行函数中? (脚本)

[英]How to pack my library in a self executing function? (emscripten)

I've created a library in C++ using Embind and Emscripten. 我已经使用Embind和Emscripten在C ++中创建了一个库。

Some hand written JS code is also added to the library using --pre-js 还使用--pre-js将一些手写的JS代码添加到库中

The library works. 图书馆工作。 But I would like to rearrange the code, to this: 但我想将代码重新排列为:

var MYLIB = (function(){
  // ... Original Code ...
  return Module;
})();

So the code would not pollute the global namespace, and the code minifier could do better optimizations. 因此,代码不会污染全局名称空间,并且代码缩减程序可以进行更好的优化。

Are there build in functions for this in emcc ? emcc是否有为此emcc函数?
The library will only run in webbrowsers, not in nodejs. 该库将仅在Web浏览器中运行,而不在nodejs中运行。

What you're looking for are the MODULARIZE and EXPORT_NAME options. 什么你要找的是MODULARIZEEXPORT_NAME选项。 Check the documentation in settings.js . 检查settings.js中的文档。

Quoting from that file: 引用该文件:

// By default we emit all code in a straightforward way into the output //默认情况下,我们以直接的方式将所有代码发送到输出中
// .js file. // .js文件。 That means that if you load that in a script tag in a web 这意味着如果您将其加载到网络中的脚本标签中
// page, it will use the global scope. //页面,它将使用全局范围。 With MODULARIZE set, we will instead emit 设置MODULARIZE后,我们将改为发出
// //
// var EXPORT_NAME = function(Module) { // var EXPORT_NAME = function(Module){
// Module = Module || //模块=模块|| {}; {};
// // .. all the emitted code from emscripten .. // // ..从emscripten ..发出的所有代码
// return Module; //返回模块;
// }; //};
// //
// where EXPORT_NAME is from the option of the same name //其中EXPORT_NAME来自同名选项

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

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