Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined reference to napi_... issues #209

Open
jdoig opened this issue Jan 30, 2023 · 11 comments
Open

undefined reference to napi_... issues #209

jdoig opened this issue Jan 30, 2023 · 11 comments

Comments

@jdoig
Copy link

jdoig commented Jan 30, 2023

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
Copy link
Collaborator

sehz commented Jan 31, 2023

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

@jdoig
Copy link
Author

jdoig commented Jan 31, 2023

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

Sorry 🤦 , 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
Copy link
Collaborator

sehz commented Feb 1, 2023

@simlay Please take look at this

@simlay
Copy link
Contributor

simlay commented Feb 1, 2023

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
Copy link
Author

jdoig commented Feb 1, 2023

@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 😄

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 😞

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 😆 :

  • I have libffi and libffi-dev installed on my machine already.
  • I've installed node-gyp, node-ffi (based on some searches for node ffi tools and libraries).
  • Used cmake-js install to: Install Node.js distribution files if needed.

But still the same problem.

@vmoroz
Copy link

vmoroz commented Feb 3, 2023

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
Copy link
Author

jdoig commented Feb 6, 2023

Ok this is utterly embarrassing 😳 ... 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 😕

@jdoig
Copy link
Author

jdoig commented Feb 9, 2023

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 😕

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
Copy link

yannleretaille commented May 22, 2023

I can confirm as I ran into the same issue:

  • Arch Linux (kernel 6.2.12)
  • node v19.9.0 from extra/nodejs
  • rust 1.71-nightly
  • build.rs:
    fn main() {
        node_bindgen::build::configure();
    }
    
  • Cargo.toml
    [package]
    name = "project-name"
    edition = "2021"
    
    [lib]
    crate-type = ["cdylib"]
    
    [dependencies]
    node-bindgen = { version = "5.1" }
    
    [...]
    
    [build-dependencies]
    node-bindgen = { version = "5.1", default-features = false, features = ["build"]}
    

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
Copy link

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.

  • Arch Linux (Kernel: 6.3.9-zen)
  • Node v16.20.0
  • Rust 1.70.0

@xssc
Copy link

xssc commented Aug 1, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants