【问题标题】:Use ngspice library in WebAssembly在 WebAssembly 中使用 ngspice 库
【发布时间】:2020-07-22 23:10:40
【问题描述】:

在将 ngspice 用作 webassembly (wasm) 项目中的库时,我需要一些帮助。

我安装了 emsdk 和最新版本的 emcc (1.39.20) 并下载了 ngspice 版本 32 的源代码。

最令我惊讶的是,我能够通过关注this guide 将 ngspice 编译为 wasm 目标:

emconfigure  ./configure --with-ngshared --disable-debug
emmake make

(我必须通过将.out.js a.out.wasm 添加到此行来稍微修补configure 以通过检查:)

# The possible output files:
ac_files="a.out a.out.js a.out.wasm conftest.exe conftest a.exe a_out.exe b.out conftest.*"

这产生了一个libngspice.so.0.0.0 文件,我试图从 C++ 代码链接到该文件。然而,duplicate symbol: main 失败了。所以看起来libngspice.so.0.0.0 包含一个main 函数,如果我正确理解配置脚本的--with-ngshared 的目的,它不应该存在。

于是我手动将ngspice的main.c中的main函数去掉,用上面的方法重新编译。这次我可以成功编译我自己的项目,链接到 ngspice。然而,当我调用ngSpice_Init 时,我收到以下运行时错误:

stderr Note: can't find init file.

exception thrown: RuntimeError: unreachable executed,@http://localhost:8001/sim.js line 1802 > WebAssembly.instantiate:wasm-function[67]:0x24e9
@http://localhost:8001/sim.js line 1802 > WebAssembly.instantiate:wasm-function[88]:0x423b
...

最少的可重现步骤:

  1. 如上编译ngspice
  2. 使用em++ -o sim.html sim.cpp lib/libngspice.so编译下面的代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "sharedspice.h"

using namespace std;


int recieve_char(char * str, int id, void* p){
    printf("recieved %s\n", str);
}

int recieve_stat(char* status, int id, void* p){
    printf("status: %s\n", status);
}

int ngexit(int status, bool unload, bool exit, int id, void* p){
    printf("exit: %d\n", status);
}

int recieve_data(vecvaluesall* data, int numstructs, int id, void* p){
    printf("data recieved: %f\n", data->vecsa[0]->creal);
}

int recieve_init_data(vecinfoall* data, int id, void* p){
    printf("init data recieved from: %d\n", id);
}

int ngrunning(bool running, int id, void* p){
    if(running){
        printf("ng is running\n");
    }else{
        printf("ng is not running\n");
    }
}


int main(){


    ngSpice_Init(&recieve_char, &recieve_stat, &ngexit,
             &recieve_data, &recieve_init_data, &ngrunning, (void*)NULL);

    char** circarray = (char**)malloc(sizeof(char*) * 7);
    circarray[0] = strdup("test array");
    circarray[1] = strdup("V1 1 0 1");
    circarray[2] = strdup("R1 1 2 1");
    circarray[3] = strdup("C1 2 0 1 ic=0");
    circarray[4] = strdup(".tran 10u 3 uic");
    circarray[5] = strdup(".end");
    circarray[6] = NULL;
    ngSpice_Circ(circarray);

    ngSpice_Command("run");


    return 0;
}

那么有人可以帮我正确地将 ngspice 库编译为 wasm 目标吗?

(在有人问之前,是的,我见过this question,但没有多大帮助)

【问题讨论】:

    标签: c++ c static-linking webassembly emscripten


    【解决方案1】:

    在对 ngspice 源进行多次更改后,我能够编译库和示例代码。有关如何将 ngspice 编译为 wasm 的补丁和指南,可以在 here 找到。

    (导致我的问题中显示的错误的问题是示例代码,没有从通过签名应该返回 int 的函数返回任何内容。这在 wasm 中是不允许的。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-10
      • 2018-01-24
      • 2018-09-11
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      • 2022-10-12
      • 1970-01-01
      相关资源
      最近更新 更多