Visual Studio 与 Cuda 的链接问题

发布于 2024-09-14 01:41:27 字数 699 浏览 5 评论 0原文

我正在使用 nVidia 的 CUDA C 进行一些编程。我使用 Visual Studio 2008 作为我的开发环境,我在某些链接方面遇到了一些麻烦,我想知道是否有人知道修复它的方法或遇到相同的问题并可以提供一个解决方案。

我的程序由 3 个文件组成。 1 个头文件 (stuff.h)、1 个 C 源文件 (stuff.c) 和 1 个 CUDA C 文件 (main.cu)。 (这些名字是假的,但这只是为了说明这一点)。

现在 stuff.h/stuff.c 定义/实现一些我从 main.cu 内部调用的辅助函数。

我正在使用 Visual Studio 2008 和来自 nVidia 的 GPU 计算 SDK 的 Cuda.rules,一切都编译得很好,但是......当涉及到将所有文件链接在一起时,它会失败。似乎在 stuff.h 中定义的所有函数(并在 stuff.c 中实现)都没有正确链接,因为它们被标记为“未解析的外部符号”。

可能的原因是什么?我该如何解决这个问题?

非常感谢,

ExtremeCoder


好吧,我已经成功地完成了所有编译。看来我所要做的就是将东西实现文件的扩展名从 .c 更改为 .cpp (这意味着编译为 c++ 可以工作,而编译为 c 则不行!)。

可能是什么原因造成的?我宁愿将所有内容保留为 .c 而不是 .cpp (因为这实际上是 C 代码......

有任何指针吗?

I am doing some programming with nVidia's CUDA C. I am using Visual Studio 2008 as my development environment and I am having some troubles with some linking and I am wondering if someone knows a way to fix it or has had the same problem and could offer a solution.

My program is made up of 3 files. 1 header file (stuff.h), 1 C source file (stuff.c) and 1 CUDA C file (main.cu). (The names are fake but it's just to illustrate the point).

Now stuff.h/stuff.c define/implement some helper functions that I call from inside main.cu.

I am using visual studio 2008 and the Cuda.rules from nVidia's GPU Computing SDK and everything compiles fine but... when it comes to linking all of the files together it fails. It seems that all of the functions defined in stuff.h (and implemented in stuff.c) are not being linked in correctly as they are flagged as "unresolved external symbols".

What are the possible causes and how could I fix this?

Many thanks,

ExtremeCoder


Okay so I have managed to get it all compiling. It seems all I had to do was change the extension of the stuff implementation file from .c to .cpp (meaning compiling as c++ works whereas compiling as c does not!).

What could be causing this? I would rather keep everything as a .c instead of .cpp (as this is really meant to be C code...

Any pointers?

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

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

发布评论

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

评论(2

野心澎湃 2024-09-21 01:41:27

main.cu 文件由 nvcc 处理,默认情况下,nvcc 是一个 C++ 编译器(实际上它是底层 CUDA 编译器和 cl.exe(默认的 MS 编译器)的包装器)。因此,它正在寻找具有 C++ 绑定的函数,而通过将它们编译为 C,您将获得 C 绑定。

如果您想将代码保留为 C,那么您可以编辑 stuff.h 将函数声明为 extern "C":

/* in stuff.h */
if defined(__cplusplus)
    extern "C"
    {
#endif
/* ... your declarations ... */
if defined(__cplusplus)
    }
#endif

或者您可以将 stuff.h 包含在 main.cu 中:

// in main.cu
extern "C"
{
#include "stuff.h"
}

The main.cu file is being processed by nvcc which, by default, is a C++ compiler (actually it's a wrapper around the underlying CUDA compiler and cl.exe, the default MS compiler). As a result it is looking for the functions with C++ binding, whereas by compiling them as C you have the C bindings.

If you want to keep your code as C then you can either edit stuff.h to declare the functions as extern "C":

/* in stuff.h */
if defined(__cplusplus)
    extern "C"
    {
#endif
/* ... your declarations ... */
if defined(__cplusplus)
    }
#endif

Or you can wrap the inclusion of stuff.h in main.cu:

// in main.cu
extern "C"
{
#include "stuff.h"
}
终弃我 2024-09-21 01:41:27

此处有一个 VS 2005 项目,它使用 CUDA 将图像转换为其灰度表示形式。不过,它使用 OpenCV 。如果您已经安装了它应该非常简单。

但即使您没有 OpenCV 并且不想编译应用程序,VS 2008 也可以转换并打开该项目,您将能够看到如何将 CUDA 源代码与 C/C++ 代码分开以及如何正确配置项目属性。

我还应该指出这个伟大的线程:

如何在 Visual Studio 2008 中启动新的 CUDA 项目?

There's a VS 2005 project that uses CUDA to convert images to their grayscale representation here. It uses OpenCV, though. If you have already installed it should be pretty straight forward.

But even if you don't have OpenCV and dont want to compile the application, VS 2008 can convert and open this project, and you'll be able to see how you should separate CUDA source code from the C/C++ code and how to configure the project properties correctly.

I should also point out this great thread:

How do I start a new CUDA project in Visual Studio 2008?

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