Pass symbol definitions with spaces as build options?

I would like to define some macros for my OpenCL program, where the macro definition will have several spaces. (also potentially special characters like newline and tab)

I want to pass the definition as

-D=my_string

to clBuildProgram() as the options argument.

I have tried enclosing my_string in single quotes and double quotes. Either way, the quote characters seem to not do what I want when the definition contains a space, or are added to the symbol definition if there are no spaces in the definition.

When passing the string:

-D ASYMBOL='two words'

I get an OpenCL API error:

[CL_INVALID_BUILD_OPTIONS] : OpenCL Error : clBuildProgram failed: Invalid build options 

The error stream also prints out the build options string exactly as I have listed above.

When passing the string:

-D ASYMBOL='oneword'

I get an OpenCL compiler error:

<program source>:763:2: warning: multi-character character constant

Can anyone help please?

Implementation:
Apple 10.7.2

never mind the about newlines and tabs. I can see they aren’t going to be allowed. From the reference pages:

The contents of definition are tokenized and processed as if they appeared during translation phase three in a `#define’ directive. In particular, the definition will be truncated by embedded newline characters.

But I sure would like spaces!

For strings you need double quotes like any other C string. ‘’ is for character constants, hence your warning message.

I don’t see why
can’t be used to escape a newline just like any other string define.

You might need additional escapes depending on what language you’re running it from. e.g. from C you will need something like

“-Dfoo="foo bar
baz"”

So that after the first escape it becomes:
-Dfoo=“foo bar
baz”

i.e. there is no embedded newline.

If that doesn’t work, just insert the strings manually before compiling it.

Thanks for the thought, but I do not want to produce a string literal, I want a code literal. I’m working on doing some light dynamic OpenCL code generation.

Suppose I wanted to replace a macro symbol INSERT_DECLARATION_HERE within a source file with the characters:

float sum(float a, float b);

There is no way to specify that without whitespace. And you can see in context, I don’t want a string literal in double quotes; I want code.

Yes, I acknowledge that I could do my own source code search and replace before sending the source code to clBuildProgram(). However, what I am trying to accomplish is straightforward enough that I just want the OpenCL preprocessing stage to take care of this for me.

You do realise C compilers totally ignore newlines out side of strings, so why would you even want to add them?

Suppose I wanted to replace a macro symbol INSERT_DECLARATION_HERE within a source file with the characters:

float sum(float a, float b);

There is no way to specify that without whitespace. And you can see in context, I don’t want a string literal in double quotes; I want code.

Yes, I acknowledge that I could do my own source code search and replace before sending the source code to clBuildProgram(). However, what I am trying to accomplish is straightforward enough that I just want the OpenCL preprocessing stage to take care of this for me.

Well I was only trying with gcc, it seems the opencl compiler is broken wrt -D.

Since you’re obviously choosing a few pre-set values just pass in an option which uses the in-line pre-processor to select what to generate. Otherwise write your own generator or template processor - the latter wouldn’t be difficult.