You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/public/introduction.md
+30-7Lines changed: 30 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,13 @@ In order for `C++` to be called from `javascript` there has to be an interface l
4
4
To be as close to what an engineer would have written, `tolc` generates human readable [`embind11`](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html#embind).
5
5
This is then compiled to a `.wasm` and a `.js` file that `javascript` can import.
6
6
7
-
## Prerequisites ##
7
+
## Using a `C++` library from `javascript` ##
8
+
9
+
This is a quick guide to using a `C++` library (here called `MyLib`) from `javascript`. We will:
10
+
11
+
1. Download and use `Tolc`
12
+
2. Download and set up `Emscripten`
13
+
3. Use the resulting `WebAssembly` from `javascript`
8
14
9
15
The following works on all supported platforms. On all platforms you need `git` available in your `path`. Commands that should be run from a terminal starts with `$ `, while comments starts with `# `.
10
16
@@ -13,6 +19,7 @@ The following works on all supported platforms. On all platforms you need `git`
13
19
Just add the following in a `CMakeLists.txt` below where the library you intend to use from `javascript` is defined:
14
20
15
21
```cmake
22
+
# Download Tolc
16
23
# Can be ["latest", "v0.2.0", ...]
17
24
set(tolc_version latest)
18
25
include(FetchContent)
@@ -29,17 +36,17 @@ find_package(
29
36
${tolc_entry_SOURCE_DIR}
30
37
REQUIRED)
31
38
32
-
tolc_create_translation(
39
+
tolc_create_bindings(
33
40
TARGET MyLib
34
41
LANGUAGE wasm
35
-
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/wasm-bindings)
42
+
OUTPUT wasm-bindings)
36
43
```
37
44
38
-
Assuming your library is called `MyLib`, and the bindings should be generated to `${CMAKE_CURRENT_BINARY_DIR}/wasm-bindings`.
45
+
Assuming your library is called `MyLib`, and the bindings should be generated to the directory `wasm-bindings`.
39
46
40
47
### Downloading `Emscripten` ###
41
48
42
-
In order to compile your library to `WebAssembly`, you need to download the [`Emscripten compiler`](https://emscripten.org/). This is typically done via their`Emscripten SDK`. Navigate to the directory where you want to install and run the following commands:
49
+
In order to compile your library to `WebAssembly`, you need to download the [`Emscripten compiler`](https://emscripten.org/). This is typically done via the`Emscripten SDK`. Navigate to the directory where you want to install and run the following commands:
43
50
44
51
```shell
45
52
# Download SDK
@@ -71,9 +78,9 @@ $ emsdk.bat install 3.1.3
71
78
$ emsdk.bat activate 3.1.3
72
79
```
73
80
74
-
---
81
+
### Configuring Your Project ###
75
82
76
-
Now when configuring your `CMake` project, pass the toolchain flag `-DCMAKE_TOOLCHAIN_FILE=${EMSDK_DIRECTORY}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake`. Where you need to replace `${EMSDK_DIRECTORY}` with the directory of the previously downloaded `Emscripten SDK`.
83
+
Now when configuring your `CMake` project, pass the toolchain flag `-DCMAKE_TOOLCHAIN_FILE=${EMSDK_DIRECTORY}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake`. Where you need to replace `${EMSDK_DIRECTORY}` with the directory of the previously downloaded `Emscripten SDK`. Note that the directory separator used by `CMake` is always forward slash (`/`), even on Windows.
Looking into `build/tolc` you should see `MyLib.js` aswell as `MyLib.wasm`. `MyLib.js` exports a `Promise` that loads the built `WebAssembly`. Here is an example usage:
95
+
96
+
```javascript
97
+
// run.js
98
+
constloadMyLib=require('./build/MyLib');
99
+
100
+
loadMyLib().then(MyLib=> {
101
+
// From here you can use the C++ functions of your library as usual
102
+
MyLib.myCppFunction();
103
+
});
104
+
```
105
+
106
+
If you want to see what more is supported you can take a look at [the Examples section](./examples.md).
0 commit comments