Coder Social home page Coder Social logo

dev-wasm-c's Introduction

Devcontainer WASM-C

Simple devcontainer for C/C++ development

Usage

Github Codespaces

Just click the button:

Open in GitHub Codespaces

Visual Studio Code

Note this assumes that you have the VS code support for remote containers and docker installed on your machine.

git clone https://github.com/dev-wasm/dev-wasm-c
cd dev-wasm-c
code ./

Visual studio should prompt you to see if you want to relaunch the workspace in a container, you do.

Building and Running

Basic example

# compile C
clang -o main.wasm main.c
# this fails, because directories/files aren't accesible by default
wasmtime main.wasm
# this works
wasmtime --dir . main.wasm

# compile C++
clang++ -o main-cc.wasm main.cc
# this fails, because directories/files aren't accesible by default
wasmtime main-cc.wasm
# this works
wasmtime --dir . main-cc.wasm

Web serving with WAGI

There is a simple example of web serving via WebAssembly + CGI (WAGI) in the webserver directory. It uses the lighttpd web server and mod_cgi. See the webserver/lighttpd.conf file for more details.

clang -o wagi.wasm webserver/wagi.c
clang++ -o wagi-cc.wasm webserver/wagi.cc -fno-exceptions

lighttpd -D -f webserver/lighttpd.conf

Once the server is running, VS Code or Codespaces should prompt you to connect to the open port.

Http client example

There is a more complicated example in the http directory which shows an example of making an HTTP client call using the experimental wasi+http support in wasmtime-http.

Debugging

The easiest way to debug is to just add breakpoints and click on the launch icon, which will launch the VS Code debugger.

Command line

If you want to debug in the command line you can do the following:

lldb wasmtime -- -g main.wasm --dir .
(lldb) target create "wasmtime"
Current executable set to 'wasmtime' (x86_64).
(lldb) settings set -- target.run-args  "-g" "main.wasm" "--dir" "."
(lldb) settings set target.disable-aslr false  # This is needed to debug inside an un-privileged container
(lldb) b main.c:28
Breakpoint 1: no locations (pending).
WARNING:  Unable to resolve breakpoint to any actual locations.
(lldb) run
Process 11490 launched: '/root/.wasmtime/bin/wasmtime' (x86_64)
1 location added to breakpoint 1
Process 11490 stopped
* thread #1, name = 'wasmtime', stop reason = breakpoint 1.1
    frame #0: 0x00007f0440e952c0 JIT(0x5624884a01d0)`main at main.c:28:13
   25  
   26   int main()
   27   {
-> 28       fprintf(stdout, "Hello C World!\n");
   29  
   30       FILE *f = fopen("test.txt", "w+");
   31       if (!f) {
(lldb)

dev-wasm-c's People

Contributors

cxm95 avatar

Watchers

 avatar

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.