Using Boost with Emscripten

孤者浪人 提交于 2019-11-28 16:43:37

I finally managed to compile the needed libraries with emscripten. Here are the steps I followed.

Changes in emscripten

Edit system/include/libcxx/climits to add the following definitions (see 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

Add the following line in system/include/libcxx/cwchar

#include <cstdio>

Compiling Boost as shared libraries

As suggested by npclaudiu, bootstrap the library using the gcc toolkit. Then edit project-config.jam to configure the compiler and replace:

# 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 ;
}

with

# 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++" ;
}

Force BOOST_HAS_SCHER_YIELD in boost/config/posix_features.hpp, around the line 67.

Then compile the libraries: ./b2 thread regex filesystem signals system

Compiling Boost as static libraries

Do all the above steps, then edit tools/build/v2/tools/gcc.jam and replace:

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

with

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

and

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

with

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

Compile the libraries: ./b2 link=static variant=release threading=single runtime-link=static thread signals system filesystem regex

For the record, Boost now includes an "emscripten" toolset, which (in my experience) makes the process described above unnecessary.

To use, boostrap boost as normal, then compile with b2 (or bjam) like so:

b2 toolset=emscripten 

You could try to configure Boost libraries specifying gcc as toolset, as Emscripten recommends itself as being a drop-in replacement for gcc. Also, I think it's better if you build Boost as static libraries for this case. Keep in mind that most of the Boost libraries are header-only, because they only define template classes/functions.

I don't know if you've happened to see this particular question in the FAQ, but in case you haven't:

Q. How do I link against system libraries like SDL, boost, etc.?

A. System libraries that are included with emscripten - libc, libc++ (C++ STL) and SDL - are automatically included when you compile (and just the necessary parts of them). You don't even need -lSDL, unlike other compilers (but -lSDL won't hurt either).

Other libraries not included with emscripten, like boost, you would need to compile yourself and link with your program, just as if they were a module in your project. For example, see how BananaBread links in libz. (Note that in the specific case of boost, if you only need the boost headers, you don't need to compile anything.)

Another option for libraries not included is to implement them as a JS library, like emscripten does for libc (minus malloc) and SDL (but not libc++ or malloc). See --js-library in emcc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!