在VS2008中构建CUDA程序时出现问题:LNK2019

发布于 2024-09-08 01:54:09 字数 3580 浏览 4 评论 0原文

我在构建程序时遇到了一些麻烦。我正在使用 Visual Studio 2008 处理 Windows 7 professional 32 位。我有 Cuda SDK,并且我的项目设置了 cudart.lib 等的所有链接。我的问题是当我尝试构建我的项目时,它返回以下错误:

1>crowdSim.obj:错误 LNK2019: 未解析的外部符号 函数“protected: void __thiscall”中引用的_setParameters 人群::_create(int)" (?_create@Crowd@@IAEXH@Z) 1>crowdSim.obj:错误LNK2019: 未解析的外部符号 _mapBuffer 在函数“protected: void __thiscall Crowd::_create(int)" (?_create@Crowd@@IAEXH@Z) 1>crowdSim.obj:错误LNK2019: 未解析的外部符号 _allocToDevice 在函数“protected: void __thiscall”中引用 人群::_create(int)" (?_create@Crowd@@IAEXH@Z) 1>crowdSim.obj:错误LNK2019: 未解析的外部符号 _registerBuffer 在函数“protected: void __thiscall”中引用 人群::_create(int)" (?_create@Crowd@@IAEXH@Z) 1>../../bin/win32/Debug/crowd.exe : 致命错误 LNK1120:4 未解决 外部

是如何设置“allocToDevice”、“mapBuffer”、“setParameters”和“registerBuffer”方法,因为如果我注释掉这些方法,我就可以毫无问题地构建项目。

我在以下文件中定义了这些方法:

crowdSim.cuh:

    extern "C"
{
    void checkCUDAError(const char *msg);

    void setParameters(SimParams *hostParams);

    void registerBuffer(uint vbo);

    void allocToDevice(void **ptr, int memSize);

    void mapBuffer(void **ptr, uint vbo);
}

crowdSim.cu:

#include <cstdlib.h>
#include <cstdio.h>
#include <string.h>

#include <cuda_gl_interop.h>

// includes, kernels
#include "crowd_kernel.cu"

extern "C"
{
void checkCUDAError(const char *msg)
{
    cudaError_t err = cudaGetLastError();
    if( cudaSuccess != err) 
    {
        fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString( err) );
        exit(-1);
    }                         
}

void setParameters(SimParams *hostParams)
{
    // copy parameters to constant memory
    cudaMemcpyToSymbol(params, hostParams, sizeof(SimParams));
}

void registerBuffer(uint vbo)
{
    cudaGLRegisterBufferObject(vbo);
}



void allocToDevice(void **ptr, size_t memSize)
{
    cudaMalloc(ptr, memSize);
}

void mapBuffer(void **ptr, uint vbo)
{
    cudaGLMapBufferObject(ptr, vbo);
}
} //extern "C"

并且它们仅从crowdSim.cpp中的“Crowd”类中的_create方法调用:

#include <math.h>
#include <memory.h>
#include <cstdio>
#include <cstdlib>
#include <GL/glew.h>

#include "crowdSim.h"
#include "crowdSim.cuh"
#include "crowd_kernel.cuh"

Crowd::Crowd(uint crowdSize) :
    //numP(crowdSize),
    hPos(0),
    hVel(0),
    dPosIn(0),
    dVelIn(0),
    dPosOut(0),
    dVelOut(0)
    {
        params.numBodies = crowdSize;
        _create(crowdSize);
    }

Crowd::~Crowd()
    {
        //_remove();
        crowdSize = 0;
    }

uint
Crowd::newVBO(uint size)
{
    GLuint vbo;
 //   glGenBuffers(1, &vbo);
 //   glBindBuffer(GL_ARRAY_BUFFER, vbo);
 //   glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);
 //   glBindBuffer(GL_ARRAY_BUFFER, 0);
    return vbo;
}

void
Crowd::_create(int numPeople)
{
    crowdSize = numPeople;

    unsigned int memSize = sizeof(float) * crowdSize * 4;

    hPos = new float[crowdSize*4];
    hVel = new float[crowdSize*4];

    hPos = (float*) malloc(memSize);
    hVel = (float*) malloc(memSize);

    posVbo = newVBO(memSize);

    registerBuffer(posVbo);

    allocToDevice((void**) &dPosIn, memSize);
    allocToDevice((void**) &dPosOut, memSize);
    allocToDevice((void**) &dVelIn, memSize);
    allocToDevice((void**) &dVelOut, memSize);

    mapBuffer((void**)&dPosVbo, posVbo);

    setParameters(&params);

}

我觉得我在这里缺少一些非常基本的东西,但我无法弄清楚什么所以任何帮助都会很棒!

I am having some trouble with building my programme. I am working on Windows 7 professional 32-bit with Visual Studio 2008. I have the Cuda SDK and my project is set up with all links to cudart.lib etc. My problem is when I try to build my project it returns the following errors:

1>crowdSim.obj : error LNK2019:
unresolved external symbol
_setParameters referenced in function "protected: void __thiscall
Crowd::_create(int)"
(?_create@Crowd@@IAEXH@Z)
1>crowdSim.obj : error LNK2019:
unresolved external symbol _mapBuffer
referenced in function "protected:
void __thiscall Crowd::_create(int)"
(?_create@Crowd@@IAEXH@Z)
1>crowdSim.obj : error LNK2019:
unresolved external symbol
_allocToDevice referenced in function "protected: void __thiscall
Crowd::_create(int)"
(?_create@Crowd@@IAEXH@Z)
1>crowdSim.obj : error LNK2019:
unresolved external symbol
_registerBuffer referenced in function "protected: void __thiscall
Crowd::_create(int)"
(?_create@Crowd@@IAEXH@Z)
1>../../bin/win32/Debug/crowd.exe :
fatal error LNK1120: 4 unresolved
externals

