You need to declare your function as
void CL_CALLBACK pfn_notify(.....
As the commentors point out; if this code is in a C++ file then it also needs to be:
extern "C" void CL_CALLBACK pfn_notify(.....
Question
I'm taking my first steps into both C++ and OpenCL to perform parallel computing, but I'm running into a bug attempting to pass a listener function to clCreateContext. My program (not shown) is crashing without error, so I need to add a function to forward OpenCL errors to stdout/stderr. The clCreateContext function has an argument for a function pointer that can be set to forward error messages to stdout or stderr. I get a compile time error however using Codeblocks/MinGW:
invalid conversion from 'void (*)(const char*, const void*, size_t, void*)' to
'void (*)(const char*, const void*, size_t, void*)'
I've replicated the problem in the code below:
#include <stdlib.h>
#include <stdio.h>
#include <CL\cl.h>
void pfn_notify(const char *errinfo, const void *private_info, size_t cb, void *user_data)
{
fprintf(stderr, "OpenCL Error (via pfn_notify): %s\n", errinfo);
}
int main()
{
/*Get platform and device info*/
cl_platform_id platform_id = NULL;
cl_uint ret_num_platforms;
cl_int ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &ret_num_devices);
/*Create openCL context*/
cl_context context = clCreateContext(NULL, 1, &device_id, &pfn_notify, NULL, &ret);
/*Some line after this throws an error*/
}
I have seen code examples of using this exact method and pfn_notify that have worked, and am not sure why my program isn't even compiling. Thanks in advance, and let me know if there's anything else I can post to help.
Solution
You need to declare your function as
void CL_CALLBACK pfn_notify(.....
As the commentors point out; if this code is in a C++ file then it also needs to be:
extern "C" void CL_CALLBACK pfn_notify(.....