How to link a static cuda library to a cpp project in visual studio

Hi, I have built a simple cuda project in visual studio that works perfectly fine when it is compiled alone.
However I want to use it in a cpp project, so I’ve referenced the project in my cpp one and added the include folder and the dependencies files.
I have also changed the cuda project configuration type as a static library.

Here is a typical header file of the cuda project

// whatever.h
#pragma once 

#include <vector>
std::vector<double> doMath(const std::vector<double>& input);

and here a typical cu file of the cuda project

// whatever.cu
#include "whatever.h"

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

__global__ void kernel(double* input, double* output)
{
// ...
}

void doMath(const std::vector<double>& input)
{
   double* dInput;
   cudaMalloc(&dInput, input.size() * sizeof(double));
   cudaMemcpy(dInput, input.begin(), input.size() * sizeof(double), cudaMemcpyHostToDevice);

   double* dOutput;
   cudaMalloc(&dOutput, input.size() * sizeof(double))


   kernel<<<1,1>>>(dInput, dOutput);
 
   std::vector<double> output(input.size());
   cudaMemcpy(output.begin(), dOutput, input.size() * sizeof(double), cudaMemcpyDeviceToHost);

  return output;
}

The main file of the cpp project looks like this

// main.cpp
#include <iostream>
#include <vector>
#include "whatever.h"

int main()
{
   std::vector<double> input = {0,1,2,3,4,5,6};
   std::vector<double> output = doMath(input);

   for(double o: output) std::cout << o << ' ';

   return 0;
}


Why do I get this error “error LNK2019: unresolved external symbol cudaMalloc” ?
Why isn’t it working, I haven’t use any cuda code in the header files.
How can I make this work ?

You still need the cuda toolkit as a dependency. In VS you can right-click your project → build dependencies → build customization and add CUDA.

Your static library has a dependency on the cuda runtime API library - cudart. You need to explicitly link against cudart in your project settings. See here. The library that you want to add is cudart.lib It will also be necessary to add the path to cudart.lib to the linker library paths in your project settings. These steps are actually not unique to CUDA; the general method to add a library or library path to the link specification for a VS project is something you can google for.