retifrav / webassembly-pthreads-example

An example of a C++ project compiling to WebAssembly with pthreads

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WebAssembly with pthreads example

An example of a C++ project compiling to WebAssembly with pthreads.

More details in the following article.

Emscripten

You need to have Emscripten installed in your system:

$ cd /path/to/programs
$ git clone https://github.com/emscripten-core/emsdk.git
$ cd emsdk
$ ./emsdk list
$ ./emsdk install latest
$ ./emsdk activate latest

$ source /path/to/programs/emsdk/emsdk_env.sh
$ emcc --version
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.48 (e967e20b4727956a30592165a3c1cde5c67fa0a8)

Building

Without CMake

$ cd /path/to/project

$ emcc ./src/without-pthreads.cpp ./src/some-shit.cpp \
    -o ./web/some-without-pthreads.js

$ emcc ./src/with-pthreads.cpp ./src/some-shit.cpp \
    -o ./web/some-with-pthreads.js \
    -pthread -sPROXY_TO_PTHREAD

With CMake

$ cd /path/to/project

$ cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE="$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" \
    -DCMAKE_BUILD_TYPE=Release -DWITH_PTHREADS=0 ..
$ cmake --build . --target install

$ cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE="$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" \
    -DCMAKE_BUILD_TYPE=Release -DWITH_PTHREADS=1 ..
$ cmake --build . --target install

Running

$ cd /path/to/project
$ python ./server.py

About

An example of a C++ project compiling to WebAssembly with pthreads

License:GNU General Public License v3.0


Languages

Language:CMake 24.2%Language:JavaScript 19.2%Language:C++ 16.0%Language:HTML 14.4%Language:Python 14.0%Language:CSS 11.3%Language:C 0.9%