CUDA和C++函数问题(Visual Studio 2013)
CUDA and C++ function problems ( Visual Studio 2013)
我正试图从Visual Studio中的cpp文件调用cu文件中定义的cuda函数,但我一直收到以下错误。
TomColourCorrection Main.obj:错误LNK2019:未解析的外部符号"public:void __cdecl hwk::TomColourCorrection::bright(int,int)"(?brightness@TomColourCorrection@hwk@@QEAAXHH@Z)在函数"public:virtual void __cdecl hwk::TomColourCorrection::processCore(class std::shared_ptr)"中引用(?processCore@TomColourCorrection@hwk@@UEAAXV$shared_ptr@VIImageProcessingContext@hwk@@@std@@@Z)
现在,通过阅读其他类似的问题,我理解了它与函数的定义有关,也理解了其中存在问题,但当我在头文件和cuda文件中定义时,我看不到它。
这是我的代码(我是CUDA的新手,但我可以很好地编译CUDA,当我不在C++中调用这个函数时,代码就会运行):
头文件
#pragma once
#include "ImageProcessorWithProperties.h"
#include <iostream>
#include <cuda_runtime.h>
#include <cuda.h>
class TomColourCorrection : public ImageProcessorWithProperties, public PropertyConsumer<TomColourCorrection>{
public: TomColourCorrection(PropNodePtr n, std::function<void()> requestReprocess);
virtual void processCore(IImageProcessingContextPtr context);
static void DeclareSettings(hwk::PropNodePtr n);
virtual ~TomColourCorrection();
void brightness(int iw, int ih); (function I am talking about)
};
}
带有函数调用的cpp文件//它只是重要代码的片段,因为它的其余部分对于实际函数本身来说是不必要的
#include "stdafx.h"
#include "TomColourCorrection.h"
#include <opencv2/imgproc/imgproc.hpp>
#include <cv.h>
#include <highgui.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <cuda_runtime.h>
#include <cuda.h>
namespace hwk{
TomColourCorrection::TomColourCorrection(PropNodePtr n, std::function<void()> requestReprocess) :
ImageProcessorWithProperties("sandbox", n, requestReprocess),
PropertyConsumer<TomColourCorrection>(n)
{
}
void TomColourCorrection::processCore(IImageProcessingContextPtr context){
brightness(16, 16); (just generic numbers at the moment as I am trying to resolve this issue etc)
}
}
CUDA文件和功能定义
#include "TomColourCorrection.h"
#include "device_launch_parameters.h"
__global__ void brightness_kernel(int iw, int ih)
{
// Calculate our pixel's location
int x = (blockIdx.x * blockDim.x) + threadIdx.x;
int y = (blockIdx.y * blockDim.y) + threadIdx.y;
// Variables to store the sum
int count = 0;
float sum = 0.0;
// Do the blur operation by summing the surround pixels
/* for (int j = -(bh / 2); j <= (bh / 2); j++)
{
for (int i = -(bw / 2); i <= (bw / 2); i++)
{
// Verify that this offset is within the image boundaries
if ((x + i) < iw && (x + i) >= 0 && (y + j) < ih && (y + j) >= 0)
{
sum += (float)source[((y + j) * iw) + (x + i)];
count++;
}
}
}*/
// Average the sum
sum /= (float)count;
// dest[(y * iw) + x] = (unsigned char)sum;
}
void brightness(int iw, int ih) //, unsigned char *source, unsigned char *dest)
{
// allocate memory for the bitmap in GPU memory
unsigned char *dev_source, *dev_dest;
// cudaHostGetDevicePointer(&dev_source, source, 0);
// cudaHostGetDevicePointer(&dev_dest, dest, 0);
// Run the boxfilter kernel
dim3 blocks(iw / 16, ih / 16);
dim3 threads(16, 16);
// Execute the kernel
brightness_kernel << <blocks, threads >> >(iw, ih);
cudaThreadSynchronize();
}
像这样修改TomColourCorrection.h:
#pragma once
#include "ImageProcessorWithProperties.h"
#include <iostream>
#include <cuda_runtime.h>
#include <cuda.h>
void brightness_wrapper(int, int);
class TomColourCorrection : public ImageProcessorWithProperties, public PropertyConsumer<TomColourCorrection>{
public:
TomColourCorrection(PropNodePtr n, std::function<void()> requestReprocess);
virtual void processCore(IImageProcessingContextPtr context);
static void DeclareSettings(hwk::PropNodePtr n);
virtual ~TomColourCorrection();
void brightness(int iw, int ih);
};
修改你的cpp文件如下:
#include "stdafx.h"
#include "TomColourCorrection.h"
#include <opencv2/imgproc/imgproc.hpp>
#include <cv.h>
#include <highgui.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <cuda_runtime.h>
#include <cuda.h>
namespace hwk{
void TomColourCorrection::brightness(int iw, int ih){
brightness_wrapper(iw, ih);}
TomColourCorrection::TomColourCorrection(PropNodePtr n, std::function<void()> requestReprocess) : ImageProcessorWithProperties("sandbox", n, requestReprocess), PropertyConsumer<TomColourCorrection>(n)
{
}
void TomColourCorrection::processCore(IImageProcessingContextPtr context){
brightness(16, 16);
}
}
在你的cuda文件中更改这个:
void brightness(int iw, int ih) //, unsigned char *source, unsigned char *dest)
到此:
void brightness_wrapper(int iw, int ih) //, unsigned char *source, unsigned char *dest)
这主要是为了拼写Ryck答案的细节。
我认为您需要更改
void brightness(int iw, int ih)
至
void TomColourCorrection::brightness(int iw, int ih)
并将实现移动到头文件或.cpp文件中。
- Visual Studio 2015:Extern "C" 和 "export" 关键字
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 试图在visual studio上用C++创建一个桌面应用程序
- 如何在MS Visual Studio 2019中运行QT UI
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- Visual Studio Code - C++ Debugger 無法啟動
- Visual Studio 2019:插入多个C++风格的单行注释
- Visual Studio Code "undefined reference to `WinMain@16'"
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- Visual Studio中的函数声明和函数定义问题
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- Visual Studio mkl_link_tool.exe链接错误
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 不同的Visual Studio版本中缺少.dll
- 用Visual Studio在C++中嵌入Julia
- Visual Studio 2017循环自动向量化问题
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- Visual Studio在尝试读取resource.txt文件时崩溃
- Visual Studio 2017 不允许我创建 C++ 专用模板