@@ -86,6 +86,48 @@ option:
86
86
rustc -Clinker-plugin-lto=" /path/to/LLVMgold.so" -L. -Copt-level=2 ./main.rs
87
87
```
88
88
89
+ ### Usage with clang-cl and --target x86_64-pc-windows-msvc
90
+
91
+ Cross language LTO can be used with the x86_64-pc-windows-msvc target, but this requires using the
92
+ clang-cl compiler instead of the MSVC cl.exe included with Visual Studio Build Tools, and linking
93
+ with lld-link. Both clang-cl and lld-link can be downloaded from [ LLVM's download page] ( https://releases.llvm.org/download.html ) .
94
+ Note that most crates in the ecosystem are likely to assume you are using cl.exe if using this target
95
+ and that some things, like for example vcpkg, [ don't work very well with clang-cl] ( https://github.com/microsoft/vcpkg/issues/2087 ) .
96
+
97
+ You will want to make sure your rust major LLVM version matches your installed LLVM tooling version,
98
+ otherwise it is likely you will get linker errors:
99
+
100
+ ``` bat
101
+ rustc -V --verbose
102
+ clang-cl --version
103
+ ```
104
+
105
+ If you are compiling any proc-macros, you will get this error:
106
+
107
+ ```
108
+ error: Linker plugin based LTO is not supported together with `-C prefer-dynamic` when
109
+ targeting Windows-like targets
110
+ ```
111
+
112
+ This is fixed if you explicitly set the target, for example
113
+ ` cargo build --target x86_64-pc-windows-msvc `
114
+ Without an explicit --target the flags will be passed to all compiler invocations (including build
115
+ scripts and proc macros), see [ cargo docs on rustflags] ( https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags )
116
+
117
+ If you have dependencies using the ` cc ` crate, you will need to set these
118
+ environment variables:
119
+ ``` bat
120
+ set CC=clang-cl
121
+ set CXX=clang-cl
122
+ set CFLAGS=/clang:-flto=thin /clang:-fuse-ld=lld-link
123
+ set CXXFLAGS=/clang:-flto=thin /clang:-fuse-ld=lld-link
124
+ REM Needed because msvc's lib.exe crashes on LLVM LTO .obj files
125
+ set AR=llvm-lib
126
+ ```
127
+
128
+ If you are specifying lld-link as your linker by setting ` linker = "lld-link.exe" ` in your cargo config,
129
+ you may run into issues with some crates that compile code with separate cargo invocations. You should be
130
+ able to get around this problem by setting ` -Clinker=lld-link ` in RUSTFLAGS
89
131
90
132
## Toolchain Compatibility
91
133
0 commit comments