Home > Back-end >  Enable exception support in Emscripten
Enable exception support in Emscripten

Time:06-23

I am using Bazel (5.2.0) to build an emscripten app. My setup looks like this:

main.cpp:

#include "emscripten.h"

#include <iostream>

int main(int argc, char **argv) {
    throw std::runtime_error("error!");
}

BUILD.bazel:

load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")

cc_binary(
    name = "index",
    srcs = ["main.cpp"],
    copts = [
        "-Wno-unused-variable",
        "-Wno-unused-but-set-variable",
        "-Wno-unused-function",
    ],
    data = ["index.html"],
    linkopts = [
        "-s USE_GLFW=3",
        "-s USE_WEBGPU=1",
        "-s WASM=1",
        "-s ALLOW_MEMORY_GROWTH=1",
        "-s NO_EXIT_RUNTIME=0",
        "-s ASSERTIONS=1",
        "-s EXCEPTION_CATCHING_ALLOWED=[..]",
    ],
    tags = ["manual"],
)

wasm_cc_binary(
    name = "index-wasm",
    cc_target = ":index",
)

WORKSPACE.bazel:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "emsdk",
    strip_prefix = "emsdk-311acff345fd71dcfe5f350653cec466ee7e3fbc/bazel",
    url = "https://github.com/emscripten-core/emsdk/archive/311acff345fd71dcfe5f350653cec466ee7e3fbc.tar.gz",
)

load("@emsdk//:deps.bzl", emsdk_deps = "deps")

emsdk_deps()

load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps")

emsdk_emscripten_deps(emscripten_version = "3.1.13")

When a build my application I get the error:

main.cpp:11:5: error: cannot use 'throw' with exceptions disabled
    throw std::runtime_error("error!");
    ^

I added "-s EXCEPTION_CATCHING_ALLOWED=[..]", already to the link options, but this seems not to help.

Any idea how exceptions can be enable in Emscripten using Bazel?

CodePudding user response:

You should set Enable C Exceptions option to Yes and Enable Objective-C Exceptions to Yes. If you still have the problem then refer

  • Related