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 directly against DLLs for gnu toolchains to eliminate the need for import libraries #478

Closed
GabrielMajeri opened this issue Jul 12, 2017 · 1 comment

Comments

@GabrielMajeri
Copy link
Contributor

GabrielMajeri commented Jul 12, 2017

As far as I can understand, the winapi-i386/x86_64-pc-windows-gnu crates exist because the Rust MinGW toolchain does not include the import libraries for these DLLs, right? Therefore there should no way to link against them.

Except there is, and it requires no import libraries. ld is a really cool linker - it's able to link against DLLs without requiring an import .lib file.

Here is an example. Let's say I am trying to build this using the nightly-gnu toolchain on Windows:

// Some random function.
#[link(name = "audioeng")]
extern "system" { fn AERT_Allocate(); }

fn main() { unsafe { AERT_Allocate(); } }

If I compile this as-is, I get a link error with the ld linker.

 = note: ld: cannot find -laudioeng

But AudioEng is a DLL, and it exists in my C:\Windows\System32\ folder. If I ask ld to also search in that folder, by using a build.rs file:

fn main() {
	println!("cargo:rustc-link-search=native=C:\\Windows\\System32\\");
}

I can now build it just fine:

     Finished dev [unoptimized + debuginfo] target(s) in 0.59 secs
     Running `target\debug\test.exe`

Would it be possible for winapi-rs to drop those auxilliary crates and just add the C:\\Windows\\System32\ folder to the native search dirs for MinGW? It would greatly simplify development.

Note: the folders, on a 64-bit native Windows, are \System32 for 64-bit DLLs and \SysWOW64 for 32-bit DLLs.

@GabrielMajeri GabrielMajeri changed the title Link directly against DLLs with MinGW to eliminate the need for import libraries Link directly against DLLs for gnu toolchains to eliminate the need for import libraries Jul 12, 2017
@retep998
Copy link
Owner

retep998 commented Jul 12, 2017

No.
I'm well are of ld's claimed ability to link against dlls directly and it isn't something I can rely on.
Assuming that winapi is being built on Windows doesn't work, because people might want to cross compile to Windows from linux or Mac which obviously don't have the dlls.
Or what if the user is on an older Windows and trying to target a newer Windows, so their dlls don't have those newer functions?
Or how about linking against the api set dlls which don't even actually exist as files due to the way the windows loader works?

Providing import libraries is something that I can do and ensures linking will always work regardless of what the host system is.

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

2 participants