0

I'm facing with a problem with making .dll from .cu files with Cuda/C++. There is a .o (C++ object file) compiled by nvcc with some parameters:

nvcc -dlink -o kernel.o -rdc=true kernel.cu

and passing options to the g++ compiler:

g++ -shared kernel.o -o kernel.dll -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.0\lib\x64" -lcudart -lcuda -lcudadevrt

However, I got the following error like below:

Warning: corrupt .drectve at end of def file
C:\Program Files\mingw64\x86_64-w64-mingw32\bin\ld.exe: kernel.o:(.text$mn+0x160): undefined reference to `__fatbinwrap_......_kernel_cu_add'
collect2.exe: error: ld returned 1 exit status

My Cuda toolkit version is 12.0 and the g++ one is 12.2.0, which runs on Windows 10.

Here is my code:

kernel.cu

#include <iostream>
#include <cuda.h>
#include <cuda_runtime.h>
#include "kernel.cuh"

extern "C" __global__ void add(int n, float *a, float *b, float *sum)
{
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    sum[i] = a[i] + b[i];
}

extern "C" __host__ void launchKernel()
{
    std::cout << "function called" << std::endl; //DEBUG CODE
    //Here launch kernel code.
}

kernel.cuh

extern "C" __declspec(dllexport) __global__ void add(int n, float *a, float *b, float *sum);

extern "C" __declspec(dllexport) __host__ void launchKernel();

I tried here and here but there is still a compile error. I'm beginner at Cuda/C++ so my code might looks like funny. To make .dll file with Cuda/C++, what should I do something else? Thank you.

Sunset_
  • 23
  • 6

0 Answers0