Linker error using OpenCL 2.0 C++ bindings header file

好久不见. 提交于 2019-12-13 04:45:08

问题


I'm getting a linker error from the OpenCL 2.0 C++ bindings header file cl2.hpp. All my headers files come directly from the Khronos OpenCL registry and I build the OpenCL.lib file myself. I don't get an error using the OpenCL 1.2 C++ bindings header file.

I am using Qt 5.5.0 and Visual Studio C++ 2013 with Windows7 64-bit.

The error is related to multiply defined symbols in multiple source files.

mainwindow.cpp.obj:-1: error: LNK2005: "enum cl::QueueProperties __cdecl
 cl::operator|(enum cl::QueueProperties,enum cl::QueueProperties)"
 (??Ucl@@YA?AW4QueueProperties@0@W410@0@Z) already defined in main.cpp.obj

I don't understand why the compiler is saying this is already defined.

I have narrowed down the problem to this code in the cl2.hpp file

QueueProperties operator|(QueueProperties lhs, QueueProperties rhs)
{
    return static_cast<QueueProperties>(static_cast<cl_command_queue_properties>(lhs) | static_cast<cl_command_queue_properties>(rhs));
}

When I comment that code out my project compiles and runs fine. Do you have any clue to what this problem is? Is it a poor design in the cl2.hpp header file?


回答1:


The issue is that the offending function is a non-inline member function. This means that when cl2.hpp is included from multiple source files, there will be multiple copies of the function definition that will clash when these object files are linked together.

The simple solution is to mark the offending function as inline (as many of the other functions in the header already are).



来源:https://stackoverflow.com/questions/33214838/linker-error-using-opencl-2-0-c-bindings-header-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!