为什么 OpenCV 无法在 NVCC 中编译?
我正在尝试将 CUDA 和 openCV 集成到一个项目中。问题是当使用 NVCC 时 openCV 无法编译,而普通的 c++ 项目可以编译得很好。这对我来说似乎很奇怪,因为我认为 NVCC 将所有主机代码传递给 c/c++ 编译器,在本例中为 Visual Studio 编译器。
我得到的错误是?
c:\opencv2.0\include\opencv\cxoperations.hpp(1137): 错误: 没有运算符“=”与这些操作数匹配 操作数类型为: const cv::Range = cv::Range
c:\opencv2.0\include\opencv\cxoperations.hpp(2469): 错误:重载函数“std::abs”的多个实例与参数匹配列表: 函数“abs(长双精度)” 函数“abs(浮点)” 函数“abs(双)” 函数“abs(长)” 函数“abs(int)” 参数类型是: (ptrdiff_t)
所以我的问题是为什么考虑到相同的编译器(应该)正在使用差异,其次我如何解决这个问题。
I am trying to integrate CUDA and openCV in a project. Problem is openCV won't compile when NVCC is used, while a normal c++ project compiles just fine. This seems odd to me, as I thought NVCC passed all host code to the c/c++ compiler, in this case the visual studio compiler.
The errors I get are?
c:\opencv2.0\include\opencv\cxoperations.hpp(1137): error: no operator "=" matches these operands
operand types are: const cv::Range = cv::Range
c:\opencv2.0\include\opencv\cxoperations.hpp(2469): error: more than one instance of overloaded function "std::abs" matches the argument list:
function "abs(long double)"
function "abs(float)"
function "abs(double)"
function "abs(long)"
function "abs(int)"
argument types are: (ptrdiff_t)
So my question is why the difference considering the same compiler (should be) is being used and secondly how I could remedy this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一般来说,我建议将主机代码和 CUDA 代码分开,仅对内核和主机“包装器”使用 nvcc。这对于 Visual Studio 来说特别容易,像平常一样创建项目(例如控制台应用程序),然后在 .cpp 文件中实现应用程序。当您想要运行 CUDA 函数时,请在一个或多个 .cu 文件中创建内核和包装器。 SDK 提供的 Cuda.rules 文件将自动启用 VS 编译 .cu 文件并将结果与其余 .cpp 文件链接。
In general I would recommend keeping separation between host code and CUDA code, only using nvcc for the kernels and host "wrappers". This is particularly easy with Visual Studio, create your project as normal (e.g. a Console application) and then implement your application in .cpp files. When you want to run a CUDA function, create the kernel and a wrapper in one or more .cu files. The Cuda.rules file provided with the SDK will automatically enable VS to compile the .cu files and link the result with the rest of the .cpp files.
NVCC 将 C++ 代码传递给主机编译器,但它必须首先解析并理解代码。不幸的是,NVCC 在 STL 方面遇到了麻烦。如果可能的话,将使用 STL 的代码分离到 .cpp 文件中,并使用 Visual Studio 编译这些代码(无需首先通过 NVCC 传递它们)。
NVCC passes C++ code through to the host compiler, but it has to first parse and understand the code. Unfortunately, NVCC has troubles with STL. If at all possible, separate code that makes use of STL into .cpp files and have those compiled with Visual Studio (without passing them first through NVCC).
将 .cu 代码编译为库,然后将其链接到主程序。我建议使用 cmake,因为它使过程变得轻而易举
Compile the .cu code as a library and then link it to the main program. I suggest to use cmake as it makes the process a breeze
cuda-grayscale 上有一个项目,展示了如何将 OpenCV + CUDA 集成在一起。如果您下载源代码,请检查 Makefile:
这是一个非常简单的项目,演示了如何将常规 C++ 代码(OpenCV 等)与 CUDA 代码分离并编译它们。
There's a project hosted at cuda-grayscale that shows how to integrate OpenCV + CUDA together. If you download the sources check the Makefile:
It's a very simple project that demonstrates how to separate regular C++ code (OpenCV and stuff) from the CUDA code and compile them.
因此,没有简单的方法使用 nvcc 来编译当前的 C++ 代码,您必须编写包装器并使用 nvcc 编译它们,同时使用 g++ 或什么来编译其余代码?
So there's no easy way to use nvcc to compile your current C++ code, you have to write wrappers and compile them using nvcc, while you compile the rest of your code using g++ or what-have-you?