There has been quite some “find OpenCL” code for CMake around. If you haven’t heard of CMake, it’s the most useful cross-platform tool to make cross-platform software.
Put this into CMakeLists.txt, changing the names for the executable.
#Minimal OpenCL CMakeLists.txt by StreamHPC
cmake_minimum_required (VERSION 3.1)
project(GreatProject)
# Handle OpenCL
find_package(OpenCL REQUIRED)
include_directories(${OpenCL_INCLUDE_DIRS})
link_directories(${OpenCL_LIBRARY})add_executable (main main.cpp)
target_include_directories (main PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries (main ${OpenCL_LIBRARY})
Then do the usual:
- make a build-directory
- cd build
- cmake .. (specifying the right Generator)
Adding your own CMake snippets and you’re one happy dev!
Cmake 3.7
CMake 3.7 makes it even easier! You can do the following:
find_package(OpenCL REQUIRED)
add_executable(test_tgt main.c)
target_link_libraries(test_tgt OpenCL::OpenCL)
This automatically sets up the include paths and target library to link against. No need to use the ${OpenCL_INCLUDE_DIRS} and ${OpenCL_LIBRARIES} any more.
(Thanks Matthäus G. Chajdas for improving this!)
Getting CMake 3.1 or higher
- Ubuntu/Debian: Get the PPA.
- Other Linux: Get the latest tar.gz and compile.
- Windows/OSX: Download the latest exe/dmg from the CMake homepage.
If you have more tips to share, put them in the comments.
Hi,
When I use the script avobe, is true that the project compile, but when I execute the resulting binary I get the error:
Failed to load kernel.
What modifications are needed in order to produce the right binary?
It seems that the kernel-file is not there. Easiest option is to copy kernel-files to the build directory during the build, as explained here: https://stackoverflow.com/questions/13429656/cmake-how-to-copy-contents-of-a-directory-into-build-directory-after-make