It seems my problem is with how I am setting up my "allocToDevice", "mapBuffer", "setParameters", and "registerBuffer" methods since if I comment these out I can build the project no problem.

I have defined the methods in the following files:

crowdSim.cuh:

    extern "C"
{
    void checkCUDAError(const char *msg);

    void setParameters(SimParams *hostParams);

    void registerBuffer(uint vbo);

    void allocToDevice(void **ptr, int memSize);

    void mapBuffer(void **ptr, uint vbo);
}

crowdSim.cu:

#include <cstdlib.h>
#include <cstdio.h>
#include <string.h>

#include <cuda_gl_interop.h>

// includes, kernels
#include "crowd_kernel.cu"

extern "C"
{
void checkCUDAError(const char *msg)
{
    cudaError_t err = cudaGetLastError();
    if( cudaSuccess != err) 
    {
        fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString( err) );
        exit(-1);
    }                         
}

void setParameters(SimParams *hostParams)
{
    // copy parameters to constant memory
    cudaMemcpyToSymbol(params, hostParams, sizeof(SimParams));
}

void registerBuffer(uint vbo)
{
    cudaGLRegisterBufferObject(vbo);
}



void allocToDevice(void **ptr, size_t memSize)
{
    cudaMalloc(ptr, memSize);
}

void mapBuffer(void **ptr, uint vbo)
{
    cudaGLMapBufferObject(ptr, vbo);
}
} //extern "C"

and they are only called from the _create method in my "Crowd" class from crowdSim.cpp:

#include <math.h>
#include <memory.h>
#include <cstdio>
#include <cstdlib>
#include <GL/glew.h>

#include "crowdSim.h"
#include "crowdSim.cuh"
#include "crowd_kernel.cuh"

Crowd::Crowd(uint crowdSize) :
    //numP(crowdSize),
    hPos(0),
    hVel(0),
    dPosIn(0),
    dVelIn(0),
    dPosOut(0),
    dVelOut(0)
    {
        params.numBodies = crowdSize;
        _create(crowdSize);
    }

Crowd::~Crowd()
    {
        //_remove();
        crowdSize = 0;
    }

uint
Crowd::newVBO(uint size)
{
    GLuint vbo;
 //   glGenBuffers(1, &vbo);
 //   glBindBuffer(GL_ARRAY_BUFFER, vbo);
 //   glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);
 //   glBindBuffer(GL_ARRAY_BUFFER, 0);
    return vbo;
}

void
Crowd::_create(int numPeople)
{
    crowdSize = numPeople;

    unsigned int memSize = sizeof(float) * crowdSize * 4;

    hPos = new float[crowdSize*4];
    hVel = new float[crowdSize*4];

    hPos = (float*) malloc(memSize);
    hVel = (float*) malloc(memSize);

    posVbo = newVBO(memSize);

    registerBuffer(posVbo);

    allocToDevice((void**) &dPosIn, memSize);
    allocToDevice((void**) &dPosOut, memSize);
    allocToDevice((void**) &dVelIn, memSize);
    allocToDevice((void**) &dVelOut, memSize);

    mapBuffer((void**)&dPosVbo, posVbo);

    setParameters(¶ms);

}

I feel like I am missing something very basic here but I can't work out what so any help would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲念泪 2024-09-15 01:54:09

您是否添加了 cuda.rules 文件以使 Visual Studio 能够识别 .cu 扩展名? cuda.rules 教 VS 如何处理 .cu,以便编译和链接它。请参阅这篇文章< /a> 有关设置的更多信息。

顺便说一句,如果头文件中函数的声明(原型)中有 extern "C" ,那么定义(实现)中就不应该需要它。它可能会让你的代码更整洁 - 一般来说我根本不使用 extern "C"

Have you added the cuda.rules file to enable Visual Studio to recognise the .cu extension? The cuda.rules teaches VS what to do with a .cu such that it will be compiled and linked. See this post for more information on setting it up.

As an aside, if you have extern "C" in the declaration (prototype) of a function in the header file then you shouldn't need it on the definition (implementation). It may keep your code tidier - in general I don't use extern "C" at all.

赠意 2024-09-15 01:54:09

最近,我在从 C++ 代码调用 CUDA 函数时遇到了问题。我决定使用 extern 并遵循了一些在线教程。

阅读你的代码后,我做了一件事不同的事情。我没有包含 cuda 文件 (crowdsim.cuh),而是在 C++ 代码中转发声明了该函数。我重写了 C++ 文件中的 extern 声明,并编译了代码,这次成功了。

这是我参考的教程。希望它有帮助

http: //codereflect.com/2008/09/29/how-to-call-cuda-programs-from-a-cc-application/

I recently had a problem with calling my CUDA functions from my C++ code. I decided to use extern and followed some tutorials online.

After reading your code, there is one thing I did differently. Instead of doing an include of the cuda file (crowdsim.cuh) I forward declared the function in my C++ code. I rewrote the extern declarations in my C++ file, and compiled the code, and this time it worked.

Here's the tutorial I referred. Hope it helps

http://codereflect.com/2008/09/29/how-to-call-cuda-programs-from-a-cc-application/

苯莒 2024-09-15 01:54:09

当然,只有当您没有混合的 c/c++ 和 Cuda 文件时,这种情况才是正确的,在这种情况下,NVCC 可以生成一些用于常规链接的对象以及 GPU 汇编对象语言中的一些代码。

This is true of course only if you don't have mixed c/c++ and Cuda files, in this case the NVCC can genrate some objects for regular linkage and some code in the GPU assembly object language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文