I was wondering if it's possible to pass macros that take arguments on the compile line to gcc or other C/C++ compilers.
I've never seen this before, but it would actually be useful for some of the OpenCL development that I've been doing where I want to replace a function name with a macro that can replaced at compile time.
Below is an example:
int y, x = 0;
y = HASH(x);
It would be nice if it were possible to define HASH as a macro on the compile line, so that when I compile the program, I could redefine HASH as necessary. For instance, it would be awesome if I could do gcc -DHASH(X)=(hash_fcn1(X)) program.c -o program
, but I've never seen this kind of thing before.
I've tried it with clBuildProgram but with no luck.
I realize that I could just have another program that passes over the program and substitutes the correct function name in for HASH, but I'm wondering if there's an easy way to do this without using a tool like sed, awk, or a string substitution or regex library in my language of choice.
Another solution would be to define a flat macro on the command line, and then have a series of guards in the actual source file that control how the macro gets defined in the source file, e.g. like in this other post how to compare string in C conditional preprocessor-directives.