How to add header file path in CMake file

2019-05-23 09:10发布

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?

标签: cmake opencl
3条回答
家丑人穷心不美
2楼-- · 2019-05-23 09:27

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.

查看更多
乱世女痞
4楼-- · 2019-05-23 09:40

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.

查看更多
登录 后发表回答