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

Unable to link rust static lib to C code on 1.57.0 #91974

Open
PunitLodha opened this issue Dec 15, 2021 · 7 comments
Open

Unable to link rust static lib to C code on 1.57.0 #91974

PunitLodha opened this issue Dec 15, 2021 · 7 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-windows Operating system: Windows

Comments

@PunitLodha
Copy link
Contributor

I am working on ccextractor, and recently the tests failed on rustc 1.57.0, with the error:-

"D:\a\ccextractor\ccextractor\windows\ccextractor.sln" (default target) (1) ->
"D:\a\ccextractor\ccextractor\windows\ccextractor.vcxproj" (default target) (2) ->
(Link target) -> 
  ccx_rust.lib(std-f87c887dcbebcf7e.std.06878a67-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol __imp_BCryptGenRandom referenced in function _ZN3std6thread5local4fast12Key$LT$T$GT$14try_initialize17h6f0eaa92973cfc73E [D:\a\ccextractor\ccextractor\windows\ccextractor.vcxproj]
  D:\a\ccextractor\ccextractor\windows\x64\Release\ccextractorwin.exe : fatal error LNK1120: 1 unresolved externals [D:\a\ccextractor\ccextractor\windows\ccextractor.vcxproj]

Error relates to an undefined symbol __imp_BCryptGenRandom referenced in std:: thread::localfastkey::try_initialize
Switching to rustc 1.56.0 or below does not cause any errors

Here are the logs for 1.57.0 test run, and here for 1.56.0 test run.

Looking into the static libraries, only the lib built with 1.57.0 has the symbol specified in the error

C:\ccextractor>dumpbin /symbols ccx_rust_1-56.lib | FIND "BCrypt"

C:\ccextractor>dumpbin /symbols ccx_rust_1-57.lib | FIND "BCrypt"
3283 00000000 UNDEF  notype       External     | __imp_BCryptGenRandom
@PunitLodha PunitLodha added the C-bug Category: This is a bug. label Dec 15, 2021
@hkratz
Copy link
Contributor

hkratz commented Dec 15, 2021

Are you targetting Windows XP? #84096 introduced a dependency on BCryptGenRandom which is not available in XP. XP support has been dropped a while ago.

@rustbot label O-windows A-linkage

@PunitLodha
Copy link
Contributor Author

I am using the i686-pc-windows-msvc target. Is that causing this issue?

@rustbot rustbot added A-linkage Area: linking into static, shared libraries and binaries O-windows Operating system: Windows labels Dec 15, 2021
@ChrisDenton
Copy link
Member

You need to link Bcrypt.lib as well. When using a static library outside of Rust, the required import libraries won't be linked automatically.

david-cattermole added a commit to david-cattermole/mayaMatchMoveSolver that referenced this issue Dec 31, 2021
A EvaluationObjects object was added to store all required nodes for
evaluation, rather than using 'rust::Vec' in C++ because it appears
it wont link to the library.

Link against 'Bcrypt' library because from Rust 1.57+ it's now required.
rust-lang/rust#91974

Issue #114.
pkgw added a commit to tdsmith/librsvg-feedstock that referenced this issue Apr 5, 2022
As of Rust 1.57, we need to make sure to link to bcrypt.lib:

rust-lang/rust#91974
scentini added a commit to bazelbuild/rules_rust that referenced this issue Apr 28, 2022
…|shared_library` (#1299)

A Rust `staticlib` or `cdylib` doesn't need to propagate linker inputs of its dependencies, except for shared libraries.

- The tests are currently disabled on osx because the CI [Toolchain does not support dynamic linking]( https://buildkite.com/bazel/rules-rust-rustlang/builds/6126#a83dbb56-50b0-4a95-bb39-09e3a78ed0d0).
- I also had to link to `Bcrypt.lib` => rust-lang/rust#91974
heinrich5991 added a commit to heinrich5991/ddnet that referenced this issue Jul 12, 2022
heinrich5991 added a commit to heinrich5991/ddnet that referenced this issue Sep 9, 2022
heinrich5991 added a commit to heinrich5991/ddnet that referenced this issue Sep 25, 2022
heinrich5991 added a commit to heinrich5991/ddnet that referenced this issue Oct 7, 2022
@bjorn3
Copy link
Member

bjorn3 commented Jul 29, 2023

If you compile the staticlib with --print native-static-libs rustc will print all extra linker arguments you need.

@Luthaf
Copy link

Luthaf commented May 2, 2024

It looks like bcrypt is no longer included in --print native-static-libs for rustc 1.78, which make linking fail with the error above in my project:

Rust 1.77.2: https://github.com/lab-cosmo/metatensor/actions/runs/8907669658/job/24461939439

Cargo default link libraries are: kernel32;advapi32;bcrypt;ntdll;userenv;ws2_32;$<$CONFIG:Debug:msvcrtd>

($<$<CONFIG:Debug>:msvcrtd> is some manual post-processing, cargo returns msvcrt)

Rust 1.78.0: https://github.com/lab-cosmo/metatensor/actions/runs/8925096694/job/24512982693?pr=605

Cargo default link libraries are: kernel32;advapi32;ntdll;userenv;ws2_32;$<$CONFIG:Debug:msvcrtd>

I am getting this list in CMake by calling cargo rustc --target=... -- --print=native-static-libs on a new empty project. Could it be that this is now detecting whether there is actually a need for bcrypt inside the project?

@bjorn3
Copy link
Member

bjorn3 commented May 3, 2024

I believe instead of referencing an external import library for bcrypt we now embed one inside the staticlib we output, thus avoiding the need to specify it as linker arg.

@ChrisDenton
Copy link
Member

Yes std no longer uses bcrypt. If another crate is relying on it then --print=native-static-libs won't be aware unless that crate is included when it's building.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-windows Operating system: Windows
Projects
None yet
Development

No branches or pull requests

6 participants