开发者

Linking CUDA and C++: Undefined symbols for architecture i386

开发者 https://www.devze.com 2023-04-10 12:52 出处:网络
I have tried really hard but no success. I hope someone can help me get this working. I have two source files.

I have tried really hard but no success. I hope someone can help me get this working. I have two source files.

Main.cpp

#include <stdio.h>
#include "Math.h"
#include <math.h>
#include <iostream>

int cuda_function(int a, int b);
int callKnn(void);

int main(void)
{
    int x = cuda_function(1, 2);
    int f = callKnn();
    std::cout << f << std::endl;
    return 1;
}

CudaFunctions.cu

#include <cuda.h>
#include <stdio.h>
#include "Math.h"
#include <math.h>
#include "cuda.h"
#include <time.h>
#include "knn_cuda_without_indexes.cu"

__global__ void kernel(int a, int b)
{
  //statements
}

int cuda_function2(int a, int b)
{
    return 2;
}

int callKnn(void)
{   
    // Variables and parameters
    float* ref;                 // Pointer to reference point array
    float* query;               // Pointer to query point array
    float* dist;                // Pointer to distance array
    int    ref_nb     = 4096;   // Reference point number, max=65535
    int    query_nb   = 4096;   // Query point number,     max=65535
    int    dim        = 32;     // Dimension of points
    int    k          = 20;     // Nearest neighbors to consider
    int    iterations = 100;
    int    i;

    // Memory allocation
    ref    = (float *) malloc(ref_nb   * dim * sizeof(float));
    query  = (float *) malloc(query_nb * dim * sizeof(float));
    dist   = (float *) malloc(query_nb * sizeof(float));

    // Init 
    srand(time(NULL));
    for (i=0 ; i<ref_nb   * dim ; i++) ref[i]    = (float)rand() / (float)RAND_MAX;
    for (i=0 ; i<query_nb * dim ; i++) query[i]  = (float)ra开发者_如何学运维nd() / (float)RAND_MAX;

    // Variables for duration evaluation
    cudaEvent_t start, stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    float elapsed_time;

    // Display informations
    printf("Number of reference points      : %6d\n", ref_nb  );
    printf("Number of query points          : %6d\n", query_nb);
    printf("Dimension of points             : %4d\n", dim     );
    printf("Number of neighbors to consider : %4d\n", k       );
    printf("Processing kNN search           :"                );

    // Call kNN search CUDA
    cudaEventRecord(start, 0);
    for (i=0; i<iterations; i++)
        knn(ref, ref_nb, query, query_nb, dim, k, dist);
    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);
    cudaEventElapsedTime(&elapsed_time, start, stop);
    printf(" done in %f s for %d iterations (%f s by iteration)\n", elapsed_time/1000, iterations, elapsed_time/(iterations*1000));

    // Destroy cuda event object and free memory
    cudaEventDestroy(start);
    cudaEventDestroy(stop);
    free(dist);
    free(query);
    free(ref);

    return 1;
}

I try run it from terminal with the following commands:

g++ -c Main.cpp -m32
nvcc -c CudaFunctions.cu -lcuda -D_CRT_SECURE_NO_DEPRECATE
nvcc -o mytest Main.o CudaFunctions.o

But get following errors:

Undefined symbols for architecture i386:
  "cuda_function(int, int)", referenced from:
      _main in Main.o
  "_cuInit", referenced from:
      knn(float*, int, float*, int, int, int, float*)in CudaFunctions.o
  "_cuCtxCreate_v2", referenced from:
      knn(float*, int, float*, int, int, int, float*)in CudaFunctions.o
  "_cuMemGetInfo_v2", referenced from:
      knn(float*, int, float*, int, int, int, float*)in CudaFunctions.o
  "_cuCtxDetach", referenced from:
      knn(float*, int, float*, int, int, int, float*)in CudaFunctions.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

I don't know if this has something to do with #include statements or header files. I am left of ideas to try.


The first undefined symbol

"cuda_function(int, int)", referenced from:
   _main in Main.o

is caused by the fact that CudaFunctions.cu defines cuda_function2, not cuda_function. Correct the name either in CudaFunctions.cu or Main.cpp.

The rest of the undefined symbols are caused by not linking against libcuda.dylib correctly, because that is where those symbols live. Try moving the -lcuda argument to the second nvcc command line, the one that actually links together the program. Better yet, try omitting the -lcuda argument entirely, because it isn't necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号