在OpenCL clCreateContext中从void(*)(...)到void(*)(...)的转换无效

时间:2014-05-08 21:41:08

标签: c++ c opencl

我正在迈出C ++和OpenCL的第一步来执行并行计算,但是我遇到了一个试图将侦听器函数传递给clCreateContext的错误。我的程序(未显示)崩溃没有错误,所以我需要添加一个函数来将OpenCL错误转发给stdout / stderr。 clCreateContext函数有一个函数指针的参数,可以设置为将错误消息转发给stdout或stderr。我得到编译时错误,但使用Codeblocks / MinGW:

invalid conversion from 'void (*)(const char*, const void*, size_t, void*)' to 
'void (*)(const char*, const void*, size_t, void*)'

我在下面的代码中复制了这个问题:

#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*/
}

我见过使用这个确切方法和pfn_notify的代码示例,但我不确定为什么我的程序甚至没有编译。在此先感谢,如果还有其他任何我可以发帖帮助,请告诉我。

1 个答案:

答案 0 :(得分:4)

您需要将您的功能声明为

void CL_CALLBACK pfn_notify(.....

评论员指出;如果此代码在C ++文件中,那么它也需要是:

extern "C" void CL_CALLBACK pfn_notify(.....