error LNK2019: 无法解析的外部符号

weixin_38285180 2017-04-12 01:27:23
在做一个关于OpenCL的小项目:
/* opencl_test.cpp*/
// newTutorial1.cpp : Defines the entrypoint for the console application.
//
//#include "stdafx.h"(mfc的头文件,此处不需要)
#include <CL/cl.h>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <fstream>

using namespace std;

int convertToString(const char*filename,std::string&s)
{
size_t size;
char* str;
std::fstream f(filename,(std::fstream::in | std::fstream::binary));

if(f.is_open())
{
size_t fileSize;
f.seekg(0,std::fstream::end);
size = fileSize = (size_t)f.tellg();
f.seekg(0,std::fstream::beg);
str = new char[size + 1];
if(!str)
{
f.close();
return 0;
}

f.read(str,fileSize);
f.close();
str[size] = '\0';
s = str;
delete[]str;
return 0;
}
cout<<"Error:failed to open file\n:"<<filename<<endl;
return -1;
}

int main(int argc,char*argv[])
{
//1
cl_uint numPlatforms;
cl_platform_id platform = NULL;
cl_int status = clGetPlatformIDs(0,NULL,&numPlatforms);
if(status != CL_SUCCESS)
{
cout<<"Error:Getting platforms!"<<endl;
return 1;
}
if(numPlatforms>0)
{
cl_platform_id*platforms = (cl_platform_id*)malloc(numPlatforms*
sizeof(cl_platform_id));
status = clGetPlatformIDs(numPlatforms,platforms,NULL);
platform = platforms[0];
free(platforms);
}

//2
cl_uint numDevices = 0;
cl_device_id *devices;
status = clGetDeviceIDs(platform,CL_DEVICE_TYPE_CPU,0,
NULL,&numDevices);
if(numDevices==0)
{
cout<<"No GPU device available."<<endl;
cout<<"Choose CPU as default device."<<endl;

status = clGetDeviceIDs(platform,CL_DEVICE_TYPE_ACCELERATOR,numDevices,NULL,&numDevices);

devices = (cl_device_id*)malloc(numDevices*sizeof(cl_device_id));

status = clGetDeviceIDs(platform,CL_DEVICE_TYPE_ACCELERATOR,numDevices,devices,NULL);
}
else
{
devices=(cl_device_id*)malloc(numDevices*sizeof(cl_device_id));
status=clGetDeviceIDs(platform,CL_DEVICE_TYPE_ACCELERATOR,
numDevices,devices,NULL);
}

//3
cl_context context=clCreateContext(NULL,1,devices,NULL,NULL,NULL);

//4
cl_command_queue commandQueue=clCreateCommandQueue(context,devices[0],0,NULL);

//5
const char*filename="HelloWorld_Kernel.cl";
string sourceStr;
status = convertToString(filename,sourceStr);
const char*source = sourceStr.c_str();
size_t sourceSize[] = {strlen(source)};
cl_program program = clCreateProgramWithSource(context,1,&source,sourceSize,NULL);

//6
status=clBuildProgram(program,1,devices,NULL,NULL,NULL);

//7
const char*input="Gdkkn Vnqkc";
size_t strlength = strlen(input);
cout<<"input string:"<<endl;
cout<<input<<endl;
char*output = (char*)malloc(strlength+1);

cl_mem inputBuffer = clCreateBuffer(context,CL_MEM_READ_ONLY|
CL_MEM_COPY_HOST_PTR,(strlength+1)*sizeof(char),
(void*)input,NULL);
cl_mem outputBuffer = clCreateBuffer(context,CL_MEM_WRITE_ONLY,(strlength+1)*sizeof(char),NULL,NULL);

//8
cl_kernel kernel = clCreateKernel(program,"helloworld",NULL);

//9
status = clSetKernelArg(kernel,0,sizeof(cl_mem),(void*)&inputBuffer);
status = clSetKernelArg(kernel,1,sizeof(cl_mem),(void*)&outputBuffer);

//10
size_t global_work_size[1]={strlength};
status = clEnqueueNDRangeKernel(commandQueue,kernel,1,NULL,
global_work_size,NULL,0,NULL,NULL);

//11
status = clEnqueueReadBuffer(commandQueue,outputBuffer,CL_TRUE,0,
strlength*sizeof(char),output,0,NULL,NULL);
output[strlength]='\0';
cout<<"output string:"<<endl;
cout<<output<<endl;

//12
status = clReleaseKernel(kernel);
status = clReleaseProgram(program);
status = clReleaseMemObject(inputBuffer);
status = clReleaseMemObject(outputBuffer);
status = clReleaseCommandQueue(commandQueue);
status = clReleaseContext(context);

if(output != NULL)
{
free(output);
output = NULL;
}

if(devices != NULL)
{
free(devices);
devices=NULL;
}
return 0;
}

