【问题标题】:CUDA with C/C++ Compilation fails带有 C/C++ 编译的 CUDA 失败
【发布时间】:2013-02-01 02:03:40
【问题描述】:

我正在尝试将 CUDA 代码与我现有的 C++ 应用程序集成。按照某些网页端的指示,我需要一个“file.cu”,其中我有一个包装函数,它在 GPU 上分配内存并启动内核。我遵循了该建议,但我现在无法编译代码。

文件.cu

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

void preComputeCorrelation_gpu( int * d )
{
    //I shall write the kernel later once I am confirmed that CUDA code works
    cudaDeviceProp prop;
    cudaGetDeviceProperties( &prop, 0 );
    printf( "name = %s\n", prop.name );
}

main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <cuda.h>

#define __CUDA_SUPPORT__

#ifdef __CUDA_SUPPORT__
// Defination to be found in "cudaWrap.cu"
extern void preComputeCorrelation_gpu( int * d );
#endif


int main()
{
//code to read d from the file and other initialization
int * d;
.
.

#ifdef __CUDA_SUPPORT__ 
    fprintf( stderr, "GPU Computation starts" );
    // Defination to be found in "cudaWrap.cu"
    preComputeCorrelation_gpu( d ); 
#else
    fprintf( stderr, "CPU Computation starts" );
    preComputeCorrelation( d );
#endif


.
.
//more code

return 0 ;
}

现在,我输入以下命令来编译代码

$ nvcc -c cudaWrap.cu <br/>
$ g++ -I /usr/local/cuda-5.0/include -L /usr/local/cuda-5.0/lib -o GA_omp GA_dev_omp.cpp main_omp.cpp data_stats.cpp cudaWrap.o

编译失败,我在第二个命令后收到以下消息。虽然第一个命令有效。

cudaWrap.o: In function `preComputeCorrelation_gpu(DataSet*)':
tmpxft_00001061_00000000-3_cudaWrap.cudafe1.cpp:(.text+0x2f): undefined reference to `cudaGetDeviceProperties'
cudaWrap.o: In function `__cudaUnregisterBinaryUtil()':
tmpxft_00001061_00000000-3_cudaWrap.cudafe1.cpp:(.text+0x6b): undefined reference to `__cudaUnregisterFatBinary'
cudaWrap.o: In function `__sti____cudaRegisterAll_43_tmpxft_00001061_00000000_6_cudaWrap_cpp1_ii_f8a043c5()':
tmpxft_00001061_00000000-3_cudaWrap.cudafe1.cpp:(.text+0x8c): undefined reference to `__cudaRegisterFatBinary'
collect2: ld returned 1 exit status

我该如何解决这个问题?

【问题讨论】:

  • 您忘记链接 libcudart.so。在 g++ 命令末尾添加 -lcudart 即可编译。
  • 完美……现在可以使用了……谢谢!
  • @mkuse:如果您添加您的解决方案作为答案,如果会有所帮助。几天后,您将能够接受它,这将使问题从未回答的列表中消失。

标签: cuda gpu gpgpu


【解决方案1】:

这个问题的解决方法是普通c++代码和cuda代码的链接需要用libcudart.so来完成

编译应该看起来像 --

$ nvcc -c cudaWrap.cu 
$ g++ -lcudart -I /usr/local/cuda-5.0/include -L /usr/local/cuda-5.0/lib -o GA_omp GA_dev_omp.cpp main_omp.cpp data_stats.cpp cudaWrap.o

在这种情况下,cudaWrap.cu 包含 cuda 代码。 main_omp.cpp 包含 main() 并且还有一些其他的应用程序文件也需要编译。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多