How to add header file path in CMake file

僤鯓⒐⒋嵵緔 提交于 2019-12-08 01:26:01

问题


I am new to OpenCL. I have written a vector addition code in OpenCL with help from Internet. I have included one header file i.e. CL/cl.h using #include.

I am using NVIDIA graphic card and the OpenCL implementation is NVIDIA_GPU_Computing_SDK. My OpenCL header files are residing at this path /opt/NVIDIA_GPU_Computing_SDK/OpenCL/common/inc. I can run OpenCL programs through linux terminal by adding this path when compiling my code. But now I want to write CMake file for this code. CMake files are working fine for C programs, but not OpenCL programs because of this Path problem. In terminal, I used to enter $cmake ., after this $make, it will search for a Makefile which is created by cmake, now my error is after entering command make

fatal error: CL/cl.h: No such file or directory!

Now tell me how can I include this header file into CMake file?


回答1:


You will need to put these lines into CMakeLists.txt:

include_directories(/opt/NVIDIA_GPU_Computing_SDK/OpenCL/common/inc)
link_directories(/opt/NVIDIA_GPU_Computing_SDK/OpenCL/common/<lib or something similar>)

add_executable(yourexe src1.c ...)
target_link_libraries(yourexe OpenCL)

But beware that it's not portable, because OpenCL SDK can be somewhere else on another machine. The proper way to do this is to use FindOpenCL.cmake module.




回答2:


Maybe you can use a CMake "find" script like:

  • http://gitorious.org/findopencl/findopencl/blobs/master/FindOpenCL.cmake
  • http://code.google.com/p/opencl-book-samples/source/browse/trunk/cmake/FindOpenCL.cmake?r=14

CMake file example from OpenCL Programming Guide Book: http://code.google.com/p/opencl-book-samples/source/browse/trunk/CMakeLists.txt?r=14




回答3:


I was looking for FindOpenCL.cmake macro which would work well on Windows, OSX and Linux... I couldn't find any which did work well on every platform, so I wrote new one which I use in couple of projects (webcl-validator and opencl-testsuite).

https://github.com/elhigu/cmake-findopencl

Especially Windows support is improved in this one.

In Windows it checks if 64bit or 32bit lib should be used and it also tries to find libraries from according to environment variables set by Nvidia, Intel and AMD OpenCL SDKs.

It also tries to find .lib in Cygwin, which didn't work with other scripts I tried.



来源:https://stackoverflow.com/questions/12857189/how-to-add-header-file-path-in-cmake-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!