How do I create a static library in Rust to link with C code in Windows?
TL;DR: Install a different flavor of GCC
pacman -R local/gccpacman -S mingw-w64-i686-gcc
Half-informed guessing follows...
After some help on Rust IRC, it sounds like the issue is that the MSYS2 / MinGW gcc
is a "stock" compiler, one without special knowledge of MSYS / MinGW / Windows special features.
mingw-w64-i686-gcc
(or mingw-w64-x86_64-gcc
) does know about Windows-specific symbols, which libbacktrace, a part of the Rust distribution, requires.
The "correct" GCC build should have the string "Built by MSYS2 project" in the gcc --version
output.
With that, the full process looks like:
$ rustc --version --verboserustc 1.17.0 (56124baa9 2017-04-24)host: i686-pc-windows-gnu$ gcc --versiongcc.exe (Rev2, Built by MSYS2 project) 6.3.0$ rustc --crate-type=staticlib func.rsnote: link against the following native artifacts when linking against this static librarynote: the order and any duplication can be significant on some platforms, and so may need to be preservednote: library: advapi32note: library: ws2_32note: library: userenvnote: library: shell32note: library: gcc_eh$ gcc -o main main.c func.lib -ladvapi32 -lws2_32 -luserenv -lshell32 -lgcc_eh$ ./main4 * 2 = 8