3

I'm trying to create a NodeJS application with a Wasm database layer. I use Rust, Diesel as a database driver and wasm-pack as a WebAssembly compiler.

When I try to build my service with wasm-pack, it fails trying to link the libpq library. The PQ_LIB_DIR environment variable is set, and the diesel-cli works without any issue, but when I try to compile my service it fails with the following error:

error: linking with `rust-lld` failed: exit code: 1
  = note: rust-lld: error: unable to find library -llibpq

How can I fix it?

My system:

  • OS: Windows 10
  • Rust: 1.38.0
  • wasm-pack: 0.8.1
  • diesel: 1.4.0
  • PostgreSQL: 12
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Lodin
  • 2,028
  • 2
  • 18
  • 29

2 Answers2

3

It's not possible to link to a system library from WebAssembly. You have to use an implementation written in pure Rust code to make it work. You also could use a JavaScript library. But it always has to be inside of the JavaScript runtime environment.

Michael
  • 2,528
  • 3
  • 21
  • 54
0

libpq is available inside the postgreSQL installation binary

For example, PosgreSQL Binary Download Site if open the zip distribution postgresql-12.0-1-windows-x64-binaries.zip you can see these 2 files:

  • pgsql\lib\libpq.dll
  • pgsql\lib\libpq.lib

You need to specify the LD_LIBRARY_PATH or you can specify the -L in the compiler arguments to tell the folder path which will auto pick those files

If you have used the installer the same file should be there inside the installation folder

You can also refer this stackoverflow link How to I tell Rust where to look for a static library?

--EDIT--

To combine a C library inside a web assemble distribution, we can use this guide -
Compiling an Existing C Module to WebAssembly

tshepang
  • 12,111
  • 21
  • 91
  • 136
Dickens A S
  • 3,824
  • 2
  • 22
  • 45
  • 1
    Unfortunately, that didn't help. Moreover, the `-L` option that leads to the PostgreSQL directory existed from the very start via the `PQ_LIB_DIR` but `lld` continue failing. Somewhere I have seen the idea that `libpq` should be WASM-complied as well; otherwise, the linker would be unable to find it. Probably, it is an answer. – Lodin Nov 04 '19 at 13:06
  • yes, if you wanted run something browser then it needs to be embedded in the wasm binary distribution as static library – Dickens A S Nov 05 '19 at 07:30