So, how do I use MSVC in Rust Beta 1.2?

From the release announcement:

In addition to all of this, 1.2 beta includes our first support for MSVC (Microsoft Visual C): the compiler is able to bootstrap, and we have preliminary nightlies targeting the platform. This is a big step for our Windows support, making it much easier to link Rust code against code built using the native toolchain. Unwinding is not yet available – code aborts on panic – but the implementation is otherwise complete, and all rust-lang crates are now testing on MSVC as a first-tier platform.

So.... how do I give that a spin? I found exactly no description on how to try it it. There's some github issues, and this one says I should add #[cfg(target_env = "msvc")]

So here's my code:

#[cfg(target_env = "msvc")]

fn main() {
    println!("Hello, world!");

Which fails with:

E:\msvc-rs>cargo clean && cargo build --verbose
   Compiling msvc-rs v0.1.0 (file:///E:/msvc-rs)
     Running `rustc src\ --crate-name msvc_rs --crate-type bin -g --out-d
ir E:\msvc-rs\target\debug --emit=dep-info,link -L dependency=E:\msvc-rs\target\
debug -L dependency=E:\msvc-rs\target\debug\deps`
error: main function not found
error: aborting due to previous error
Could not compile `msvc-rs`.

Caused by:
  Process didn't exit successfully: `rustc src\ --crate-name msvc_rs --cr
ate-type bin -g --out-dir E:\msvc-rs\target\debug --emit=dep-info,link -L depend
ency=E:\msvc-rs\target\debug -L dependency=E:\msvc-rs\target\debug\deps` (exit c
ode: 101)

I'm using the Rust Beta 1.2 32bit. Would this work on 64 bit?

Do I need to install Visual Studio 2015 first?

Is there any more detailled description on how to give this a spin?

As always, thanks in advance for you help! :slight_smile:

I think target_env is internal to the Rust compiler. That's not something you have access to in your code.

To try MSVC you need to download a version of rustc that explicitely targets MSVC. The one that is advertised on the website only targets MinGW. You can find the list of available downloads here. Here is a link to the current beta version for example.

Unzip the file and add the location to your PATH (don't remove the existing installation or you won't have access to Cargo anymore). You will also need to add cl.exe and link.exe to your path if it's not already the case. This can be done by executing C:\Program Files\Visual Studio 12\VC\bin\x86_amd64\vcvarsx86_amd64.bat in your console.

Afterwards, cargo build should just work.

1 Like

Ah thanks a lot, that clears this up. I'll give this a try. This needs Visual Studio 2015, right?

Any chance of a 32bit version coming soon?

In general, when making announcements such as in the blog post, I think it would be of value to create a short piece of documentation and link to it. For people without deep knowledge of rust, it's not straightforward to figure out how to try something like this out. A short two paragraphs could be of great help :smile:

It works for me with Visual Studio 2013 Express.

Unfortunately the support isn't 100% just yet (e.g. unwinding is still just-an-abort), so we haven't taken the time to fill out the release infrastructure for MSVC just yet, but it's coming soon! @tomaka's links for downloading the nightlies should all work (at least the nightly rustc itself), and we'll soon also have standard exe/msi installers which also bundle Cargo as well.

Overall the releases here are definitely still a work in progress, but stay tuned as this is likely to rapidly improve over the next few weeks!

You'll need to have some Visual Studio installed, but right now there's no hard requirement that I'm aware of for which version is needed.


Yeah once we've got all the release processes in place I definitely want to write up some short docs to help everyone get started. Sorry you've had a bit of a rocky start, but I hope this all helps!

1 Like

While trying it out, it complained that MSCR120.dll is missing. I have Visual Studio 2012 installed and googling shows that this file is shipped with Visual Studio 2013. I'll install that and retry.


I tried using Visual Studio 2013 Express edition with rust-1.1.0-x86_64-pc-windows-gnu.msi and got the following message:

E:\msgbox>cargo clean && cargo build --verbose --release
   Compiling msgbox v0.1.0 (file:///E:/msgbox)
     Running `rustc src\ --crate-name msgbox --crate-type bin -C opt-leve
l=3 -C lto --out-dir E:\msgbox\target\release --emit=dep-info,link -L dependency
=E:\msgbox\target\release -L dependency=E:\msgbox\target\release\deps`
error: linking with `link.exe` failed: exit code: 1112
note: "link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\Rust\bin\rustlib\x86_64-pc-w
indows-msvc\lib" "E:\msgbox\target\release\msgbox.o" "/OUT:E:\msgbox\target\rele
ase\msgbox.exe" "/OPT:REF,ICF" "C:\Users\kosta\AppData\Local\Temp\rustc.uZ1lr7OL
4Mh4\libstd-74fa456f.rlib" "/LIBPATH:E:\msgbox\target\release" "/LIBPATH:E:\msgb
ox\target\release\deps" "/LIBPATH:C:\Rust\bin\rustlib\x86_64-pc-windows-msvc\lib
" "/LIBPATH:E:\msgbox\.rust\bin\x86_64-pc-windows-msvc" "/LIBPATH:E:\msgbox\bin\
x86_64-pc-windows-msvc" "ws2_32.lib" "userenv.lib" "advapi32.lib" "kernel32.lib"
 "shell32.lib" "msvcrt.lib" "compiler-rt.lib"
note: msvcrt.lib(chkstk.obj) : fatal error LNK1112: module machine type 'X86' co
nflicts with target machine type 'x64'

error: aborting due to previous error
Could not compile `msgbox`.

It seems I need a 64bit edition of Visual Studio to run the 64-bit version of rust. Is that correct?

Is there 64bit version of Visual Studio Express? I couldn't find one...


You should be able to use Visual Studio 2013 Express - just make sure you launch the 'VS2013 x64 Cross Tools Command Prompt' from the Visual Studio Tools directory and use that shell to compile rust programs.

@ricky26 Ah thanks that did the trick! :smile:

Instead of using the Express edition, why not use the Community Edition? It's still free and comes with a lot more stuff including extension support.

Instead of running the shell from within Visual Studio you can also just run "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64\vcvars64.bat" directly to get your environment variables setup.

It is, and it can. The problem is everything else. To cross compile, you need a cross linker; neither gcc or LINK (as far as I know) qualify. You need the target platform's Rust libraries (which you have to manually download and install because they're kinda big). You need the target platform's native libraries (or how else can you link against them?).

"that explicitly targets MSVC" is shorthand for "that actually contains the necessary precompiled Rust libraries for linking to work and is configured to output compatible binaries by default".