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

link error undefined reference #1890

Closed
toplinuxsir opened this issue Sep 3, 2020 · 8 comments
Closed

link error undefined reference #1890

toplinuxsir opened this issue Sep 3, 2020 · 8 comments

Comments

@toplinuxsir
Copy link

I link c++ libray (libmylib.so ) , with g++ -lmylib , It works ok, and link success,
But with rust ,I have pass the -lmylib, compiles ok, and always link error:
'
/usr/bin/ld: callc.d2qs7r1b-cgu.0:(.text._ZN5callc4main17h70fed83690e9e7b8E+0x106): undefined reference to USB5622E_GetNetCfgA' /usr/bin/ld: callc.d2qs7r1b-cgu.0:(.text._ZN5callc4main17h70fed83690e9e7b8E+0x351): undefined reference to USB5622E_AI_GetRateInfo'
/usr/bin/ld: callc.d2qs7r1b-cgu.0:(.text._ZN5callc4main17h70fed83690e9e7b8E+0x4d0): undefined reference to USB5622E_AI_VerifyParam' /usr/bin/ld: callc.d2qs7r1b-cgu.0:(.text._ZN5callc4main17h70fed83690e9e7b8E+0x4f7): undefined reference to USB5622E_AI_InitTask'
/usr/bin/ld: callc.d2qs7r1b-cgu.0:(.text._ZN5callc4main17h70fed83690e9e7b8E+0x508): undefined reference to USB5622E_AI_StartTask' /usr/bin/ld: callc.d2qs7r1b-cgu.0:(.text._ZN5callc4main17h70fed83690e9e7b8E+0x581): undefined reference to USB5622E_AI_SendSoftTrig'
/usr/bin/ld: callc.d2qs7r1b-cgu.0:(.text._ZN5callc4main17h70fed83690e9e7b8E+0x5a0): undefined reference to USB5622E_AI_ReadBinary' /usr/bin/ld: callc.d2qs7r1b-cgu.0:(.text._ZN5callc4main17h70fed83690e9e7b8E+0x875): undefined reference to USB5622E_DEV_Release'
/usr/bin/ld: callc.d2qs7r1b-cgu.0:(.text._ZN5callc4main17h70fed83690e9e7b8E+0x961): undefined reference
'

@emilio
Copy link
Contributor

emilio commented Sep 3, 2020

Bindgen doesn't take care of linking anything. Are you passing the right flags to rustc? Like println!("cargo:rustc-link-lib=mylib") from build.rs or such?

@toplinuxsir
Copy link
Author

toplinuxsir commented Sep 3, 2020

@emilio
Yes, I have pass the right flags to rustc
my build.rs

fn main() {
    println!(r"cargo:rustc-link-search=/usr/lib/mylib");


    println!("cargo:rustc-link-lib=static=stdc++");
    println!(r"cargo:rustc-link-lib=mylib");
}

@nagisa
Copy link
Member

nagisa commented Sep 3, 2020

I started hitting something similar recently. More often than not it is because rustc reorders the -l flags before their use because it is missing the dependency information due to missing #[link(name="...")].

@toplinuxsir
Copy link
Author

@nagisa How to resolve it ? Thanks

@emilio
Copy link
Contributor

emilio commented Nov 13, 2020

I think this is not a bindgen issue itself given the above... But a minimal repro would go a long way. Please reopen if this turns out to be an issue that should be fixed in bindgen.

@emilio emilio closed this as completed Nov 13, 2020
@nagisa
Copy link
Member

nagisa commented Nov 13, 2020

@emilio bindgen currently does not provide the means to add #[link(name="...")] attributes to extern blocks it generates. Something like this reproducer from rust-lang/rust#65610 should be trivially adaptable to use bindgen.

@mrozekma
Copy link

I'm a bindgen novice, but am confused why this isn't a bigger problem for users. I have to edit the generated bindings to add #[link(...)] attributes on the functions or the final link will fail when I try to call them, but the documentation seems to expect I'll just be able to use the generated bindings as-is.

@mengwanguc
Copy link

I totally agree with @mrozekma , and I do think it's a big problem for me as I need to add #[link(name="...")] manually for every function I'm calling in the generated "binding.rs". bindgen should be able to do it for users

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

5 participants