infinyon / node-bindgen

Easy way to write Node.js module using Rust
Apache License 2.0
495 stars 43 forks source link

undefined reference to `napi_...` issues #209

Open jdoig opened 1 year ago

jdoig commented 1 year ago

I'm getting an error:

error: linking with `cc` failed: exit status: 1
|
 = note: LC_ALL="C" PATH="<huge PATH string omitted>"

= note: /usr/bin/ld: /home/james/Documents/platform/shared/library/openid_token_verifier/target/release/deps/libnj_core-381c20950f1364ad.rlib(nj_core-381c20950f1364ad.nj_core.3ac555e3-cgu.15.rcgu.o): in function `nj_core::module::init_module':
          nj_core.3ac555e3-cgu.15:(.text._ZN7nj_core6module11init_module17h689025d98d800e2fE+0x10): undefined reference to `napi_module_register'
          /usr/bin/ld: /home/james/Documents/platform/shared/library/openid_token_verifier/target/release/deps/libnj_core-381c20950f1364ad.rlib(nj_core-381c20950f1364ad.nj_core.3ac555e3-cgu.0.rcgu.o): in function `nj_core::basic::JsExports::define_property':
          nj_core.3ac555e3-cgu.0:(.text._ZN7nj_core5basic9JsExports15define_property17h2ddbe1e6c72b4dd1E+0xf5): undefined reference to `napi_define_properties'
          collect2: error: ld returned 1 exit status

  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

Could someone please advise me on what I need to do to get my project building?

sehz commented 1 year ago

Can you provide OS, Rust, and other information to debug this?

jdoig commented 1 year ago

Can you provide OS, Rust, and other information to debug this?

Sorry :facepalm: , of course @sehz : OS:

NAME="Pop!_OS"
VERSION="22.04 LTS"

Rust (I've tried with both of these:

stable-x86_64-unknown-linux-gnu
nightly-x86_64-unknown-linux-gnu

I've tried compiling with node-bindgen 4.3 and 5 but get the same errors.

sehz commented 1 year ago

@simlay Please take look at this

simlay commented 1 year ago

Hmm. This looks to be related to how the nj-core is linked to node at completive. @jdoig What's your nodejs setup like? Like, I'm using fnm to manage which nodejs I'm using and this builds with node 14 (as that's what a project I work on sometimes uses).

Though, now that I look at the node repo, it looks like napi_module_register is soon to be deprecated but napi_define_properties isn't.

jdoig commented 1 year ago

@simlay My node setup is very much that of someone who's a node novice/hobbyist . My current goal is to port some of our auth' code over to a rust library I can call from Python/Node/etc for our other developers. So that we have ve a single uniformed auth library with strong typing and good error handling. Node isn't my runtime of choice :smile:

