javascript - 在 Emscripten 中使用 Boost

标签 javascript c++ boost emscripten

我有一个想要转换为 Web 应用程序的 c++ 项目。为此,我想使用 Emscripten 来构建项目。

该项目使用了一些外部库。我设法编译或找到了大多数库的 JavaScript 版本,现在我被 Boost 困住了。实际上我什至不知道如何开始使用 Boost:他们使用 boostrap 脚本来生成文件来构建库。可以将工具集传递给此脚本,但显然不支持 Emscripten。

我的项目使用 Boost 的以下部分:线程、正则表达式、文件系统、信号、系统。如何使用 Emscripten 编译这些库?

编辑

按照 npclaudiu 的回答,我用 gcc 工具包引导库,然后我编辑 project-config.jam 来配置编译器,替换:

# Compiler configuration. This definition will be used unless
# you already have defined some toolsets in your user-config.jam
# file.
if ! gcc in [ feature.values <toolset> ]
{
    using gcc ;
}

# Compiler configuration. This definition will be used unless
# you already have defined some toolsets in your user-config.jam
# file.
if ! gcc in [ feature.values <toolset> ]
{
    using gcc : : "/full/path/to/em++" ;
}

现在,输入 ./b2 可以有效地构建库。 Boost.Signals 和 Boost.System 编译良好。其他的有一些错误。

Boost.Thread 提示:

libs/thread/src/pthread/thread.cpp:503:27: error: use of undeclared identifier 'pthread_yield'
        BOOST_VERIFY(!pthread_yield());
                      ^

Boost.Regex 经常提示 CHAR_BIT 未声明,但这似乎是 emscripten 中的一个问题:

In file included from libs/regex/build/../src/c_regex_traits.cpp:28:
In file included from ./boost/regex/v4/c_regex_traits.hpp:26:
In file included from ./boost/regex/v4/regex_workaround.hpp:35:
/path/to/emscripten/system/include/libcxx/vector:1989:92: error: use of undeclared identifier 'CHAR_BIT'
static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
                                                                                       ^

Boost.FileSystem 似乎也因 emscripten 而失败:

In file included from libs/filesystem/src/windows_file_codecvt.cpp:21:
/path/to/emscripten/system/include/libcxx/cwchar:117:9: error: no member named 'FILE' in the global namespace
using ::FILE;
      ~~^

最佳答案

我终于设法用 emscripten 编译了所需的库。以下是我遵循的步骤。

emscripten 的变化

编辑 system/include/libcxx/climits 以添加以下定义(参见 http://github.com/kripken/emscripten/issues/531):

#ifndef CHAR_BIT
# define CHAR_BIT __CHAR_BIT__
#endif

#ifndef CHAR_MIN
# define CHAR_MIN (-128)
#endif

#ifndef CHAR_MAX
# define CHAR_MAX 127
#endif

#ifndef SCHAR_MIN
# define SCHAR_MIN (-128)
#endif

#ifndef SCHAR_MAX
# define SCHAR_MAX 127
#endif

#ifndef UCHAR_MAX

# define UCHAR_MAX 255
#endif

#ifndef SHRT_MIN
# define SHRT_MIN (-32767-1)
#endif

#ifndef SHRT_MAX
# define SHRT_MAX 32767
#endif

#ifndef USHRT_MAX
# define USHRT_MAX 65535
#endif

#ifndef INT_MAX
# define INT_MAX __INT_MAX__
#endif

#ifndef INT_MIN
# define INT_MIN (-INT_MAX-1)
# define INT_MIN (-INT_MAX-1)
#endif

#ifndef UINT_MAX
# define UINT_MAX (INT_MAX * 2U + 1)
#endif

#ifndef LONG_MAX
# define LONG_MAX __LONG_MAX__
#endif

#ifndef LONG_MIN
# define LONG_MIN (-LONG_MAX-1)
#endif

#ifndef ULONG_MAX
# define ULONG_MAX (LONG_MAX * 2UL + 1)
#endif

system/include/libcxx/cwchar

中添加以下行
#include <cstdio>

将 Boost 编译为共享库

按照 npclaudiu 的建议,使用 gcc 工具包引导库。然后编辑 project-config.jam 配置编译器并替换:

# Compiler configuration. This definition will be used unless
# you already have defined some toolsets in your user-config.jam
# file.
if ! gcc in [ feature.values <toolset> ]
{
    using gcc ;
}

# Compiler configuration. This definition will be used unless
# you already have defined some toolsets in your user-config.jam
# file.
if ! gcc in [ feature.values <toolset> ]
{
    using gcc : : "/full/path/to/emscripten/em++" ;
}

boost/config/posix_features.hpp 中强制 BOOST_HAS_SCHER_YIELD,在第 67 行附近。

然后编译库:./b2 thread regex filesystem signals system

将 Boost 编译为静态库

执行上述所有步骤,然后编辑 tools/build/v2/tools/gcc.jam 并替换:

toolset.flags gcc.archive .AR $(condition) : $(archiver[1]) ;

toolset.flags gcc.archive .AR $(condition) : "/full/path/to/emscripten/emar" ;

toolset.flags gcc.archive .RANLIB $(condition) : $(ranlib[1]) ;

toolset.flags gcc.archive .RANLIB $(condition) :
  "/full/path/to/emscripten/emranlib" ;

编译库:./b2 link=static variant=release threading=single runtime-link=static thread signals system filesystem regex

关于javascript - 在 Emscripten 中使用 Boost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15724357/

相关文章:

javascript - 使用 Celerity 下载文件

javascript - 如何在不加载相同 URL 的情况下镜像 iframe? CSS 未激活

java - JVM 最大堆大小参数在通过 C++ 实例化时不起作用

c++ - boost 范围适配器

c++ - boost::shared_ptr 断言错误与 boost::asio:io_service

c++ - 从状态内的自定义函数(不是 Action ) boost MSM 调用 process_event?

javascript - 即使页面向下滚动,也会将 div 垂直和水平居中

javascript - 在 Jade 中渲染 JSON 对象名称

c++ - 使用另一个项目的 ImageMagick 静态编译会出现链接器错误

c++ - fread() 无法读取 Dev-Cpp 中的文件数据