另外是:
/*kernel.cl*/
_kernel void helloworld(_global char*in,_global char*out)
{
int num = get_global_id(0);
out[num] = in[num] + 1;
}

但是却一直报错:
opencl.obj : error LNK2019: 无法解析的外部符号 _clGetPlatformIDs@12,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clGetDeviceIDs@24,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clCreateContext@24,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clReleaseContext@4,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clCreateCommandQueue@20,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clReleaseCommandQueue@4,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clCreateBuffer@24,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clReleaseMemObject@4,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clCreateProgramWithSource@20,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clReleaseProgram@4,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clBuildProgram@24,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clCreateKernel@12,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clReleaseKernel@4,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clSetKernelArg@16,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clEnqueueReadBuffer@36,该符号在函数 _main 中被引用
1>opencl.obj : error LNK2019: 无法解析的外部符号 _clEnqueueNDRangeKernel@36,该符号在函数 _main 中被引用
1>c:\users\leungchinan\documents\visual studio 2012\Projects\opencltest\Debug\opencltest.exe : fatal error LNK1120: 16 个无法解析的外部命令
1>
1>生成失败。
请问是怎么回事,应该怎么解决,求大神解答
...全文
3637 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
another_wood 2020-09-13
  • 打赏
  • 举报
回复
原因分析: 找不到解析的外部符号,通常是说头文件有了,但是没找到库文件。库文件的添加有两步,第一步是指定库文件的目录,第二步是指定具体要用哪个库文件。 解决方案: 1. 右键项目点击属性,设置对应版本(x86/64)的库目录和包含目录. 例如我的64位库目录是C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\lib\x64,包含目录是C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\include\CL. 2. 接下来点击项目属性中的连接器,在附加依赖项中添加OpenCL.lib,从而告诉项目需要依赖这个库文件。
hit_ybsix 2018-11-13
  • 打赏
  • 举报
回复
以上,如图
hit_ybsix 2018-11-13
  • 打赏
  • 举报
回复
如果已经按照https://blog.csdn.net/zhouxuanyuye/article/details/79829953博客描述的配置了库和依赖,那么请检查VS,项目—属性—配置管理器 对应的平台是否和引入的依赖文件是对应的,比如添加的是C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\lib\x64,那么目标平台就要选择x64而不是win32
两猿店 2017-08-10
  • 打赏
  • 举报
回复
楼主解决了吗?我也出现了这样的问题
fisherTHW 2017-04-13
  • 打赏
  • 举报
回复
set the path of opencl lib file in Additional Library Directories. OR manually resolve cl functions in your code, for example, #define DECLFUNC(ret, fname, args) \ typedef CL_API_ENTRY ret (CL_API_CALL *type_##fname)args CL_API_SUFFIX__VERSION_1_0;\ static type_##fname fname = nullptr; #define GETFUNC(fname) fname = (type_##fname)Platform::GetFuncAddress(g_OpenCLdll, #fname); DECLFUNC(clCreateContext); GETFUNC(clCreateContext);

601

社区成员

发帖
与我相关
我的任务
社区描述
异构开发技术
社区管理员
  • OpenCL和异构编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