Compiling CPP/CUDA extension on a windows system

username: flyerjia
cooked: <p>Hi, i am trying to compile cpp/cuda extension on my windows systems. I met one error about pybind11, and i don’t konw how to deal with it, can you help me ?<br>
d:\anaconda3\lib\site-packages\torch\lib\include\pybind11\cast.h(1453): error: expression must be a pointer to a complete object type</p>

username: peterjc123
cooked: <aside class="quote no-group" data-username="flyerjia" data-post="1" data-topic="58557">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/cdc98d/48.png" class="avatar"> flyerjia:</div>
<blockquote>
<p>error: expression must be a pointer to a complete object type</p>
</blockquote>
</aside>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/11004">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/11004" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/11004" target="_blank" rel="noopener nofollow ugc">CUDA Extension Build Failure on Windows and MSVC 2017</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2018-08-29" data-time="17:19:50" data-timezone="UTC">05:19PM - 29 Aug 18 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2022-10-18" data-time="11:54:02" data-timezone="UTC">11:54AM - 18 Oct 22 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/JackHunt" target="_blank" rel="noopener nofollow ugc">
          <img alt="JackHunt" src="https://avatars.githubusercontent.com/u/3265323?v=4" class="onebox-avatar-inline" width="20" height="20">
          JackHunt
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: dependency bug
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: windows
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: cpp-extensions
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: cuda
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: pybind
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          has workaround
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">## Issue description

Building a custom CUDA extension fails with the followin<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">g error on MSVC 2017:
`&lt;env_lib_dir&gt;\site-packages\torch\lib\include\pybind11\cast.h(1393): error: expression must be a pointer to a complete object type`

However, I have not observed the error with multiple GCC versions on Linux.

## Code example
Here is an MWE.

setup.py
```
import os
from setuptools import setup
from torch.utils.cpp_extension import CppExtension, CUDAExtension, BuildExtension

setup(name='exten', ext_modules=[
    CUDAExtension('exten_cuda', ['exten_cuda.cpp', 'exten_cuda_kernel.cu'])
    ], cmdclass={'build_ext': BuildExtension})
```

exten_cuda.cpp
```
std::vector&lt;at::Tensor&gt; exten_cuda_forward(at::Tensor a);
std::vector&lt;at::Tensor&gt; exten_cuda_backward(at::Tensor a);

std::vector&lt;at::Tensor&gt; exten_forward(at::Tensor a) {
  return exten_cuda_forward(a);
}

std::vector&lt;at::Tensor&gt; exten_backward(at::Tensor a) {
  return exten_cuda_backward(a);
}

// Do Python bindings.
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  m.def("forward", &amp;exten_forward, "Extension forward CUDA");
  m.def("backward", &amp;exten_backward, "Extension backward CUDA");
}
```

exten_cuda_kernel.cu
```
#include &lt;torch/torch.h&gt;
#include &lt;cuda.h&gt;
#include &lt;cuda_runtime.h&gt;
#include &lt;vector&gt;

std::vector&lt;at::Tensor&gt; exten_cuda_forward(at::Tensor a) {
  // Do nothing.

  // Return empty vector (just for compile error demo).
  return {};
}

std::vector&lt;at::Tensor&gt; exten_cuda_backward(at::Tensor a) {
  // Do nothing.

  // Return empty vector (just for compile error demo).
  return {};
}
```

## System Info
```
Collecting environment information...
PyTorch version: 0.4.1
Is debug build: No
CUDA used to build PyTorch: 9.2

OS: Microsoft Windows 10 Education
GCC version: (x86_64-posix-seh, Built by strawberryperl.com project) 7.1.0
CMake version: version 3.12.0

Python version: 3.7
Is CUDA available: Yes
CUDA runtime version: 9.2.148
GPU models and configuration: GPU 0: GeForce GTX 1060 6GB
Nvidia driver version: 399.07
cuDNN version: Could not collect

Versions of relevant libraries:
[pip] numpy (1.15.1)
[pip] torch (0.4.1)
[pip] torchvision (0.2.1)
[conda] cuda92                    1.0                           0    pytorch
[conda] pytorch                   0.4.1           py37_cuda92_cudnn7he774522_1  [cuda92]  pytorch
[conda] torchvision               0.2.1                     &lt;pip&gt;
```

- PyTorch or Caffe2: PyTorch
- How you installed PyTorch (conda, pip, source): conda
- OS: Windows 10 x64
- PyTorch version: 0.4.1
- Python version: 3.7
- CUDA/cuDNN version: 9.2
- GPU models and configuration: GTX 1060 6GB
- MSVC version (if compiling from source): 15.7.5
- CMake version: 3.12.0
- Versions of any other relevant libraries:  setuptools 0.2.1</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>