C++/CUDA:未解析的外部符号 "enum cudaError ..."

C++/CUDA: unresolved external symbol "enum cudaError ..."

SO 上有很多关于此主题的类似帖子,但到目前为止我找不到解决问题的方法。

我想向现有 C++ 项目添加 CUDA 功能(Window,Visual Studio 2019)。这是我到目前为止所做的(基于我在谷歌上搜索的结果)。

  1. 解决方案资源管理器 -> 右键单击​​项目 -> 构建依赖项 -> 构建自定义 -> 为 CUDA 11.2(.targets、.props)勾选复选框
  2. 从 Visual Studio 创建了一个新的 'dummy' CUDA 项目,构建并启动它(VS2019 创建了一个小项目,它使用 GPU 上的 CUDA 添加了两个数组的项目)。成功了。
  3. 检查并比较了我现有项目和 'dummy' 项目之间的项目属性,并更改了以下选项:
    • CUDA C/C++ -> 目标机器平台 -> 64 位
    • 链接器 -> 附加依赖项 -> 添加 cudart_static.lib

接下来我用一些测试代码创建了一个 class(几乎完全取自 'dummy' 项目):

SHCalculator.h:

#pragma once

class SHCalculator
{
private:

public:
    void DoTestCalculationWithCuda();
};

SHCalculator.cpp:

#include "pch.h"

#include "SHCalculator.h"
#include "SHCalculation.cuh"

void SHCalculator::DoTestCalculationWithCuda()
{
    const int arraySize = 5;
    const int a[arraySize] = { 1, 2, 3, 4, 5 };
    const int b[arraySize] = { 10, 20, 30, 40, 50 };
    int c[arraySize] = { 0 };


    cudaError_t cudaStatus = AddWithCuda(c, a, b, arraySize);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addWithCuda failed!");
        return;
    }

    printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
        c[0], c[1], c[2], c[3], c[4]);

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceReset failed!");
        return;
    }
}

SHCalculation.cuh:

cudaError_t AddWithCuda(int* c, const int* a, const int* b, unsigned int size);

//__global__ void addKernel(int* c, const int* a, const int* b);

SHCalculation.cu:

#include "pch.h"
#include "SHCalculation.cuh"


//__global__ void addKernel(int* c, const int* a, const int* b)
//{
//    int i = threadIdx.x;
//    c[i] = a[i] + b[i];
//}



cudaError_t AddWithCuda(int* c, const int* a, const int* b, unsigned int size)
{
    int* dev_a = 0;
    int* dev_b = 0;
    int* dev_c = 0;
    cudaError_t cudaStatus = cudaError_t::cudaErrorAssert;

    //  left out all the other code (memory allocation, kernel calling) here, since the error 
    // comes without it as well. 

    return cudaStatus;
}

注意,我已经注释掉或删除了很多代码,因为无论如何都会出现错误。这是错误消息:

LNK2019 unresolved external symbol "enum cudaError __cdecl AddWithCuda(int *,int const *,int const *,unsigned int)" (?AddWithCuda@@YA?AW4cudaError@@PEAHPEBH1I@Z) referenced in function "public: void __cdecl SHCalculator::DoTestCalculationWithCuda(void)" (?DoTestCalculationWithCuda@SHCalculator@@QEAAXXZ)

LNK1120 1 unresolved externals

附加信息: CUDA 包含在预编译头中 pch.h:

// cuda
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

链接器错误的原因可能是什么?

这里是缺少的东西:

对所有 CUDA 文件执行:

  • 右击 -> 属性
  • 在配置属性 -> 常规 -> 项目类型中选择 CUDA C/C++

这就是没有创建 *.obj 文件并且 linker 不能 link 它们的原因。