0

Here comes the sample codes:

#include <cuda.h>
#include <cuda_runtime.h>
#include <stdio.h>

int main() {
    unsigned char* cu_test;
    cudaMalloc((void**)&cu_test, 3200);

    CUdeviceptr pbase;
    size_t psize;
    CUresult res = cuMemGetAddressRange(&pbase, &psize, (CUdeviceptr)cu_test);
    printf("cu_img_yuv size: %ld", psize);

    return 0;
}

While it throws error when compiling whatever the cuda version is(tested from 11.3 to 11.5):

$ nvcc main.cu -o main
/tmp/tmpxft_0000e288_00000000-11_main.o: In function `main':
tmpxft_0000e288_00000000-6_main.cudafe1.cpp:(.text+0x54): undefined reference to `cuMemGetAddressRange_v2'
collect2: error: ld returned 1 exit status

Can someone help pointing out what the problem is plz?

talonmies
  • 70,661
  • 34
  • 192
  • 269

1 Answers1

1

The message:

error: ld returned 1 exit status

denotes that it is a linking error ( see ld ).

To see this is the case, you can run

nvcc -c main.cu -o main.o

and you will not get any error. This is the source-code compilation step!

Solution:

You need to explicitly specify linkage with the CUDA driver stub library:

nvcc main.cu -o main -lcuda

That is because cuMemGetAddressRange() is part of the CUDA Driver API, not the CUDA runtime API.

NOTE: you did not cudaFree() the allocated memory, you might want to fix this!

Edit: (credit to @talonmies comment) You do not have to explicitly link against the CUDA runtime library (-lcudart), because nvcc will automatically link against it.