Skip to content

Latest commit

 

History

History
 
 

reverse

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

DFINITY Apps in C

DFINITY uses standard wasm binaries, so we may use standard tools such as Clang to build apps.

First, install Clang and set up a couple of aliases. For example, on Debian:

$ sudo apt-get install clang lld gcc-multilib
$ alias wld="wasm-ld-8 --no-entry --export-dynamic --allow-undefined"
$ alias wcc="clang --target=wasm32 -c -O3"

Reverse a string

The file reverse.c contains function that reverses a string in place. Build a wasm binary with:

$ wcc reverse.c
$ wld reverse.o -o reverse.wasm

Then package the binary so dfx can install and run it:

$ echo '{"canisters":{"reverse":{"main":"reverse"}}}' > dfx.json

The script build.sh performs the above steps.

To test the app, run dfx start in another terminal, then:

$ dfx canister create reverse
$ cp reverse.wasm .dfx/local/canisters/
$ dfx canister install --all
$ dfx canister call reverse go '("repaid")'

The script start.sh performs the above steps.

We see:

("diaper")

To stop and delete the canister:

$ dfx canister stop --all
$ dfx canister delete --all

The script stop.sh performs the above steps.