c++ - 从另一个文件编译 __device__ 函数调用时出现 Visual Studio MSB3721 错误

标签 c++ visual-studio cuda

我正在尝试编译一个 CUDA 项目,当我尝试运行在单独的 .cu 文件中定义的函数时,它会立即出现 255 错误

这是定义主内核的地方

#include <curand_kernel.h>
#include <ctime>

#include <stdio.h>
#include "Scene.cuh"

__global__ void fill(float *c, Scene* scene)
{
    int index = blockIdx.y * blockDim.x * blockDim.y * gridDim.x +
                threadIdx.y * blockDim.x * gridDim.x +
                blockIdx.x * blockDim.x + threadIdx.x;


    // this is the line which gives the compilation error
    float3 result = scene->computeRayFromIndex(index);

    c[index * 4 + 0] += 1.0f; 
    c[index * 4 + 1] += 1.0f;
    c[index * 4 + 2] += 1.0f; 
    c[index * 4 + 3] += 1.0f;
}

这里是scene.cuh

#ifndef Scene_h
#define Scene_h

#include "cuda_runtime.h"

class Scene {

public:
    Scene();

    __host__ __device__ float3 computeRayFromIndex(int);

    int width;
    int height;

    int cameraType;

    private:

};

#endif

和scene.cu

#include "Scene.cuh"

Scene::Scene() {

}

__host__ __device__ float3 Scene::computeRayFromIndex(int pixelIndex) {
    float3 test;
    return test;
}

我正在使用 visual studio 2013,并且像往常一样从菜单中将 cuda 文件添加到我的项目中

这是编译错误

Error   10  error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin\nvcc.exe" -gencode=arch=compute_20,code=\"sm_20,compute_20\" --use-local-env --cl-version 2013 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64"  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include"  -G   --keep-dir x64\Debug -maxrregcount=0  --machine 64 --compile -cudart static  -g   -DWIN32 -DWIN64 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /FS /Zi /RTC1 /MDd " -o x64\Debug\fillRandomTexture.cu.obj "D:\CUDA\projects\vRay\vRay\fillRandomTexture.cu"" exited with code 255.

如果我注释掉,项目会构建并运行良好

float3 result = scene->computeRayFromIndex(index);

在主内核文件中

最佳答案

在CUDA中,当我们想从另一个设备代码函数中调用一个设备代码函数,而这两个设备代码函数在不同的编译单元中时,有必要启用relocatable device code generation and linking。在编译这样的项目时。

在 visual studio 中,这可以从项目属性页面为整个项目设置,如 here 所示:

enter image description here

此外,在使用 visual studio 和 CUDA 时,错误“MSB3721”是来自 visual studio 的非特定错误,表示“我运行了 nvcc,它返回了一个错误”。但是,nvcc 中的实际错误应该在此之前发生。如果您在“MSB3721”错误之前没有在编译输出窗口中看到它,那么您的详细级别太低了。您可以增加它,具体的方法会因 VS 版本而略有不同,因此我建议针对您的特定版本搜索如何执行此操作。

关于c++ - 从另一个文件编译 __device__ 函数调用时出现 Visual Studio MSB3721 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45258052/

相关文章:

CUDA : error C2491: 'log1p' : definition of dllimport function not allowed

c++ - 如何检测 Windows 资源管理器是否显示给定文件夹?

c++ - 无法在动态链接库 libstdc++-6.dll 中找到过程入口点 __gxx_personality_v0

.net - 使用官方 Microsoft dotnet docker 镜像在 linux 上构建 dotnet

visual-studio - 在解决方案之间共享 Visual Studio 项目(程序集)的最佳实践是什么

c++ - 模板内的模板 VS 2005 问题?

c++ - CUDA 7.5 实验性 __host__ __device__ lambda

c++ - 如何确定socket listen() backlog 参数的值?

c++ - 虚函数上未解析的外部符号

c++ - CUDA 在多个数据上合并一个 warp