0

I am trying to compile this code using the CUDA Compiler:

#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <curand.h>

int main(void)
{
    size_t n = 100;
    size_t i;
    int *hostData;
    unsigned int *devData;
    hostData = (int *)calloc(n, sizeof(int));
    curandGenerator_t gen;
    curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_DEFAULT);
    curandSetPseudoRandomGeneratorSeed(gen, 12345);
    cudaMalloc((void **)&devData, n * sizeof(int));
    curandGenerate(gen, devData, n);
    cudaMemcpy(hostData, devData, n * sizeof(int), cudaMemcpyDeviceToHost);
    for(i = 0; i < n; i++)
    {
        printf("%d ", hostData[i]);
    }
    printf("\n");
    curandDestroyGenerator (gen);
    cudaFree ( devData );
    free ( hostData );
    return 0;
}

By using this command:

nvcc -o RNG RNG7.cu

This is the output I receive:

[root@client2 CUDA]$ nvcc -o RNG7 RNG7.cu
/tmp/tmpxft_00001ed1_00000000-13_RNG7.o: In function `main':
tmpxft_00001ed1_00000000-1_RNG7.cudafe1.cpp:(.text+0x6c): undefined reference to `curandCreateGenerator'
tmpxft_00001ed1_00000000-1_RNG7.cudafe1.cpp:(.text+0x7a): undefined reference to `curandSetPseudoRandomGeneratorSeed'
tmpxft_00001ed1_00000000-1_RNG7.cudafe1.cpp:(.text+0xa0): undefined reference to `curandGenerate'
tmpxft_00001ed1_00000000-1_RNG7.cudafe1.cpp:(.text+0x107): undefined reference to `curandDestroyGenerator'
collect2: ld returned 1 exit status

In another discussion they stated that this problem could be related to a linker problem or something, that I need to manually link the library in the compiler command to include the ones stated on my code.

I have no idea to achieve this, can someone please help with this?

Thanks!

Wilo Maldonado
  • 551
  • 2
  • 11
  • 22
  • I'm not sure what the name of the curand library is, but your command line should look something like `nvcc -o RNG RNG7.cu -lcurand` in order to link against it. – Jared Hoberock Aug 01 '12 at 02:58
  • possible duplicate of [CURAND Library - Compiling Error - Undefined reference to functions](http://stackoverflow.com/questions/11734578/curand-library-compiling-error-undefined-reference-to-functions) – talonmies Aug 01 '12 at 04:00
  • @Wilo Maldonado: Didn't you just ask this exact same question? – talonmies Aug 01 '12 at 04:02
  • It's the same question with the same linker error. The answer of the other question by @asm is a correct solution. – pQB Aug 01 '12 at 06:27

1 Answers1

2

Use the following options.

nvcc -o RNG7 RNG7.cu -lcurand -Xlinker=-rpath,/usr/local/cuda/lib

it will work like charm.

lxkarthi
  • 336
  • 4
  • 14