GithubHelp home page GithubHelp logo

michaelfranzl / clang-wasm-browser-starterpack Goto Github PK

View Code? Open in Web Editor NEW
17.0 3.0 3.0 479 KB

Minimal working examples of C and C++ software development targeting the web via WebAssembly.

License: The Unlicense

Nix 100.00%
webassembly webassembly-demo webassembly-tutorial clang c browser javascript cpp wasi wasi-sdk

clang-wasm-browser-starterpack's Introduction

Clang-WASM-Browser Starterpack

Minimal working examples of C and C++ software development targeting the web via WebAssembly.

Background

Clang can directly emit WebAssembly since version 8. For simple scenarios, this allows a lean and direct software development workflow targeting the web, omitting more complete SDKs like emscripten.

The wasi-sdk project is a cornerstone in this workflow. It contains no compiler or library code itself; it merely pulls in via git submodules the upstream llvm-project tree, as well as the wasi-libc tree. It contributes a Makefile which compiles these components using suitable flags.

wasi-libc is an implementation of the C standard library which compiles down to WASI syscalls (calls which would 'normally' be done into the OS kernel). The implementation of the actually used syscalls has to be provided (in other words, imported) to the WebAssembly instance. Since we are targeting the web, the implementation is provided by the JavaScript library @wasmer/wasi via the browser.

The wasi-sdk project lacks examples that show how it can be used; the present project aims to fill that gap.

Motivation

Inspired by the awesome emscripten project, I wanted to understand the low-level mechanics of getting compiled C and C++ code to run in the browser, and to find the leanest possible workflow.

Running

Using Nix

nix run
# Wait for it:
# Serving HTTP on 127.0.0.1 port 8000 (http://127.0.0.1:8000/) ...

Manual

Download and extract or install wasi-sdk Release 22.

Set the path to the extracted or installed wasi-sdk as WASI_SDK (default: /opt/wasi-sdk) and run make in the examples subdirectory:

cd examples

WASI_SDK=/path/to/wasi-sdk make

# Start a generic web server:
python3 -m http.server 8000 --bind 127.0.0.1


# In a different terminal:
open http://localhost:8000/

The Examples

The examples start as simple as possible, and then add more and more complexity:

Plain C

C with Standard Library

String handling

Plain C++

C++ with Standard Library

clang-wasm-browser-starterpack's People

Contributors

michaelfranzl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

clang-wasm-browser-starterpack's Issues

Need help with compiling C++ to wasm

Hi Michael!

I am attempting to compile C++ to WebAssembly without using Emscripten or other alternative methods that would require modifications to my C++ code (such as adding macros). I saw your answer to this Stackoverflow question from 2021 and found this repo.

I attempted to compile the 11th example, but unfortunately encountered two error messages:

wasm-ld: error: unknown --unresolved-symbols value: import-functions
wasm-ld: error: unable to find library -lclang_rt.builtins-wasm32

Actually I'm curious if there have been any advancements or new methods for compiling C++ to wasm since then. If so, I would greatly appreciate any guidance or resources you could provide. Otherwise, I would be grateful for any assistance in overcome these error messages.

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.