rust-lang / rust-lang/rust-bindgen

Dynamic Linking Support (windows-msvc)

Open
#2,548 1 comment 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

windows
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

When using bindgen on Windows, it is important to know if the library being linked with dynamically or statically as exported global variables from a DLL need their corresponding import declarations marked dllimport. When using __declspec(dllexport) on the variable declaration, bindgen correctly marks the import declaration with #[link(name = "...")] which causes rustc to mark any variables as dllimport.

However, many Windows native libraries do not use __declspec(dllexport) and use module definition files instead. In this case, bindgen does not know to mark the import declarations with #[link(name = "...")] as required and this typically results in obtuse linker errors such as:

  = note: bindgen_repro.2pvcn21dyln8dfar.rcgu.o : error LNK2019: unresolved external symbol SomeVariable referenced in function _ZN13bindgen_repro4main17h30f7fdf053f3b91dE
          target\debug\deps\bindgen_repro.exe : fatal error LNK1120: 1 unresolved externals      

I've assembled a complete reproducer here which demonstrates this: https://github.com/wesleywiser/bindgen-windows-dll-repro

In order to handle libraries that use module definition files, some kind of configuration option probably needs to be added to bindgen to allow the user to force generation of the #[link] attributes.

In searching the issue list, I found #1974 which is similar but asks for examples where the status quo does not work. This is such a case 🙂

Actual Results
/* automatically generated by rust-bindgen 0.65.1 */

extern "C" {
    pub static mut SomeVariable: ::std::os::raw::c_int;
}
Expected Results
/* automatically generated by rust-bindgen 0.65.1 */

#[link(name = "example")]
extern "C" {
    pub static mut SomeVariable: ::std::os::raw::c_int;
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked bindgen-windows-dll-repro reproducer and compare its generated bindings with the expected #[link(name = "example")] output. Trace how bindgen handles exported global variables and link attributes, then define and validate a configuration option that supports module-definition-file libraries.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.