Search code examples
pythonwindowsrustldmsys2

MSYS2 UCRT64 ld can't find -lpython39


I am trying to use cargo to build a rust project that uses python. The rustup I installed was targeted for the GNU ABI in MSYS2, and when I try to cargo build, and I get an error saying that ld can't find the python library, despite it being installed.

= note: C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: c
= note: C:/msys64/
= note: C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-m  = note: C:/msys64/ucrt64/bin/../lib/gc
= note: C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lpython3

When I run MSYS2 UCRT64, I can run python 3.9, and I can use which python to confirm that it is installed in C:/msys64/ucrt64/bin/python, and when I run ld -lpython39 (that is, not using cargo or anything) I get that same error

$ ld -lpython39
C:\msys64\ucrt64\bin\ld.exe: cannot find -lpython39: No such file or directory

So, does ld require an -L argument to tell it the location of python39 even though it's in the default location? If so, how do I do so with cargo? Am I not supposed to use rustup and use the rust that comes with MSYS2? The rust website advises against this.


Solution

  • I was able to fix this by installing python-devel and adding to my LD_LIBRARY_PATH by adding to my ~/.bashrc

    export LD_LIBRARY_PATH="/usr/lib"
    

    I figured this out because on a fresh install of Ubuntu, I had the exact same error, and bafflingly, the equivalent directory isn't added to that environment variable by default. So on ubuntu I solved this by adding

    export LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH"