Problems building Cargo on Windows - gcc not found


#1

I’ve been trying to build Cargo on Windows, but I’ve been stumbling on a few places.

The main issue being, cargo build complains GCC is not found. The error message links to https://github.com/alexcrichton/gcc-rs#compile-time-requirements which says GCC from MSys should be installed, but from what @alexcrichton mentioned in IRC the GCC bundled with the Rust installation should be used.

Is that meant to be done automatically by Cargo, or do I need to add Rust’s GCC to the PATH manually? I tried the later but it still didn’t work (different error message)

Full error message:

$ cargo build
   Compiling url v1.2.3
   Compiling miniz-sys v0.1.7
   Compiling fs2 v0.3.0
   Compiling term v0.4.4
Build failed, waiting for other jobs to finish...
error: failed to run custom build command for `miniz-sys v0.1.7`
process didn't exit successfully: `D:\devel\rust\cargo0\target\debug\build\miniz-sys-60c8d67696f63a43\build-script-build` (exit code: 101)
--- stdout
TARGET = Some("x86_64-pc-windows-gnu")
OPT_LEVEL = Some("0")
PROFILE = Some("debug")
TARGET = Some("x86_64-pc-windows-gnu")
debug=true opt-level=0
HOST = Some("x86_64-pc-windows-gnu")
TARGET = Some("x86_64-pc-windows-gnu")
TARGET = Some("x86_64-pc-windows-gnu")
HOST = Some("x86_64-pc-windows-gnu")
CC_x86_64-pc-windows-gnu = None
CC_x86_64_pc_windows_gnu = None
HOST_CC = None
CC = None
TARGET = Some("x86_64-pc-windows-gnu")
HOST = Some("x86_64-pc-windows-gnu")
CFLAGS_x86_64-pc-windows-gnu = None
CFLAGS_x86_64_pc_windows_gnu = None
HOST_CFLAGS = None
CFLAGS = None
running: "gcc.exe" "-O0" "-ffunction-sections" "-fdata-sections" "-g" "-m64" "-o" "D:\\devel\\rust\\cargo0\\target\\debug\\build\\miniz-sys-60c8d67696f63a43\\out\\miniz.o" "-c" "miniz.c"


failed to execute command: The system cannot find the file specified. (os error 2)
Is `gcc.exe` not installed? (see https://github.com/alexcrichton/gcc-rs#compile-time-requirements for help)



--- stderr
thread 'main' panicked at 'explicit panic', C:\Users\bruno\.cargo\registry\src\github.com-1ecc6299db9ec823\gcc-0.3.38\src\lib.rs:958
note: Run with `RUST_BACKTRACE=1` for a backtrace.

#2

Oh the compiler uses the bundled gcc for linking. For compiling something like Cargo you’ll need a full-blown C compiler, in which case you’ll need to install externally through some other source. The README in the rust-lang/rust project can help with this.


#3

What I usually do on Windows if I am using the GNU ABI rustc compilers:

  1. Ensure you using a 64-bit GNU ABI toolchain with rustup show.
  2. Install MSYS2.
  3. In an MSYS2 terminal: pacman --sync mingw-w64-x86_64-gcc
  4. Add C:\msys64\mingw64\bin to system PATH.

#4

Did that, but now I get ld: cannot find -lz

   Compiling curl-sys v0.3.4
   Compiling miniz-sys v0.1.7
   Compiling docopt v0.6.86
   Compiling semver v0.5.1
   Compiling flate2 v0.2.14
   Compiling libssh2-sys v0.2.3
   Compiling libgit2-sys v0.6.2
   Compiling curl v0.4.1
   Compiling crates-io v0.4.0 (file:///D:/devel/rust/cargo0/src/crates-io)
   Compiling git2 v0.6.0
   Compiling git2-curl v0.7.0
   Compiling cargo v0.16.0 (file:///D:/devel/rust/cargo0)
error: linking with `gcc` failed: exit code: 1
  |
  = note: "gcc" "-Wl,--enable-long-section-names" "-fno-use-linker-plugin" "-Wl,--nxcompat" "-nostdlib" "-m64" "D:\\devel\tools.Rust\\rust\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\crt2.o" "D:\\devel\\tools.Rust\\rust\\lib\\rustlib\\x86_64-p-windows-gnu\\lib\\rsbegin.o" "-L" "D:\\devel\\tools.Rust\\rust\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "D:\\devel\\rut\\cargo0\\target\\debug\\cargo.0.o" "-o" "D:\\devel\\rust\\cargo0\\target\\debug\\cargo.exe" "-Wl,--gc-sections" "-nodefultlibs" "-L" "D:\\devel\\rust\\cargo0\\target\\debug\\deps" "-L" "D:\\devel\\rust\\cargo0\\target\\debug\\build\\libgit2sys-2ad4b82b157c6cfe\\out/lib" "-L" "D:\\devel\\rust\\cargo0\\target\\debug\\build\\libssh2-sys-09a62826e4511be3\\out/lib "-L" "D:\\devel\\rust\\cargo0\\target\\debug\\build\\curl-sys-6f3915983e98ec99\\out/lib" "-L" "D:\\devel\\rust\\cargo0\\arget\\debug\\build\\miniz-sys-60c8d67696f63a43\\out" "-L" "D:\\devel\\tools.Rust\\rust\\lib\\rustlib\\x86_64-pc-windows-nu\\lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libgit2_curl-15582022822149c1.rlib "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libcargo.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libmiow-549a39858489e9a.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libadvapi32-5f2f517e5d696cc5.rlib" "D:\\devel\\rust\\cago0\\target\\debug\\deps\\libpsapi-721097d5488ba1c4.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libtoml-cf3bfcede77aba4.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libtempdir-c5a9975021308003.rlib" "D:\\devel\\rust\\cargo0\\arget\\debug\\deps\\libsemver-aa02779cffedd79d.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libsemver_parser-7f94ba4437c2b67.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libnum_cpus-b4751aee9eea536c.rlib" "D:\\devel\\rust\\caro0\\target\\debug\\deps\\libglob-028bcc81f721b7f5.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libgit2-dd1eef663a95782.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libbitflags-0e272044714c8076.rlib" "D:\\devel\\rust\\cargo0\\trget\\debug\\deps\\libfs2-a5c12619615122d2.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libcrossbeam-2f53cae32cd29f1.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libcrates_io.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\liburl-b0f1ae179ec5d1a4.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libidna-82ebdb343d6c1855.rlib" "D:\\devel\\ust\\cargo0\\target\\debug\\deps\\libunicode_bidi-04ad1da5552425fd.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\lbmatches-3614821dd5ceb75c.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libcurl-5ada06335f638af5.rlib" "D:\\devel\rust\\cargo0\\target\\debug\\deps\\libflate2-d719035eaa7c6a88.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libminz_sys-722889de4af2439c.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\librand-49a08859d086fffe.rlib" "D:\\devel\\rut\\cargo0\\target\\debug\\deps\\libdocopt-664aa2a6d9ccf7ca.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\librustc_erialize-b495e2b03d862e12.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\liblazy_static-7f1b96a3a3eb529d.rlib" "D:\devel\\rust\\cargo0\\target\\debug\\deps\\liblibgit2_sys-ea4491fc95d75e7d.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\eps\\libenv_logger-c716af707f2027e1.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libregex-36c8e259ac5ba542.rlib" D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libregex_syntax-6602c4e3d91326a4.rlib" "D:\\devel\\rust\\cargo0\\target\\deug\\deps\\libthread_local-a3c0092e9fb6507d.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libthread_id-bcd46c79a620618.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libaho_corasick-d1dfd931d7cac82f.rlib" "D:\\devel\\rust\\cargo0\target\\debug\\deps\\libmemchr-c555f740a543880f.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\liblog-bf16bb9a4912b1d.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libutf8_ranges-5c6a6dacba3be7ce.rlib" "D:\\devel\\rust\\cargo0\\trget\\debug\\deps\\libterm-44dfa57762d62876.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libnet2-755d3e4f87237d0erlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libws2_32-c73408aa15cb2b6f.rlib" "D:\\devel\\rust\\cargo0\\target\\dbug\\deps\\libkernel32-df86a08647459244.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libcfg_if-72c1f992b13d5087.rib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libunicode_normalization-5de3a9c8fd8ddf4e.rlib" "D:\\devel\\rust\\carg0\\target\\debug\\deps\\liblibssh2_sys-f6231139877f6253.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libtar-8ec74fd965dea8d.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libfiletime-7100ae4a7fd471a6.rlib" "D:\\devel\\rust\\carg0\\target\\debug\\deps\\libcurl_sys-240b02122ee4ff1a.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libwinapi-088952d327ff4e2.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\liblibz_sys-531330ec7a80e9f9.rlib" "D:\\devel\\rust\\carg0\\target\\debug\\deps\\liblibc-ad32fde1bd850538.rlib" "D:\\devel\\rust\\cargo0\\target\\debug\\deps\\libstrsim-b42a69487d9a3b0.rlib" "D:\\devel\\tools.Rust\\rust\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd-a4729905.rlib" "D:\\devel\\tols.Rust\\rust\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libpanic_unwind-a4729905.rlib" "D:\\devel\\tools.Rust\\rust\\lib\rustlib\\x86_64-pc-windows-gnu\\lib\\libunwind-a4729905.rlib" "D:\\devel\\tools.Rust\\rust\\lib\\rustlib\\x86_64-pc-windws-gnu\\lib\\liblibc-a4729905.rlib" "D:\\devel\\tools.Rust\\rust\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librand-a472905.rlib" "D:\\devel\\tools.Rust\\rust\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcollections-a4729905.rlib" "D:\\devel\tools.Rust\\rust\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liballoc-a4729905.rlib" "D:\\devel\\tools.Rust\\rust\\lib\\rstlib\\x86_64-pc-windows-gnu\\lib\\liballoc_system-a4729905.rlib" "D:\\devel\\tools.Rust\\rust\\lib\\rustlib\\x86_64-pc-wndows-gnu\\lib\\librustc_unicode-a4729905.rlib" "D:\\devel\\tools.Rust\\rust\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\lbcore-a4729905.rlib" "D:\\devel\\tools.Rust\\rust\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcompiler_builtins-a472990.rlib" "-l" "advapi32" "-l" "psapi" "-l" "advapi32" "-l" "winhttp" "-l" "rpcrt4" "-l" "ole32" "-l" "crypt32" "-l" "ws2_32 "-l" "kernel32" "-l" "bcrypt" "-l" "crypt32" "-l" "user32" "-l" "ws2_32" "-l" "crypt32" "-l" "z" "-l" "ws2_32" "-l" "useenv" "-l" "shell32" "-l" "advapi32" "-l" "gcc_eh" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "D:\devel\\tools.Rust\\rust\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsend.o"
  = note: ld: cannot find -lz


error: aborting due to previous error

error: Could not compile `cargo`.

#5

Looks like you to link some libraries in your LIBRARY_PATH as well. According to cargo's README it looks like they really assume a Linux target for building and they don’t list all of the libraries needed so it could be difficult to build it from source without some more documentation from the cargo developers.

I’ve worked with other Rust libraries (like rust-sdl2) that do spell out how to build and link in the needed dependancies on Windows. You could open an issue and see if anyone is willing to document this.

Edit: Check out: cargo’s appveyor.yml file which builds cargo on a Windows VM for hints on how to build on Windows.


#6

So I’m looking at this again, but I’m still stuck. I do have a libz.a, it is in C:\msys64\mingw64\lib though. Most other libs are in C:\msys64\mingw64\x86_64-w64-mingw32\lib, and the Cargo build seems to find those okay.

I’ve tried a simple C Hello World on the MSys2 shell: gcc main.c -lz and that works. So I do have the z lib and it can be linked fine - that seems to indicate the problem might be in the Cargo build?


#7

Ok, I re-installed Msys, Rust installation, redid settings, I think I fixed that. No longer have the “cannot find -lz” problem, but now stuck on the strtok_r problem: https://github.com/jonathandturner/rls/issues/151 :dizzy_face: