title: Python with C++: CUDA, cuSPARSE compilation gives an undefined symbol error
body: <p>My goal is to run a Python function calling C++/CUDA functions, which use cuBLAS and cuSPARSE.</p>
  <p>First, I compile a custom library using <em>setuptools</em> with no errors. Then, I run a python script that imports the library. After that, I get the following error: <code>undefined symbol: cusparseCreateDnVec</code>. This library also uses the cuSPARSE function: <code>cusparseCreateDnMat</code> with no problems.</p>
  <p>These are my NVIDIA settings: <code>NVIDIA-SMI 470.57.02    Driver Version: 470.57.02    CUDA Version: 11.4</code></p>
  <p><strong>As suggested by <em>unddoch</em></strong>:</p>
  <pre><code>$ ldd pytorch_custom_cuda.cpython-38-x86_64-linux-gnu.so
  
          linux-vdso.so.1 (0x00007ffd7ffdd000)
          libc10.so =&gt; not found
          libcudart.so.11.0 =&gt; /usr/local/cuda-11.4/lib64/libcudart.so.11.0 (0x00007fe258c82000)
          libtorch_cuda.so =&gt; not found
          libstdc++.so.6 =&gt; /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fe258aa0000)
          libgcc_s.so.1 =&gt; /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fe258a85000)
          libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe258893000)
          /lib64/ld-linux-x86-64.so.2 (0x00007fe258f5a000)
          libdl.so.2 =&gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fe25888b000)
          libpthread.so.0 =&gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fe258868000)
          librt.so.1 =&gt; /lib/x86_64-linux-gnu/librt.so.1 (0x00007fe25885d000)
          libm.so.6 =&gt; /lib/x86_64-linux-gnu/libm.so.6 (0x00007fe25870e000)
  </code></pre>
  <p>After running:</p>
  <pre><code>$ setup_debug.py install
  $ python min_repr_ex.py
  </code></pre>
  <p>I get the following error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;min_repr_ex.py&quot;, line 2, in &lt;module&gt;
      import pytorch_custom_cuda
  ImportError: /home/my_username/my_project/venv/lib/python3.8/site-packages/pytorch_custom_cuda-0.0.0-py3.8-linux-x86_64.egg/pytorch_custom_cuda.cpython-38-x86_64-linux-gnu.so: undefined symbol: cusparseCreateDnVec
  </code></pre>
  <p>These are the files needed to run this example:</p>
  <pre><code>min_repr_example_folder
  |  min_repr_ex.py
  |  min_repr_example.cpp
  |  setup_debug.py
  </code></pre>
  <p>min_repr_example.cpp</p>
  <pre><code>#include &lt;ATen/cuda/CUDAContext.h&gt;
  #include &lt;torch/extension.h&gt;
  #include &lt;cuda_runtime_api.h&gt; // cudaMalloc, cudaMemcpy, etc.
  #include &lt;cuda.h&gt;
  #include &lt;cublas_v2.h&gt;
  #include &lt;cusparse.h&gt; // SpMM, SpMV
  
  void foo() {
      float vals[] = {1.0f, 2.0f, 3.0f, 4.0f};
      float *gpu_vals;
      cusparseDnMatDescr_t matA;
      cusparseDnVecDescr_t vecX;
  
      cudaMalloc((void**) &amp;gpu_vals, 4 * sizeof(float));
      cudaMemcpy(gpu_vals, vals, 4 * sizeof(float), cudaMemcpyHostToDevice);
      cusparseCreateDnMat(&amp;matA, 2, 2, 2, gpu_vals, CUDA_R_32F, CUSPARSE_ORDER_COL);
      cusparseCreateDnVec(&amp;vecX, 4, gpu_vals, CUDA_R_32F);
  }
  
  PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
    m.def(&quot;foo&quot;, &amp;foo, &quot;example function&quot;);
  }
  </code></pre>
  <p>min_repr_ex.py</p>
  <pre><code>import torch
  import pytorch_custom_cuda
  
  print(&quot;:)&quot;)
  </code></pre>
  <p>setup_debug.py:</p>
  <pre><code>from setuptools import setup
  from torch.utils.cpp_extension import BuildExtension, CUDAExtension
  
  setup(
      name='pytorch_custom_cuda',
      ext_modules=[
          CUDAExtension('pytorch_custom_cuda', [
              'min_repr_example.cpp'],
              extra_compile_args={'nvcc': ['-lcublas', '-lcusparse']}
          )
      ],
      cmdclass={
          'build_ext': BuildExtension
      })
  
  </code></pre>