OpenCL-HPP setDefault 崩溃

OpenCL-HPP setDefault crash

这是我试图 运行 并理解的一段代码。但它有一个尴尬的错误 设置默认函数。

cmake_minimum_required(VERSION 3.19)
project(OpenCL_HPP)

set(CMAKE_CXX_STANDARD 14)

# find OpenCL
find_package(OpenCL REQUIRED)
find_package(Threads REQUIRED)


include_directories(SYSTEM ${OpenCL_INCLUDE_DIRS})

link_directories(${OpenCL_LIBRARIES})

add_executable(OpenCL_HPP main.cpp)
target_link_libraries(${PROJECT_NAME} ${OpenCL_LIBRARIES} Threads::Threads)

代码:

    #define CL_HPP_ENABLE_EXCEPTIONS
    #define CL_HPP_MINIMUM_OPENCL_VERSION 120
    #define CL_HPP_TARGET_OPENCL_VERSION 200
    
    #include <vector>
    #include <memory>
    #include <algorithm>
    #include <iostream>
    #ifdef __APPLE__
    #include <OpenCL/cl.hpp>
    #else
    #include <CL/cl2.hpp>
    #endif
    
    constexpr int numElements = 32;
    
    
    int main(void)
    {
        // Filter for a 2.0 platform and set it as the default
        std::vector<cl::Platform> platforms;
        cl::Platform::get(&platforms);
        cl::Platform plat;
        for (auto &p : platforms) {
            std::string platver = p.getInfo<CL_PLATFORM_VERSION>();
            if (platver.find("OpenCL 2.") != std::string::npos) {
                plat = p;
            }
        }
        if (plat() == 0)  {
            std::cout << "No OpenCL 2.0 platform found.";
            return -1;
        }
    
    
    /*
        The setDefault chrashes in the call_once function, with error code -1
    */
        cl::Platform newP = cl::Platform::setDefault(platforms[0]);
        //cl::Platform newP = plat;
        if (newP != plat) {
            std::cout << "Error setting default platform.";
            return -1;
        }
    
        return 0;
}

错误:

/home/BLA/CLionProjects/OpenCL_HPP/cmake-build-debug/OpenCL_HPP terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

错误来自 call_once 函数,据我所知,这应该是 pThread 库的一部分,但所有这些都会干扰 stdlib。如果我错了,请纠正我。

我运行的机器是Ubuntu16.04,Opencl来自Intel,我没有安装任何其他OpenCL驱动程序(例如GPU)。此代码是 OpenCL-HPP doxygen 中的主要绑定示例。

我想知道,有没有办法纠正这个问题。 OpenCL-HPP 是否使用 Pthread 库或 STD 库来进行链接?

在对 OpenCL-HPP 进行一些调试和阅读后,我发现了问题。

主要问题是 OpenCL-HPP 使用 pthreads,如果不包括它们/linked 就会出现如上所述的问题。

有帮助的文章:
cmake fails to configure with a pthread error
Cmake error undefined reference to `pthread_create'

主要问题是 Call_once 方法在没有任何真正可以理解的原因的情况下崩溃。该项目将构建。

使一切脱轨的一件事是 CMake,它并不能真正帮助理解 linking 过程。

CMake 设置的输出:

-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for CL_VERSION_2_0
-- Looking for CL_VERSION_2_0 - found
-- Found OpenCL: /usr/lib/x86_64-linux-gnu/libOpenCL.so (found version "2.0") 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Configuring done
-- Generating done

这里的要点是并没有真正找到 pthreads,这对我来说不是很清楚。

find_package(Threads REQUIRED) 
target_link_libraries(${PROJECT_NAME} ${OpenCL_LIBRARIES} Threads::Threads)

代码实际上没有做任何事情,因为 pthreads 没有被正确 linked。或者不承认 pthreads 应该 linked.

将以下代码添加到我的 CMake 后,奇迹发生了,崩溃也奇迹般地消失了。

if( CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -Werror=return-type")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
endif()

这是我的下一个问题。为什么所有这些在 linking 时没有给出更详细的警告或错误?
OpenCL-HPP 文档并没有真正表达 linking 到 pthreads 的需要。结果就是找问题的过程非常痛苦。
为什么 CMake 必须对 CMAKE_CXX_FLAGS 进行这样的设置并且不能 link int target_link_libraries 命令?