Typically I've node 14/16/18 installed and controlled via nvm. And now, to follow your setup I've also installed fnm and tired this using: 14/18/system versions of node though fnm` ... all with the same error still :disappointed:

I've checked my gcc, make and python versions against Node's toolchain requirements.

And here's some "clutching-at-straws" steps I've taken :laughing: :

But still the same problem.

vmoroz commented 1 year ago

Though, now that I look at the node repo, it looks like napi_module_register is soon to be deprecated but napi_define_properties isn't.

Please note that while we consider simplification of Node-API module registration and stopping use of napi_module_register, we do not remove any existing API functions. To keep the ABI compatibility this function will be there for a long time.

jdoig commented 1 year ago

Ok this is utterly embarrassing :flushed: ... The function I was wanting to expose had it's

#[node_bindgen()]

commented out.

One interesting issue with this is: Now I have un-commented that line everything works ... as long as I don't have a build.rs as soon as I add it back into the project I get exactly the same error as before :confused:

jdoig commented 1 year ago

This is still causing problems:

  1. node_bindgen::build::configure(); in build.rs causes it not to build (though looking at the code that configure should noop on Linux right?) with the initial error.
  2. If I remove that line from the build.rs both cargo and nj-cli can build my library ... BUT cargo test fails with the initial error.

So I can now build my node binary and it all seems to be working form inside a node session ... I just cant run my unit tests.

Update: To rule out any weirdness in my local terminal session I started up a fresh Debian Docker container: apt installed nodejs and gcc, then installed rust through rustup-init.sh ... same results, I can build but cant test :confused:

Update: I also have tried compiling the node-bindgen examples with the same issues.

Update: I'm not sure if it's at all relevant but I've also tried compiling Neon examples and they compile fine and test fine with no linking issues.

Update: Just had a colleague with an M2 Mac try this with similar results. Can cargo build cant cargo test the /examples folder of this repo without getting linking errors:

 param git:(master) cargo test
   Compiling nj-example-param v0.0.0 (/Users/xxx/node-bindgen/examples/param)
    Finished test [unoptimized + debuginfo] target(s) in 0.28s
     Running unittests src/lib.rs (/Users/xxx/node-bindgen/examples/target/debug/deps/nj_example_param-67c9517494263fc8)
dyld[80564]: symbol not found in flat namespace (_napi_create_int32)
error: test failed, to rerun pass '--lib'

Caused by:
  process didn't exit successfully: `/Users/xxx/node-bindgen/examples/target/debug/deps/nj_example_param-67c9517494263fc8` (signal: 6, SIGABRT: process abort signal)

(he got the same error when he did pass the --lib flag too)

yannleretaille commented 1 year ago

I can confirm as I ran into the same issue:

Results in this error:

> ~/.cargo/bin/nj-cli build -o dist 
  |
  = note: LC_ALL="C" PATH="~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:~/.cargo/bin:~/.local/bin:/usr/local/bin:/usr/bin:/var/lib/snapd/snap/bin:/usr/local/sbin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl" VSLANG="1033" "cc" "-m64" "/tmp/rustcHYSVpD/symbols.o" "~/project-folder/target/debug/build/project-name-c045ed565816713e/build_script_build-c045ed565816713e.1u26bdjqvo8hjbno.rcgu.o" "~/project-folder/target/debug/build/project-name-c045ed565816713e/build_script_build-c045ed565816713e.484c40cyeng6z092.rcgu.o" "~/project-folder/target/debug/build/project-name-c045ed565816713e/build_script_build-c045ed565816713e.g1afhabpyeyft7b.rcgu.o" "~/project-folder/target/debug/build/project-name-c045ed565816713e/build_script_build-c045ed565816713e.pzta0jecpcl93jf.rcgu.o" "~/project-folder/target/debug/build/project-name-c045ed565816713e/build_script_build-c045ed565816713e.qt7jpx7nnsf9p38.rcgu.o" "~/project-folder/target/debug/build/project-name-c045ed565816713e/build_script_build-c045ed565816713e.1xta2omldd5rwbqj.rcgu.o" "-Wl,--as-needed" "-L" "~/project-folder/target/debug/deps" "-L" "~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "~/project-folder/target/debug/deps/libnode_bindgen-72f039e41aead63a.rlib" "~/project-folder/target/debug/deps/libnj_build-1aaf22f736cb42df.rlib" [...] "~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-35b8a4bd2de4e62e.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "~/project-folder/target/debug/build/project-name-c045ed565816713e/build_script_build-c045ed565816713e" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
  = note: /usr/bin/ld: ~/project-folder/target/debug/deps/libnj_core-aaa6e789a8145b4c.rlib(nj_core-aaa6e789a8145b4c.nj_core.8b52fda313818e30-cgu.14.rcgu.o): in function `nj_core::module::init_module':
          ~/.cargo/git/checkouts/node-bindgen-48597ec4d5b120bf/dba17af/nj-core/src/module.rs:90: undefined reference to `napi_module_register'
          /usr/bin/ld: ~/project-folder/target/debug/deps/libnj_core-aaa6e789a8145b4c.rlib(nj_core-aaa6e789a8145b4c.nj_core.8b52fda313818e30-cgu.0.rcgu.o): in function `nj_core::basic::JsExports::define_property':
          ~/.cargo/git/checkouts/node-bindgen-48597ec4d5b120bf/dba17af/nj-core/src/basic.rs:963: undefined reference to `napi_define_properties'
          collect2: error: ld returned 1 exit status

  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `project-name` (build script) due to previous error

Removing/Uncommenting the node_bindgen::build::configure() line in build.rs fixes the issue and the project builds correctly.

szalovszky commented 1 year ago

I can also confirm as I also ran into the same issue. Simply deleting build.rs or not having node_bindgen::build::configure() in it fixes the problem and the bindings still work as expected.

xssc commented 11 months ago

getting the same issue on ubuntu 22.0.4 with nodejs 16.20.1 & rust 1.71.0, removing build.rs works