2022-04-27 03:12:50 +00:00
# "native-lib" sample introduction
This sample demonstrates how to write required interfaces in native library, build it into a shared library and register the shared library to iwasm.
The native library should provide `get_native_lib` API for iwasm to return the native library info, including the module name, the native symbol list and the native symbol count, so that iwasm can use them to regiter the native library, for example:
```C
static int
2022-10-06 12:21:21 +00:00
foo_wrapper(wasm_exec_env_t exec_env, int x, int y)
2022-04-27 03:12:50 +00:00
{
return x + y;
}
#define REG_NATIVE_FUNC(func_name, signature) \
{ #func_name , func_name##_wrapper, signature, NULL }
static NativeSymbol native_symbols[] = {
REG_NATIVE_FUNC(foo, "(ii)i")
};
uint32_t
get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols)
{
*p_module_name = "env";
*p_native_symbols = native_symbols;
return sizeof(native_symbols) / sizeof(NativeSymbol);
}
```
## Preparation
2024-01-24 05:42:45 +00:00
Please install WASI SDK, download the [wasi-sdk release ](https://github.com/WebAssembly/wasi-sdk/releases ) and extract the archive to default path `/opt/wasi-sdk` .
2022-04-27 03:12:50 +00:00
## Build the sample
```bash
mkdir build
cd build
cmake ..
make
```
`iwasm` , one wasm module `test.wasm` and two shared libraries `libtest_add.so` , `libtest_sqrt.so`
will be generated.
## Run workload
2022-10-07 07:17:36 +00:00
### Linux
2022-04-27 03:12:50 +00:00
```bash
cd build
2022-10-28 11:31:21 +00:00
./iwasm --native-lib=./libtest_add.so --native-lib=./libtest_sqrt.so --native-lib=./libtest_hello.so --native-lib=./libtest_hello2.so wasm-app/test.wasm
2022-04-27 03:12:50 +00:00
```
2022-10-07 07:17:36 +00:00
### macOS
```bash
cd build
2022-10-28 11:31:21 +00:00
./iwasm --native-lib=libtest_add.dylib --native-lib=libtest_sqrt.dylib --native-lib=libtest_hello.dylib --native-lib=libtest_hello2.dylib wasm-app/test.wasm
2022-10-07 07:17:36 +00:00
```
2022-04-27 03:12:50 +00:00
The output is:
```bash
2023-08-10 01:26:52 +00:00
init_native_lib in test_hello2.c called
2022-04-27 03:12:50 +00:00
Hello World!
10 + 20 = 30
sqrt(10, 20) = 500
2022-10-27 07:06:14 +00:00
test_hello("main", 0x0, 0) = 41
2022-10-28 11:31:21 +00:00
malloc(42) = 0x24e8
test_hello("main", 0x24e8, 42) = 41
2022-10-27 07:06:14 +00:00
Message from test_hello: Hello, main. This is test_hello_wrapper!
2022-10-28 11:31:21 +00:00
test_hello2("main", 0x0, 0) = 85
malloc(86) = 0x24e8
test_hello2("main", 0x24e8, 86) = 85
2023-08-10 01:26:52 +00:00
Message from test_hello2: Hello, main. This is test_hello2_wrapper! Your wasm_module_inst_t is 0x7fe0e6804280.
deinit_native_lib in test_hello2.c called
2022-04-27 03:12:50 +00:00
```