title: CMake CUDA separate compilation static lib link error on Windows but not on Ubuntu
body: <p>I am using CMake to compile a CUDA project, which contains a static lib and a main file. MWE <a href="https://gist.github.com/8f1dbffd0f28010bced69ea8c08a751c" rel="noreferrer">here</a>. The directory is:</p>
  
  <p><code>├── CMakeLists.txt
   ├── src
       ├── mylib.h
       ├── mylib.cu
   ├── test
       ├── CMakeLists.txt
       ├── main.cpp</code></p>
  
  <p>On Ubuntu everything works fine. But on Windows I got a link error:</p>
  
  <pre><code>mylib.lib(mylib.cu.obj) : error LNK2019: unresolved external symbol __cudaRegisterLinkedBinary_40_tmpxft_00006024_00000000_7_mylib_cpp1_ii_935b38c5 referenced in function "void __cdecl __sti____cudaRegisterAll(void)" (?__sti____cudaRegisterAll@@YAXXZ)\build\test\Release\main.exe : fatal error LNK1120: 1 unresolved externals
  </code></pre>
  
  <p>This issue only relates to the first <code>CMakeLists.txt</code>:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.8)
  project(MyTest LANGUAGES CXX CUDA)
  
  # check requirements
  find_package(CUDA   8.0 REQUIRED)
  
  # set include and link directories
  if (UNIX)
      set(CUDA_SAMPLE_INC ${CUDA_TOOLKIT_ROOT_DIR}/samples/common/inc)
      set(CUDA_TARGET_INC ${CUDA_TOOLKIT_ROOT_DIR}/targets/x86_64-linux/include)
      set(CUDA_SAMPLE_LKN ${CUDA_TOOLKIT_ROOT_DIR}/targets/x86_64-linux/lib)
  endif (UNIX)
  if (WIN32)
      set(CUDA_SAMPLE_INC C:/ProgramData/NVIDIA\ Corporation/CUDA\ Samples/v9.0/common/inc)
      set(CUDA_TARGET_INC C:/Program\ Files/NVIDIA GPU\ Computing\ Toolkit/CUDA/v9.0/include)
      set(CUDA_SAMPLE_LKN C:/Program\ Files/NVIDIA\ GPU\ Computing\ Toolkit/CUDA/v9.0/lib/x64)
  endif (WIN32)
  include_directories(src ${CUDA_SAMPLE_INC} ${CUDA_TARGET_INC})
  link_directories(${CUDA_SAMPLE_LKN})
  
  # define and compile our static library 
  set(STATIC_MY_LIB mylib)
  add_library(${STATIC_MY_LIB} STATIC src/mylib.cu)
  
  # install
  install(TARGETS ${STATIC_MY_LIB}
          ARCHIVE DESTINATION ${CMAKE_SOURCE_DIR}/lib
          LIBRARY DESTINATION ${CMAKE_SOURCE_DIR}/lib)
  
  # comment it out to suppress the error
  set_target_properties( ${STATIC_MY_LIB} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
  
  # add our test project
  add_subdirectory(test)
  </code></pre>
  
  <p>If I comment out <code>set_target_properties( ${STATIC_MY_LIB} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)</code>, the link error is gone.</p>
  
  <p>Environments:</p>
  
  <ul>
  <li>Ubuntu 16.04, gcc 5.4, Tesla Titan X, CUDA 9.1, CMake 3.10.1</li>
  <li>Windows 10, VS 2015, K20c, CUDA 9.0, CMake 3.10.1</li>
  </ul>
  
  <p>I have tried suggestions in <a href="https://tutel.me/c/programming/questions/17827243/register.html" rel="noreferrer">1</a>, <a href="https://devtalk.nvidia.com/default/topic/525616/question-about-___cudaregisterlinkedbinary-referenced-in-function-____cudaregisterall/?offset=3#3942819" rel="noreferrer">2</a>. But none of them works.</p>
  
  <p>Why this happens? And how to overcome?</p>