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.




























Remember the times that the OpenCL compilers where not that good as they’re now? Correct source-code being rejected, typos being accepted, long compile times, crashes during compiling and other irritating bugs. These made the work of an OpenCL developer in “the old days” quite tiresome – you needed a lot of persistence and report bugs. Lucky on desktops the drivers have improved a lot.







Getting your Windows machine ready for OpenCL is rather straightforward. In short, you only need the latest drivers for your OpenCL device(s) and you’re ready to go. Of course, you will need to add an OpenCL SDK in case you want to develop OpenCL applications but that’s equally easy.




This means that with an interest in AI, embedded programming and sensors, you’re all set.
