1

I'm writing an OpenCL kernel which is then compiled to run on GPU. Kernel code is contained in a *.cl file, which is then read by the application. It's inconvenient to always copy the *.cl file into application folder. Much better solution would be, if the kernel code could be parsed and converted into C char[] string during application compilation. Then there would be no need to provide the original *.cl file to the application's user.

My question therefore is: How can I in the most convenient and simple way convert OpenCL code into a C string, which will then be used by the application, when the application is compiled? Is there any way to do it using C preprocessor or do I need to run a Python, Perl or similar script to do the job?

melpomene
  • 84,125
  • 8
  • 85
  • 148
Matej Gomboc
  • 161
  • 3
  • 10

2 Answers2

0

Remember that C code could be generated. You can generate the C code containing the initialization of your array.

So you can have an (and you can even find some existing) utility which generate some C code like

0x23, 0x34, 0x02, 0x33,

and use appropriately #include with that file. See also this answer.

That utility could be a simple C program, a simple Python script, or some Unix pipeline (combining hd or od with cut or awk or sed, etc...) or something else.

You'll better configure suitably your build automation tool to regenerate such a header when needed. If you use make, you'll just add a few lines (some recipe) in your Makefile.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

It will not be a C string only the byte array. C string is sequence of characters terminated by zero.

Use online tool to convert binary file to the C array for example http://tomeko.net/online_tools/file_to_hex.php?lang=en

0___________
  • 60,014
  • 4
  • 34
  • 74