0

Due to some restructuring in my cuda code I get the following error " Unresolved extern function" when I try to call a device function from a kernal.

test.cuh looks like this:

#include "cuda_runtime.h"

class TestKernel
{
  public:
    int number;
    TestKernel(int number);
    __device__ __host__
    void run();
};

The test.cu looks like this.

#include"testkernel.cuh"
#include "stdio.h"

TestKernel::TestKernel(int number)
{
   this->number = number;
}
void TestKernel::run()
{
    printf("%d", this->number);
} 

My main file looks like this.

#include "device_launch_parameters.h"
#include "testkernel.cuh"


__global__ void kernel(TestKernel * testKernel)
{
    testKernel[blockIdx.x * blockDim.x + threadIdx.x].run();
}

int main()
{
    TestKernel * testKernel;
    cudaMallocManaged(&testKernel, 10 * sizeof(*testKernel));
    for (int i = 0; i < 10; i++)
    {
       testKernel[i] = TestKernel(i);
    }
    kernel << <1, 10 >> > (testKernel);

    return 0;
}

I have found suggestions that this could be solved by adding -dc during the building, however when I do that I get several other errors:

"Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol __cudaRegisterLinkedBinary_45_tmpxft_00007118_00000000_7_TestKernel_cpp1_ii_aa56518f referenced in function "void __cdecl __nv_cudaEntityRegisterCallback(void * *)" (?__nv_cudaEntityRegisterCallback@@YAXPEAPEAX@Z "

I am trying to build it with Visual Studio 2017, and with CUDA 10.1.

Atle Kristiansen
  • 707
  • 7
  • 28
  • 3
    1. You need to build your project with the relocatable device code setting enabled. See [here](https://stackoverflow.com/questions/17188527/cuda-external-class-linkage-and-unresolved-extern-function-in-ptxas-file/17190918#17190918) This is a setting in the visual studio CUDA project, you don't manually add the compile switches (like `-dc`) yourself. 2. You should decorate the definition of `void TestKernel::run()` with `__host__` `__device__` just like you have it decorated in your class declaration. – Robert Crovella Aug 20 '19 at 14:49
  • That fixed that issue, but for some reason it added alot of external libs provided by vcpkg, "Don't know what to do with 'A:/vcpkg/installed/x64-windows/debug/lib/OpenGL32.Lib". I think Visual Studio adds these libs automatically.. – Atle Kristiansen Aug 20 '19 at 15:13
  • Okei, I just had to remove the userwide integration with vcpkg. Thanks for the assist. – Atle Kristiansen Aug 20 '19 at 15:21

0 Answers0