C++ 初学者 CUDA 程序中未解析的外部符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2061715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:59:01  来源:igfitidea点击:

Unresolved external symbols in beginners CUDA program

c++visual-studio-2008cuda

提问by Mr Bell

I create a new Win32 Console App as an empty project I am running Windows 7 64bit with Visual Studio 2008 C++. I am trying to get the sample code from the bottom of this article to build: http://www.ddj.com/architect/207200659

我创建了一个新的 Win32 控制台应用程序作为一个空项目,我正在使用 Visual Studio 2008 C++ 运行 Windows 7 64 位。我试图从本文底部获取示例代码来构建:http: //www.ddj.com/architect/207200659

I add CUDA Build Rule v2.3.0 to the project's custom build rules. It is the only thing with a checkbox in the available rule files list

我将 CUDA Build Rule v2.3.0 添加到项目的自定义构建规则中。它是可用规则文件列表中唯一带有复选框的东西

I create moveArrays.cu in the Source Files (folder/filter???)

我在源文件(文件夹/过滤器???)中创建了 moveArrays.cu

In that file I add the following code:

在该文件中,我添加了以下代码:

// moveArrays.cu
//
// demonstrates CUDA interface to data allocation on device (GPU)
// and data movement between host (CPU) and device.

#include <stdio.h>
#include <assert.h>
#include <cuda.h>
int main(void)
{
   float *a_h, *b_h;     // pointers to host memory
   float *a_d, *b_d;     // pointers to device memory
   int N = 14;
   int i;
   // allocate arrays on host
   a_h = (float *)malloc(sizeof(float)*N);
   b_h = (float *)malloc(sizeof(float)*N);
   // allocate arrays on device
   cudaMalloc((void **) &a_d, sizeof(float)*N);
   cudaMalloc((void **) &b_d, sizeof(float)*N);
   // initialize host data
   for (i=0; i<N; i++) {
      a_h[i] = 10.f+i;
      b_h[i] = 0.f;
   }
   // send data from host to device: a_h to a_d 
   cudaMemcpy(a_d, a_h, sizeof(float)*N, cudaMemcpyHostToDevice);
   // copy data within device: a_d to b_d
   cudaMemcpy(b_d, a_d, sizeof(float)*N, cudaMemcpyDeviceToDevice);
   // retrieve data from device: b_d to b_h
   cudaMemcpy(b_h, b_d, sizeof(float)*N, cudaMemcpyDeviceToHost);
   // check result
   for (i=0; i<N; i++)
      assert(a_h[i] == b_h[i]);
   // cleanup
   free(a_h); free(b_h); 
   cudaFree(a_d); cudaFree(b_d);
}

When I build I get these errors:

当我构建时,我收到这些错误:

1>------ Build started: Project: CUDASandbox, Configuration: Debug x64 ------
1>Linking...
1>moveArrays.cu.obj : error LNK2019: unresolved external symbol cudaFree referenced in function main
1>moveArrays.cu.obj : error LNK2019: unresolved external symbol cudaMemcpy referenced in function main
1>moveArrays.cu.obj : error LNK2019: unresolved external symbol cudaMalloc referenced in function main
1>moveArrays.cu.obj : error LNK2019: unresolved external symbol __cudaUnregisterFatBinary referenced in function __cudaUnregisterBinaryUtil
1>moveArrays.cu.obj : error LNK2019: unresolved external symbol __cudaRegisterFatBinary referenced in function __sti____cudaRegisterAll_45_tmpxft_00001264_00000000_6_moveArrays_cpp1_ii_main
1>D:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\x64\Debug\CUDASandbox.exe : fatal error LNK1120: 5 unresolved externals
1>Build log was saved at "file://d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\x64\Debug\BuildLog.htm"
1>CUDASandbox - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I can compile and run the example CUDA programs that came with the SDK. I know I am missing something simple here, but what is it?

我可以编译和运行 SDK 附带的示例 CUDA 程序。我知道我在这里遗漏了一些简单的东西,但它是什么?

回答by sand

I guess you are missing to link to the correct library. Make sure you have the CUDA library added under "Configuration Properties->Linker->Input". Refer this.

我猜你没有链接到正确的库。确保在“配置属性->链接器->输入”下添加了 CUDA 库。参考这个

回答by betontalpfa

Two things are needed:

需要做两件事:

  1. Add CUDA path:
    Go: "Configuration Properties->Linker->General->Additional Libary Directories" and add $(CudaToolkitLibDir)to the list.

  2. Add CUDA runtime library:
    Go: "Solution Properties->Linker->Input->Additional Dependencies" and add cudart.libto the list.

  1. 添加CUDA路径:
    转到:“配置属性->链接器->常规->附加库目录”并添加$(CudaToolkitLibDir)到列表中。

  2. 添加 CUDA 运行时库:
    转到:“解决方案属性->链接器->输入->附加依赖项”并添加cudart.lib到列表中。

回答by fslichen

In visual studio 2019:

在视觉工作室 2019 中:

Double check the availability of CUDA_PATH environment variable.

仔细检查 CUDA_PATH 环境变量的可用性。

Configuration Properties -> VC++ Directories -> Include Directories -> Add $(CUDA_PATH)\include

配置属性 -> VC++ 目录 -> 包含目录 -> 添加 $(CUDA_PATH)\include

Configuration Properties -> VC++ Directories -> Library Directories -> Add $(CUDA_PATH)\lib\x64

配置属性 -> VC++ 目录 -> 库目录 -> 添加 $(CUDA_PATH)\lib\x64

Configuration Properties -> Linker -> Input -> Additional Dependencies -> Add .lib files under $(CUDA_PATH)\lib\x64 and ignore existing .lib files.

配置属性 -> 链接器 -> 输入 -> 附加依赖项 -> 在 $(CUDA_PATH)\lib\x64 下添加 .lib 文件并忽略现有的 .lib 文件。