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

rustc 1.82.0 doesn't add LIBPATH when invoking link.exe #132016

Open
nandin-borjigin opened this issue Oct 22, 2024 · 8 comments
Open

rustc 1.82.0 doesn't add LIBPATH when invoking link.exe #132016

nandin-borjigin opened this issue Oct 22, 2024 · 8 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-windows-msvc Toolchain: MSVC, Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nandin-borjigin
Copy link

nandin-borjigin commented Oct 22, 2024

Rust-to-rust dynamic linking (through import library) on Windows fails with toolchain version 1.82.0.

The build log shows that 1.82.0 is not adding any /LIBPATH arguments when invoking link.exe.

I tried this code: nandin-borjigin/rustc-1.82.0-repro

Image

I expected to see this happen: Successful build

Instead, this happened: LINK : fatal error LNK1181: cannot open input file 'mylib.dll.lib'

Image

Meta

rustc --version --verbose:

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-pc-windows-msvc
release: 1.82.0
LLVM version: 19.1.1
Backtrace

Possibly related

@nandin-borjigin nandin-borjigin added the C-bug Category: This is a bug. label Oct 22, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 22, 2024
@ChrisDenton
Copy link
Member

cc @petrochenkov, I believe there were some changes to linking with msvc?

@nandin-borjigin
Copy link
Author

Adding this build script helps, but I think this should only be considered a workaround and rustc should emit library search path by default.

    println!("cargo:rustc-link-search=native={}/deps", out_dir.display());

@jieyouxu jieyouxu added A-linkage Area: linking into static, shared libraries and binaries O-windows Operating system: Windows O-windows-msvc Toolchain: MSVC, Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed O-windows Operating system: Windows labels Oct 22, 2024
@bjorn3
Copy link
Member

bjorn3 commented Oct 22, 2024

I don't think this was supposed to work out of the box. Either you should make my_lib a cdylib (requiring you to build the cdylib from within the build script rather than specifying it as dependency, at least until artifact dependencies are stable) and link it like a C library (which includes adding it to the linker search path from a build script) or you should link it like a regular rust library and avoid #[link(name = "my_lib.dll")] extern "C" {}.

@petrochenkov
Copy link
Contributor

This is likely from #128370.

@nandin-borjigin
Copy link
Author

Hi @bjorn3 , thanks for the input. I have a few follow up questions:

  1. As per The Rust Reference/Linkage, the difference between dylib and cdylib seems to be whether the produced library is consumable in Rust or other languages. If mylib is only going to be used in Rust, do we need to use cdylib?
  2. If mylib is changed to cdylib, the compiler would warn saying that mylib provides no linkable target. This should be the reason why you mentioned mylib should be built form the build script in this case. I wonder how I should properly do that in build script? Will triggering cargo build ../mylib be sufficient? or do I have to take care of (debug|release) profiles and crate features as well?

@bjorn3
Copy link
Member

bjorn3 commented Oct 23, 2024

mylib is only going to be used in Rust, do we need to use cdylib?

If you want to use it as regular dependency you should make it a dylib.

Will triggering cargo build ../mylib be sufficient? or do I have to take care of (debug|release) profiles and crate features as well?

You probably want to forward the build profile. Also make sure to set --target-dir to some subdirectory of OUT_DIR. If you have any relevant crate features you will also need to forward them.

By the way what do you want to achieve in the end for which you see dylibs as solution?

@nandin-borjigin
Copy link
Author

nandin-borjigin commented Oct 25, 2024

By the way what do you want to achieve in the end for which you see dylibs as solution?

mylib implements an Allocator, which is configured as the GlobalAllocator in app. But we don't want to get into an infinite recursion where the allocations in mylib::Allocator calls into mylib::Allocator recursively.

@bjorn3
Copy link
Member

bjorn3 commented Oct 26, 2024

You can call into the system allocator from mylib using std::alloc::System or you can directly use mmap.

@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 9, 2024
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-msvc Toolchain: MSVC, Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants