How to create CUDA library using Visual Studio

Hello-

I am new to programming with CUDA would like to write my own performance library using CUDA. I have seen several hello world examples, but all of the seem to create an executable. What I am looking for is how to go about creating a library that I can link with. I know libraries like NPP do this, so I’m sure there is a way, but I can not find any examples of how to build such projects.

I am developing on Windows and am using Visual Studio 2017.

Thanks in advance!

Jim

This describes pretty much the whole process and it works for Windows, Linux and Mac (I use all 3):
[url]https://devtalk.nvidia.com/default/topic/1014106/separate-compilation-of-cuda-code-into-library-for-use-with-existing-code-base/[/url]

And this:
[url]Creating a static CUDA library to be linked with a C++ program - Stack Overflow

Thanks for the quick response, I looked over each of the links carefully and I think I have the gist of how to build the libraries. The examples seemed to be command line orientated, which is fine, it definitely gives be a better understanding of the entire compilation and link process. However, I was hoping that there might be a more Visual Studio IDE based approach. I would have thought that someone out there has already developed a CUBA library project template, but I could not find one anywhere. Perhaps I’ll have to man up and write one myself.

Thanks for the help, it definitely got me pointed in the right direction.

Jim

Building a library with CUDA code in VS is the same as any ordinary library. Just change the target of the project from EXE to DLL, that’s why there is no template for it as the only difference is --shared being passed to nvcc.

Surely people here do it on a daily basis, and the only extra process is that a program can’t call a kernel function directly from a shared library. It needs a wrapper using extern ‘C’ linkage, as demonstrated in these threads.

Thanks once again for the assistance, your suggestion worked. I thought I would explain each step of the process in case someone else stumbles across this thread.

The steps below assume that you have already installed the CUDA toolkit. This example assumes the use of the CUDA Tookkit 10.0

  1. Add a new project to your solution, selecting the NVIDIA CUDA 10.0 Runtime.
  2. Open the property page for the new project.
  3. Under Configuration properties, select the 'General' option.
  4. Under 'Project Defaults' select the configuration type (I chose Static Library). This will automatically set the project target to '.lib'
  5. Edit the auto-generated cuda source file (Kernel.cu) and remove the main function. For this example, keep the CPP wrapper function 'addWithCuda' in the file, this is the function that will call the CUDA kernel.
  6. Build the project.

To test the library perform the following steps.

  1. Add a new project to your solution, selecting NVIDIA CUDA 10.0 Runtime.
  2. Add a reference to the created CUDA library project to the new test project by right clicking on references under the new test project and selecting the CUDA library project.
  3. Edit the test project source file Kernel.cu and remove the CUDA wrapper AND the CUDA kernel.
  4. Add an include that includes the test library include file.
  5. Build the test project.
  6. Execute the test project.

My thanks to Saulcpp for pointing me in the right direction.

Jim

1 Like

Thanks BigPrimate, I follow your instruction in VS2019 for Cuda 12.0 Runtime, but it still has link errors if i choose Configuration properties->C/C+±>Code Generation->runtime lib as “Multi-threaded Debug (/MTd)” in my exe project.

Since there is no runtime lib setting options in the static lib project side, I must change the exe project settings as Multi-threaded Debug DLL (/MDd). In this way I successfully build my solution.