简体   繁体   English

省略一些C ++子系统

[英]Omit some C++ subsystems

I notice that with emscripten, even relatively small C++ files can quickly be turned into rather huge JavaScript files. 我注意到,使用emscripten,即使是相对较小的C ++文件也可以很快变成相当庞大的JavaScript文件。 Example: 例:

#include <memory>
int main(int argc, char** argv) {
  std::shared_ptr<int> sp(new int);
}

Compile this with a recent emsdk using a command like 使用像这样的命令用最近的emsdk编译它

em++ -std=c++11 -s DISABLE_EXCEPTION_CATCHING=1 -s NO_FILESYSTEM=1 \
     -s NO_BROWSER=1 -s NO_EXIT_RUNTIME=1 -O3 -o foo.js foo.cc

The resulting file is over 400k big. 生成的文件超过400k。 With -g thrown in I can do -g抛出我可以做到

grep -n '^function _' foo.js | c++filt -_

and see what kinds of functions we have there. 看看我们在那里有什么样的功能。 Here are some examples: 这里有些例子:

std::__1::moneypunct<char, false>::do_thousands_sep() const
std::__1::locale::~locale()
std::__1::basic_string<wchar_t, …>::~basic_string()
std::__1::time_get<…>::__get_day(…) const
std::__1::codecvt<wchar_t, char, __mbstate_t>::codecvt(unsigned int)
std::__1::locale::__imp::install(std::__1::locale::facet*, long)
_printf_core

I'm not calling any of this myself, but nevertheless the functions all get included. 我自己并没有打电话给任何人,但是所有的功能都包括在内。 Probably many of them are included in some virtual function table. 可能其中许多都包含在一些虚拟功能表中。 The others might be due to some static initializer. 其他可能是由于一些静态初始化器。

If this were normal code linked against a single shared library somewhere on my HDD; 如果这是正常的代码链接到我的硬盘驱动器上的某个共享库; I wouldn't object. 我不反对。 But half a megabyte in JavaScript code to be transferred, just for a single shared pointer? 但只需要一个共享指针,就可以传输半兆字节的JavaScript代码? There has to be a way to avoid that. 必须有办法避免这种情况。

One solution, implemented here , is simply splitting the C++ library in several parts. 这里实现的一个解决方案是简单地将C ++库分成几个部分。 By moving the code dealing with I/O and locale into a separate library, all the code which can work without this can avoid the static initializer of the I/O subsystem which leads to a dependency on the functions described above. 通过将处理I / O和locale的代码移动到一个单独的库中,所有可以在没有这个的情况下工作的代码可以避免I / O子系统的静态初始化器,这会导致依赖于上述功能。 Unfortunately this will also affect strstream , for obvious reasons. 不幸的是,由于显而易见的原因,这也会影响strstream


Update: Since upstream commit 301e4ad (first included in release 1.30.6), the system libraries are no longer compiled as a single *.bc file, but instead as a *.a static library which contains several distinct objects. 更新:由于上游提交301e4ad (首先包含在1.30.6版本中),系统库不再编译为单个*.bc文件,而是编译为包含多个不同对象的*.a静态库。 Of these, only the required ones actually get linked in, which reduces code size for simple cases quite a lot. 其中,只有所需的实际链接在一起,这减少了简单案例的代码大小。

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

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