Skip to content

Commit acb6f26

Browse files
authored
Some notes about developing on Windows (pytorch#7447)
* Some notes about developing on Windows * typofix
1 parent 03767b6 commit acb6f26

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

CONTRIBUTING.md

+55
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,61 @@ If you are working on the CUDA code, here are some useful CUDA debugging tips:
235235

236236
Hope this helps, and thanks for considering to contribute.
237237

238+
## Windows development tips
239+
240+
Occasionally, you will write a patch which works on Linux, but fails CI on Windows.
241+
There are a few aspects in which MSVC (the Windows compiler toolchain we use) is stricter
242+
than Linux, which are worth keeping in mind when fixing these problems.
243+
244+
1. Symbols are NOT exported by default on Windows; instead, you have to explicitly
245+
mark a symbol as exported/imported in a header file with `__declspec(dllexport)` /
246+
`__declspec(dllimport)`. We have codified this pattern into a set of macros
247+
which follow the convention `*_API`, e.g., `AT_API` inside ATen. (Every separate
248+
shared library needs a unique macro name, because symbol visibility is on a per
249+
shared library basis.)
250+
251+
The upshot is if you see an "unresolved external" error in your Windows build, this
252+
is probably because you forgot to mark a function with `*_API`. However, there is
253+
one important counterexample to this principle: if you want a *templated* function
254+
to be instantiated at the call site, do NOT mark it with `*_API` (if you do mark it,
255+
you'll have to explicitly instantiate all of the specializations used by the call
256+
sites.)
257+
258+
2. If you link against a library, this does not make its dependencies transitively
259+
visible. You must explicitly specify a link dependency against every library whose
260+
symbols you use. (This is different from Linux where in most environments,
261+
transitive dependencies can be used to fulfill unresolved symbols.)
262+
263+
3. If you have a Windows box (we have a few on EC2 which you can request access to) and
264+
you want to run the build, the easiest way is to just run `.jenkins/pytorch/win-build.sh`.
265+
If you need to rebuild, run `REBUILD=1 .jenkins/pytorch/win-build.sh` (this will avoid
266+
blowing away your Conda environment.) I recommend opening `cmd.exe`, and then running
267+
`bash` to work in a bash shell (which will make various Linux commands available.)
268+
269+
Even if you don't know anything about MSVC, you can use cmake to build simple programs on
270+
Windows; this can be helpful if you want to learn more about some peculiar linking behavior
271+
by reproducing it on a small example. Here's a simple example cmake file that defines
272+
two dynamic libraries, one linking with the other:
273+
274+
```
275+
project(myproject CXX)
276+
set(CMAKE_CXX_STANDARD 11)
277+
add_library(foo SHARED foo.cpp)
278+
add_library(bar SHARED bar.cpp)
279+
# NB: don't forget to __declspec(dllexport) at least one symbol from foo,
280+
# otherwise foo.lib will not be created.
281+
target_link_libraries(bar PUBLIC foo)
282+
```
283+
284+
You can build it with:
285+
286+
```
287+
mkdir build
288+
cd build
289+
cmake ..
290+
cmake --build .
291+
```
292+
238293
## Caffe2 notes
239294

240295
In 2018, we merged Caffe2 into the PyTorch source repository. While the

0 commit comments

Comments
 (0)