Skip to content

Cross-compilation issue with linker search paths #139966

Open

Description

@kognise

Describe the bug

I am encountering an issue when cross-compiling with a Windows library installed, libpthread in this example. GCC is incorrectly picking up the Windows library instead of the system one.

I came across this in the context of a Rust project with a build.rs file. First, the build file has to be compiled for and executed on the host platform with GCC. Afterward, the rest of the project can be compiled for the target with MinGW.

NIX_LDFLAGS contains the Windows library when it's installed, which causes GCC to error because it doesn't obviously understand the format. If I manually remove it from the environment, then MinGW doesn't know where to pick up the library and the build fails.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Clone https://github.com/kognise/crosscomp-mcve
  2. Run nix-shell --pure
  3. Run cargo clean && cargo build --target x86_64-pc-windows-gnu

Expected behavior

I'm relatively new to Nix, so I honestly don't know what the right solution to this is.

The expected behavior from a developer's perspective is simply for GCC to find the Linux libpthread and for MinGW to find the Windows version so that everything can compile nicely.

Screenshots

Additional context

I would just like to mention that this isn't a problem with Rust exclusively, or the overlay I'm using. This is just the simplest way I've been able to reproduce it, and how I came across the issue during real-world development.

Notify maintainers

Not sure who I should specifically ping for this, so apologies if I incorrectly mentioned you.

@matthewbauer (from MinGW derivation history)

@madjar @cstrahan @globin @Havvy (from rustc maintainers, probably didn't need to ping all of you so sorry)

Metadata

  • system: "x86_64-linux"
  • host os: Linux 5.10.67, NixOS, 21.05.3367.fd8a7fd07da (Okapi)
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.3.15
  • channels(root): "nixos-21.05.3367.fd8a7fd07da"
  • channels(kognise): "nixos-21.05-21.05.3367.fd8a7fd07da"
  • nixpkgs: /nix/var/nix/profiles/per-user/root/channels/nixos

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute:
# a list of nixos modules affected by the problem
module:

Activity

  1. added
    0.kind: bug
    on Sep 29, 2021
  2. added
    6.topic: cross-compilation
    6.topic: windows
    on Sep 29, 2021
  3. nixos-discourse commented on Sep 29, 2021

    @nixos-discourse

    This issue has been mentioned on NixOS Discourse. There might be relevant details there:

    https://discourse.nixos.org/t/cross-compile-rust-for-windows/15259/3

  4. lammermann commented on Oct 6, 2021

    @lammermann
    Contributor

    I had a similar problem in one of my projects. The advice in this link helped me nix-community/naersk#181 (comment) . Hope it works for you too.

  5. stale commented on Apr 17, 2022

    @stale

    I marked this as stale due to inactivity. → More info

  6. added
    2.status: stale
    on Apr 17, 2022
  7. zopsicle commented on Jan 17, 2023

    @zopsicle

    A way to work around this is to not put windows.pthreads in the build inputs of the shell, but to set CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS. This will tell Cargo to tell rustc about the directory containing libpthread.a for Windows, but only when actually building a crate for Windows.

    CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS = "-L native=${windows.pthreads}/lib";
  8. nixos-discourse commented on Aug 17, 2023

    @nixos-discourse
  9. RossSmyth commented on Feb 15, 2025

    @RossSmyth
    Contributor

    This is still a really annoying issue :(

    The way I solved it is by doing
    RUSTFLAGS = libs.optional stdenv.hostPlatform.isMinGW "-L native=${windows.pthreads}/lib";

    This line

    ++ lib.optionals stdenv.hostPlatform.isMinGW [ windows.pthreads ];

    Doesn't work. I do not know why.

  10. removed
    2.status: stale
    on Feb 15, 2025
  11. pinage404 commented on Feb 15, 2025

    @pinage404
    Contributor

    i have been able to build Rust targeting Windows with this

    {
      contents = [
        rust-toolchain
        pkgs.stdenv.cc
        (pkgs.pkgsCross.mingwW64.stdenv.cc.override ({
          extraBuildCommands = ''
            printf '%s' '-L ${pkgs.pkgsCross.mingwW64.windows.mingw_w64_pthreads}/lib' >>$out/nix-support/cc-ldflags
          '';
        }))
      ];
    }

    https://gitlab.com/pinage404/git-gamble/-/blob/ac671d9fa2bd880e2adb596718facdb5f0519cb3/nix/packages/container-image-rust-windows/default.nix#L27

  12. RossSmyth commented on Feb 25, 2025

    @RossSmyth
    Contributor

    Yeah that should be equivalent, but really one shouldn't have to do these workarounds. They are pervasive if you look around nixpkgs and github just to get around this issue.

  13. added
    2.status: stale
    on Aug 24, 2025
  14. removed
    2.status: stale
    on Oct 20, 2025
  15. Sewer56 commented on Oct 20, 2025

    @Sewer56
    Contributor

    It would be ideal if such workaround could be avoided- that is true, unfortunately.

    I guess it's just another case of

    I have to write a flake from scratch- before I can build or run random project from the web, and since the repo probably wouldn't accept a flake.nix for obscure OS in its repository, I have to do it for every project I touch from scratch every time.

    Or however you want to phrase it.

  16. added
    2.status: stale
    and removed
    2.status: stale
    on Oct 20, 2025
  17. RossSmyth commented on Feb 26, 2026

    @RossSmyth
    Contributor

    If anyone see this issue again, please, please let us know! We are trying to figure out what is going on here!

    1. Copy the build full log, not just the linker error
    2. Upload the full, exact Nix code and CLI command ran. We would like to know the exact Nixpkgs commit used, the source code, and how it was invoked. Please lock your inputs! Either with flakes or another locking method.
    3. Build it again, and this time set env.RUSTFLAGS= "-Clink-arg=-v -Wlinker-messages" and upload the full build log, nix source code, and cli command used.
  18. added
    2.status: stale
    on Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: bug2.status: stale6.topic: cross-compilation6.topic: windows

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions