绑定头文件的链接器错误

绑定头文件的链接器错误

本文介绍了使用OpenCL 2.0 C ++绑定头文件的链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从OpenCL 2.0 C ++绑定头文件中收到链接器错误 cl2.hpp .我所有的标头文件都直接来自 Khronos OpenCL注册表和文件我.使用OpenCL 1.2 C ++绑定头文件不会出现错误.

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.

我正在将Qt 5.5.0和Visual Studio C ++ 2013与Windows7 64位一起使用.

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.

我已将问题缩小到cl2.hpp文件中的这段代码

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));
}

当我注释掉该代码时,我的项目将编译并运行良好.您对这个问题有什么线索吗? cl2.hpp头文件中的设计不好吗?

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?

推荐答案

问题是有问题的函数是非内联成员函数.这意味着当多个源文件中包含cl2.hpp时,将有多个函数定义副本,当这些目标文件链接在一起时,它们会发生冲突.

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.

简单的解决方案是将有问题的功能标记为inline(与标头中的许多其他功能一样).

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

这篇关于使用OpenCL 2.0 C ++绑定头文件的链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 18:23