error LNK2019: 无法解析的外部符号
在做一个关于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>生成失败。
请问是怎么回事,应该怎么解决,求大神解答