通过 #include 将文件加载为字符串字面量的技巧

C23 新增的 #embed 预处理指令可以实现相同的功能。
资源文件通常使用 xxd -i <file> 来生成数组。

  • C++:
1#include <iostream>
2#include <string>
3
4#define INCLUDE_FILE_TO_STRING(X, ...) (#X #__VA_ARGS__)
5
6static std::string source = 
7#include "kernel.cl"
8
9int main()
10{
11    std::cout << source << std::endl;
12}
  • kernel.cl:
1INCLUDE_FILE_TO_STRING(
2
3__kernel void matFill(__global float* mat, unsigned len, float value)
4{
5    unsigned idx = get_global_id(0);
6
7    if (idx >= len)
8        return;
9
10    mat[idx] = value;
11}
12
13);

注意事项

  • 必须将 INCLUDE_FILE_TO_STRING( 和结尾的 ); 写在被加载的文件里面,否则无法编译通过。
  • 此方式加载后会丢失换行符,因此不能用于加载强依赖换行符的文本(例如 python 脚本)。
  • 这可能不是一个合规的写法,在 gcc 11.4.0clang 14.0.0 上测试通过,其它编译器可能无法编译通过。