在Kernel代码中include头文件
有两种方法:
- 在
clBuildProgram
时,通过options
参数,使用-I /path/of/include/dir
指定include的头文件的路径,可以是相对路径(基于当前CL host所在的应用程序可执行文件so/exe所在的位置)或者绝对路径,这样来提供头文件。但是这样会依赖host上的文件系统,可移植性比较差。在新版本中继续支持这个功能只是为了向后兼容老版本。 - 第二种办法就是把
clBuildProgram
替换为clCompileProgram
和clLinkProgram
。在使用clCompileProgram之前,可以先把头文件编译成program,然后把头文件的program作为参数传进去。然后调用clLinkProgram,生成可执行的CL可执行程序。
5.8.5. Separate Compilation and Linking of Programs
- Separate compilation and link stages. Program sources can be compiled to generate a compiled binary object and linked in a separate stage with other compiled program objects to the program executable.
- Embedded headers. In OpenCL 1.0 and 1.1, the I build option could be used to specify the list of directories to be searched for headers files that are included by a program source(s). OpenCL 1.2 extends this by allowing the header sources to come from program objects instead of just header files.
- Libraries. The linker can be used to link compiled objects and libraries into a program executable or to create a library of compiled binaries.
This option is not portable due to its dependency on host file system and host operating system. It is supported for backwards compatibility with previous OpenCL versions. Developers are encouraged to create and use explicit header objects by means of clCompileProgram followed by clLinkProgram.