下面是我试图运行和理解的一段代码。但是它在setDefault函数中有一个尴尬的错误。
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’实例后调用
什么():未知错误-1
程序已完成,出口代码134 (被信号6: SIGABRT中断)
错误出现在call_once函数中,据我所知,这应该是pThread库的一部分,但所有这些都会干扰stdlib。如果我错了就纠正我。
我运行它的机器是Ubuntu16.04,Opencl来自英特尔,我没有安装任何其他的OpenCL驱动程序(例如GPU )。此代码是OpenCL doxygen中的主要绑定示例。
我在想,有没有办法纠正这件事。OpenCL是为了链接目的而使用Pthread或STD吗?
发布于 2021-05-26 09:11:55
在对OpenCL进行了一些调试和阅读之后,我发现了这个问题。
主要问题是OpenCL使用p线程,如果它们不包括/链接,就会出现上面所描述的问题。
有助于下列方面的文章:
cmake fails to configure with a pthread error
Cmake error undefined reference to `pthread_create'
Cmake gives me an error (pthread_create not found) while building BornAgain
主要问题是Call_once方法在没有任何真正可以理解的原因的情况下崩溃。不过,这个项目将建立起来。
让所有事情脱轨的是CMake,它并没有真正帮助理解链接过程。
来自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这里的要点是,没有真正找到线程,对我来说这一点还不清楚。
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} ${OpenCL_LIBRARIES} Threads::Threads)代码并没有真正地执行任何操作,因为p线程没有正确地链接。或者,不认识到应该链接这些线程。
一旦我将下面的代码添加到我的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()这是我面临的下一个问题。为什么所有这些在链接时没有给出更详细的警告或错误?
OpenCL文档并没有真正表达链接到线程的需求。因此,一个人在寻找问题的过程中有着相当痛苦的经历。
为什么CMake必须为CMAKE_CXX_FLAGS设置这样的设置,并且不能链接到target_link_libraries命令?
https://stackoverflow.com/questions/67669269
复制相似问题